Skip to content

Instantly share code, notes, and snippets.

@rcanepa
Last active July 23, 2018 00:33
Show Gist options
  • Save rcanepa/961529fcd27827c756e3a681562e7e16 to your computer and use it in GitHub Desktop.
Save rcanepa/961529fcd27827c756e3a681562e7e16 to your computer and use it in GitHub Desktop.
Numpy
# Element wise sum and multiplication
np.array([3, 1]) + np.array([2, 1]) # => np.array([5, 1])
np.array([3, 1]) * np.array([2, 1]) # => np.array([6, 1])
# Square root
np.sqrt(np.array([1,2])) # => array([1, 1.41421356])
# Magnitude of a vector
np.linalg.norm(np.array([1, 2])) # => 2.23606797749979
# Dot product between two arrays
np.array([2, 1]).dot(np.array([1, 2])) # => 4
# Return the sum of every element of the array
np.array([2, 1]).sum() # => 4
# Create an array of 10 zeros
np.zeros(10)
# Create a 2D array of `rows` rows and `columns` columns filled with zeros
np.zeros((rows, columns))
# Create an array of 10 ones
np.ones(10)
# Create a 2D array of `rows` rows and `columns` columns filled with ones
np.ones((rows, columns))
# Create a 2D array of `rows` rows and `columns` columns filled with random
# numbers between 0 and 1
np.random.random((rows, columns))
# Create a 2D array of `rows` rows and `columns` columns filled with random
# from a Gaussian distribution with mean 0 and var 1
# Notice that in this case, we are not passing a tuple
np.random.randn(rows, columns)
# Matrices multiplication
# Inner dimensions must match
# We can multiply A(2, 3) by B(3, 3) but we can't B(3, 3) * A(2, 3)
# In numpy, * does an element wise type of multiplication
A.dot(A)
# array([[ 7, 10],
# [15, 22]])
# Matrix inverse
A = np.array([[1,2], [3,4]])
# array([[1, 2],
# [3, 4]])
np.linalg.inv(A)
# array([[-2. , 1. ],
# [ 1.5, -0.5]])
# A * Ainv = identity matrix (1 on the diagonal, zeros in the rest)
A.dot(np.linalg.inv(A))
# array([[1.0000000e+00, 0.0000000e+00],
# [8.8817842e-16, 1.0000000e+00]])
# Matrix determinant
np.linalg.det(A)
# -2.0000000000000004
# Matrix diagonal
np.diag(A)
# array([1, 4])
# Create a diagonal matrix (the rest are zeros)
np.diag([1,2,3])
# array([[1, 0, 0],
# [0, 2, 0],
# [0, 0, 3]])
# Outer product between arrays
a = np.array([1,2])
b = np.array([3,4])
np.outer(a, b)
# array([[3, 4],
# [6, 8]])
# Matrix trace (sum of the diagonal)
np.trace(A)
# 5
# Eigenvalues and Eigenvectors with eig and eigh
X = np.random.randn(100, 3) # for instance: 100 samples and 3 features
cov = np.cov(X.T) # covariance matrix
cov.shape
# (3, 3)
np.linalg.eig(cov)
# (array([1.27646852, 0.63402007, 0.92477144]),
# array([[-0.59240802, -0.74197816, 0.31388079],
# [-0.68828838, 0.66860896, 0.28146255],
# [ 0.41870257, 0.04929983, 0.90678425]]))
np.linalg.eigh(cov)
# (array([0.63402007, 0.92477144, 1.27646852]),
# array([[ 0.74197816, 0.31388079, -0.59240802],
# [-0.66860896, 0.28146255, -0.68828838],
# [-0.04929983, 0.90678425, 0.41870257]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment