Numpy

Creation

import numpy as np

A = np.array([[1,2,3],[4,5,6],[7,8,9]])
B = np.random.randn(5,1)
C = np.zeros((1,3))

Andrew Ng Tips

  • Andrew Ng, call.reshape to document the size you expect things to be, its cheap

  • Don't use rank 1 arrays like np.random.randn(5) use np.random.randn(5,1) , rank 1 won't act as col or row

Info

x.shape[0]

Operations

Broadcasting

A = np.array([1,2,3,4])
A + 100 #elementwise addition

A = np.array([[1,2,3], [4,5,6]]) + np.array([[100,200,300]])
#adds 100, 200, 300 to both rows

(m, n) +-/* (1,n) => (m,n)

  • if either row or col is 1, than that will be expanded to the proper size

Common ops

Reducers

  • Axis:

    • 0 is vertical, column

    • 1 is horizontal, row

    • default is all in 2D

  • keepdims keeps it as a matrix instead of collapsing to a 1D vector i.e (x, 1) instead of (x,)

Reshaping

Normalize

For normalizing machine learning stuff

Last updated