Beispiel #1
0
def directional_variance_gradient(matrix: Matrix, w: Vector) -> float:
    w_dir = direction(w)
    return [
        sum(2 * dot_product(v, w_dir) * v[i] for v in matrix)
        for i in range(len(w))
    ]
Beispiel #2
0
def directional_variance(matrix: Matrix, v: Vector) -> float:
    v_dir = direction(v)
    return sum(dot_product(v, v_dir)**2 for v in matrix)
Beispiel #3
0
def ridge_penalty(beta: Vector, alpha: float) -> float:
    """The larger alpha is, the larger the penalty is"""
    return alpha * dot_product(beta[1:], beta[1:])
Beispiel #4
0
def transform_vector(v: Vector, components: Matrix) -> Vector:
    return [dot_product(v, w) for w in components]
Beispiel #5
0
def predict(xs: Vector, beta: Vector) -> float:
    assert xs[0] == 1, "The first element of xs must be 1"
    return dot_product(xs, beta)