Skip to content
5u2ie edited this page Dec 13, 2019 · 3 revisions

NUMPY

  • normal list built-in methods don't work on numpy arrays

  • in python + just concatenates

  • in numpy + it does element-wise vector addition

  • has instance methods: dot(), sum()

  • has linalg methods:

    • np.linalg.norm(A)
    • np.linalg.inv(A)
    • np.linalg.det(A)
    • np.linalg.diag(A)
  • matrix datatypes

    • np.matrix, np.array (just use the array always)
    • can be sliced
  • matrix methods:

    • np.mean(), .var()
    • np.trace(a) -- sum of the diagonal

INNER (DOT) PRODUCT

C = sum over i {A(i)B(i)} a.dot(b) SAME AS np.inner(a,b)

MATRIX PRODUCTS

  • inner dimensions of the 2 matrices have to match
  • sum of dot products for each pair
  • using * in numpy

OUTER PRODUCT

  • when calculating the covariance of
  • C(i,j) = A(i)B(j)
  • np.outer()

Eigenvalues, eigenvectors

  • often apply this to covariance matrix of a dataset (symmetrical)
  • to get covariance: np.cov(X.T)
  • np.linalg.eig() for
  • np.linalg.eigh(cov) for symmetric (equal to the transpose of itself) / Hermitian matrices (equal to the conjugate transpose of itself)

Solving linear systems

Ax = b

A - a matrix x - column we are trying to solve for b = vector of numbers

solution: multiply by A(-1) (A inverse) on both sides

x = np.linalg.inv(A).dot(b)

-- alternatively: x = np.linalg.solve(A,b)