Click to Flip Card
Numpy / Operations
We can apply operations along both rows and columns (axes). Reshaping preserves the data but with different rows and columns. Transpose swaps the indices of columns and rows. Matrices are multiplicated using @ (not *). For matrix element-wise multiplication we use *
Min, Max, Average
We can apply operations along the axes (rows or columns).
""" Operations / Min, Max, Mean
"""
import numpy as np
M = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
print("Max item =", np.max(M))
print("Max item =", np.min(M))
print("Average =", np.mean(M))
print("Max in each row =", np.max(M, axis=1))
print("Min in each row =", np.min(M, axis=1))
print("Average in each row =", np.mean(M, axis=0))
"""
Max item = 9
Max item = 1
Average = 5.0
Max in each row = [3 6 9]
Min in each row = [1 4 7]
Average in each row = [4. 5. 6.]
"""
Reshape
Reshaping maintain the data but as different numbers of rows and columns. The new matrix should have the same size as original matrix. The argument -1 means as many as needed. Flatten transform a matrix into a one-dimensional array.
""" Matrices / Reshape
"""
import numpy as np
M = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
])
A = M.reshape(2, 6)
B = M.reshape(1, -1)
C = M.flatten()
assert M.size == A.size
assert M.size == B.size
assert M.size == C.size
print("Matrix =\n", M)
print("Reshaped (2,6) =\n", A)
print("Reshaped (1,-1) =\n", B)
print("Flatten =\n", C)
"""
Matrix =
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Reshaped (2,6) =
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
Reshaped (1,-1) =
[[ 1 2 3 4 5 6 7 8 9 10 11 12]]
Flatten =
[ 1 2 3 4 5 6 7 8 9 10 11 12]
"""
Transpose
Transposing is a common operation in linear algebra. Indices of column and rows of each element are swapped. A vector cannot be transposed.
""" Matrices / Transpose
"""
import numpy as np
M = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
M_transposed = M.T
assert M[0, 1] == M_transposed[1, 0]
assert M[1, 0] == M_transposed[0, 1]
assert M[1, 1] == M_transposed[1, 1]
print("Matrix =\n", M)
print("Transposed =\n", M_transposed)
"""
Matrix =
[[1 2 3]
[4 5 6]
[7 8 9]]
Transposed =
[[1 4 7]
[2 5 8]
[3 6 9]]
"""
Inverse
We can calculate the inverse of a square matrix. Matrices are multiplicated using @ (not *). The new matrix M_inverse is calculated so that M @ M_inverse = I
""" Matrices / Inverse
"""
import numpy as np
M = np.array([
[4, 3],
[3, 2],
])
M_inverse = np.linalg.inv(M)
I = np.array([
[1, 0],
[0, 1],
])
assert (M @ M_inverse == I).all()
assert (M_inverse @ M == I).all()
print("Matrix =\n", M)
print("Inverse =\n", M_inverse)
print("M @ M_inverse = I: \n", M @ M_inverse)
"""
Matrix =
[[4 3]
[3 2]]
Inverse =
[[-2. 3.]
[ 3. -4.]]
M @ M_inverse = I:
[[1. 0.]
[0. 1.]]
"""
Addition
For addition simply use + operator.
""" Matrices / Addition and Substraction (+ -)
"""
import numpy as np
A = np.array([
[1, 1],
[2, 2],
])
B = np.array([
[1, 1],
[3, 3],
])
C = np.add(A, B) # first method
D = A + B # second method
assert (C == D).all()
print("A =\n", A)
print("B =\n", B)
print("A + B =\n", C)
"""
A =
[[1 1]
[2 2]]
B =
[[1 1]
[3 3]]
A + B =
[[2 2]
[5 5]]
"""
Multiplication
For element-wise multiplication we use *
""" Matrices / Multiplication
"""
import numpy as np
A = np.array([
[1, 1],
[2, 2],
])
B = np.array([
[1, 1],
[3, 3],
])
C = np.dot(A, B) # first method
D = A @ B # second method
E = A * B # element-wise multiplication
assert (C == D).all()
assert (E[1, 1] == 6)
print("A =\n", A)
print("B =\n", B)
print("A @ A =\n", A @ A)
print("A * A =\n", A * A)
"""
A =
[[1 1]
[2 2]]
B =
[[1 1]
[3 3]]
A @ A =
[[3 3]
[6 6]]
A * A =
[[1 1]
[4 4]]
"""
Last update: 54 days ago