Linear Algebra

Vectors, pg 84


In [1]:
height_weight_age = [70, # inches
                    170, # pounds
                    40] # years

grades = [95, 80, 75, 62] # exams 1..4

In [2]:
# vector calculations are componentwise
# so we need to zip vectors together
def vector_add(v, w):
    """adds corresponding elements"""
    return [v_i + w_i
           for v_i, w_i in zip(v, w)]

In [3]:
v = [1, 2]
w = [3, 3]

vector_add(v, w)


Out[3]:
[4, 5]

In [4]:
def vector_subtract(v, w):
    """subtracts corresponding elements"""
    return [v_i - w_i
           for v_i, w_i in zip(v, w)]

In [5]:
v = [1, 2]
w = [3, 3]
vector_subtract(v, w)


Out[5]:
[-2, -1]

In [27]:
one = [1, 1]
two = [2, 3, 4]
[w_i - v_i for v_i, w_i in zip(one, two)]


Out[27]:
[1, 2]

Vector Sums, pg 86


In [28]:
def vector_sum(vectors):
    """sums add corresponding elements"""
    result = vectors[0] # start with first vector
    for vector in vectors[1:]: # then loop over the others
        result = vector_add(result, vector) # and add them to the result
    return result

In [30]:
# can just use reduce
def vector_sum(vectors):
    return reduce(vector_add, vectors)

# or even
from functools import partial
vector_sum = partial(reduce, vector_add)

In [31]:
# need to multiply by scalar
def scalar_multiply(c, v):
    """c is a number, v is a vector"""
    return [c * v_i for v_i in v]

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

In [33]:
# dot product = sum of componentwise products
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 [35]:
dot([2,1], [2, 3])


Out[35]:
7

In [36]:
# vector sum of squares
def sum_of_squares(v):
    """v_1 * v_1 + ... + v_n * v_n"""
    return dot(v, v)

In [37]:
# can compute magnitude
import math
def magnitude(v):
    return math.sqrt(sum_of_squares(v))

In [38]:
# can compute distance between two vectors
def squared_distance(v, w):
    """(v_1 - w_1) ** 2 + ... + (v_n - w_n)**2"""
    return sum_of_squares(vector_subtract(v, w))
def distance(v, w):
    return math.sqrt(squared_distance(v, w))

# or
def distance(v, w):
    return magnitude(vector_subtract(v, w))

Matrices, pg 91


In [40]:
# lists of lists, 
# with inner lists being rows of numbers
# in python, start at index 0 instead of 1
A = [[1, 2, 3],
     [4, 5, 6]] # two rows, three columns

B = [[1, 2],
     [3, 4],
     [5, 6]] # three rows, two columns

In [41]:
def shape(A):
    num_rows = len(A)
    num_cols = len(A[0]) if A else 0 # num elements in first row
    return num_rows, num_cols

# n rows, k columns; n x k matrix

def get_row(A, i):
    return A[i]

def get_column(A, j):
    return [A_i[j]
            for A_i in A]

In [42]:
# create a matrix
def make_matrix(num_rows, num_cols, entry_fn):
    """returns a num_rows x num_cols matrix
    whose [i,j]th entry is entry_fn(i, j)"""
    return[[entry_fn(i, j)               # given i, create a list
            for j in range(num_cols)] # [entry_fn(i, 0), ...]
           for i in range(num_rows)]  # create one list for each i

In [43]:
def is_diagonal(i, j):
    """1's on the diagonal and 0's everywhere else"""
    return 1 if i == j else 0

identity_matrix = make_matrix(5, 5, is_diagonal)

In [44]:
print identity_matrix


[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]

In [ ]: