Numpy 
import numpy as np
- Casting list to a np array:
np.array([1,2,3])
arange
Returns integers given a start, stop (not included), and stepsize (default is 1):
np.arange(start=0,stop=10,step=2)
returns: array([0,2,4,6,8])
zeros and ones:
- zeros
np.zeros((3,4))
returns:
array([[0.,0.,0.,0.],
[0.,0.,0.,0.],
[0.,0.,0.,0.]])
- ones
np.ones((3))
returns:
array([1.,1.,1.])
- another arbitrary number by multiplying times ones
np.ones((4))*5
returns:
array([5.,5.,5.,5.])
linspace:
Evenly spaced numbers over a specified interval. Includes stop value.
np.linspace(0,10,3)
returns
array([0., 5., 10.])
Identity matrix:
np.eye(3)
returns
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Percentiles
q75, q25 = np.percentile(sample,[75,25])
Reshaping
arr = np.arange(24)
arr.shape # --> (25,)
arr = arr.reshape(5,5) # Not permanent. Need to reassign.
arr.shape # --> (5,5)
max and argmax:
arr.max() # --> Returns max value in the array
arr.argmax() # --> Returns the index location of the max value in the array
- similar with min and argmin.
Data types:
arr.dtype # --> Returns, e.g.dtype('int32')
Random sampling
Seed
np.random.seed(101)
Uniform sampling:
The line below returns three random numbers in the range [0,1) sampled uniformly:
np.random.rand(3)
- The following gives a shape of 3 rows by 4 columns:
np.random.rand(3,4)
Normal sampling:
- standard normal distribution (mean=0, variance=1):
np.random.randn(3)
- more general normal distribution:
np.random.normal(mean,std_dev,shape)
Random integers:
np.random.randint(start,stop,shape)
Numpy operations
arr = np.array(0,1,2)
arr + 5
this returns array([5,6,7]) as the operation is done on an element-by-element basis.
- Similar with array-array operations:
arr = np.array(2,3,4)
arr*arr
this returns: array([4,9,16]).
Covariance matrix, eigenvalues and eigenvectors:
covariance_matrix = np.cov(scaled_X,rowvar=False)
eigen_values, eigen_vectors = np.linalg.eig(covariance_matrix)