Linear Algebra Python Flashcards
Initialize a vector
- v = [3,2]
- v1 = np.array([3,2])
Addition of two vectors
v1 + v2
Scalar multiplication
2 * v
Transpose of a vector
- np.transpose(v)
- v.T
- v.T.T
Dot product approaches
- np.dot(v1, v2)
- np.matmul(v1, v2)
- sum(np.multiply(v1, v2))
Create one random number
np.random.rand/ np.random.randn
Create random vector of size 10
np.random.randn(10)
create a 3 * 1 matrix of random numbers
np.random.randn(3,1)
Get the dimensions of a matrix
mat.ndim
Get the number of elements in a matrix
mat.size
Get the size of the matrix - 4X6
mat.shape
Just get the number of rows of a matrix
mat.shape[0]
Just get the number of columns of a matrix
mat.shape[1]
Get the first column of a matrix
mat[: , 0]
Get the first and third columns of a matrix
mat[: , [0,2]]
Get the one to third columns of a matrix
mat[: , 0:3]
Repeat 1st column twice in a matrix
mat[:, [0,0]]
Get the first row of a matrix
mat[0, :]
Get the first and third rows of a matrix
mat[[0,2], :]
Get 1 to third rows in a matrix
mat[0:3 , :]
Repeat 1st row twice in a matrix
mat[[0,0], :]
Repeat 1st row n times in a matrix
mat[[0] * n , :]
Create a zero vector of size 4
np.zeros(4) # 4 row vector
convert 3X1 matrix to 3x1 vector
- mat = np.random.randn(3,1)
- arr = np.asarray(mat) # 2D array
- vector = np.squeeze(arr) # 1d array