Exemple #1
0
def find_eigenvector(A, tolerance=0.00001):
    guess = [1 for __ in A]

    while True:
        result = matrix_operate(A, guess)
        length = magnitude(result)
        next_guess = scalarMultiply(1 / length, result)

        if distance(guess, next_guess) < tolerance:
            return next_guess, length  # eigenvector, eigenvalue

        guess = next_guess
Exemple #2
0
def find_eigenvector(A, tolerance=0.00001):
    guess = [1 for __ in A]

    while True:
        result = matrix_operate(A, guess)
        length = magnitude(result)
        next_guess = scalarMultiply(1/length, result)

        if distance(guess, next_guess) < tolerance:
            return next_guess, length # eigenvector, eigenvalue

        guess = next_guess
def minimizeStochastic(targetFn, gradientFn, x, y, theta0, alpha0=0.01):
    data = zip(x, y)
    theta = theta0
    alpha = alpha0
    minTheta, minValue = None, float("inf")
    iterationsWithNoMovement = 0

    while iterationsWithNoMovement < 100:
        value = sum([targetFn(xi, yi, theta) for xi, yi in data])

        if value < minValue:
            minTheta, minValue = theta, value
            iterationsWithNoMovement = 0
            alpha = alpha0
        else:
            iterationsWithNoMovement += 1
            alpha *= 0.9

        for xi, yi in inRandomOrder(x, y):
            gradientI = gradientFn(xi, yi, theta)
            theta = vectorSubtract(theta, scalarMultiply(alpha, gradientI))

    return minTheta
def minimizeStochastic(targetFn, gradientFn, x, y, theta0, alpha0=0.01):
    data = zip(x, y)
    theta = theta0
    alpha = alpha0
    minTheta, minValue = None, float("inf")
    iterationsWithNoMovement = 0

    while iterationsWithNoMovement < 100:
        value = sum([targetFn(xi, yi, theta) for xi, yi in data])

        if value < minValue:
            minTheta, minValue = theta, value
            iterationsWithNoMovement = 0
            alpha = alpha0
        else:
            iterationsWithNoMovement += 1
            alpha *= 0.9

        for xi, yi in inRandomOrder(x, y):
            gradientI = gradientFn(xi, yi, theta)
            theta = vectorSubtract(theta, scalarMultiply(alpha, gradientI))

    return minTheta
Exemple #5
0
def project(v, w):
    projectionLength = dotProduct(v, w)
    return scalarMultiply(projectionLength, w)
Exemple #6
0
def project(v, w):
    projectionLength = dotProduct(v, w)
    return scalarMultiply(projectionLength, w)