In [5]:
height_weight_age = [70, 170, 40]

In [12]:
grades = [95, 80, 75, 62]

In [13]:
def vector_add(v, w):
    """ add corresponding elements"""
    return [v_i + w_i for v_i, w_i in zip(v, w)]

In [18]:
def vector_substract(v, w):
    """ substracts corresponding elements"""
    return [v_i - w_i for v_i, w_i in zip(v, w)]

In [19]:
def vector_sum(vectors):
    """sums all corresponding elements"""
    return reduce(vector_add, vectors)

In [20]:
def scalar_multiply(c, v):
    """c is a number, v is a vector"""
    return [c * v_i for v_i in v]

In [21]:
def vector_mean(vectors):
    """compute the vector whose ith element is the mean of the ith elements of the input vectors"""
    n = len(vectors)
    return scalar_multiply(1/n, vector_sum(vectors))

In [22]:
def dot(v, w):
    """v_1 * w_1 + ... + v_n * w_n"""
    return sum(v_i * w_i for v_i, w_i in zip(v, w))

In [23]:
def sum_of_squares(v):
    return dot(v, v)

In [24]:
import math
def magnitude(v):
    return math.sqrt(sum_of_squares(v))

In [25]:
magnitude(grades)


Out[25]:
157.77832550765646

In [26]:
def distance(v, w):
    return magnitude(vector_substract(v, w))

In [28]:
distance(height_weight_age, grades)


Out[28]:
99.74968671630002

In [29]:
def cosine_similarity(v, w):
    return dot(v, w) / math.sqrt(dot(v, v) * dot(w, w))

In [30]:
cosine_similarity(height_weight_age, grades)


Out[30]:
0.7832023530808994