Matrices and Matrix Operations
Glossary
A matrix — a rectangular numeric table or two-dimensional array consisting of m rows and n columns
A transpose of the matrix — a matrix operation when its rows become columns with the same numbers
Practice
1# A matrix from a list of lists23import numpy as np45matrix = np.array([6 [1, 2, 3],7 [4, 5, 6],8 [7, 8, 9]])9print(matrix)
1# A matrix from a list of vectors23import numpy as np45string0 = np.array([1,2,3])6string1 = np.array([-1,-2,-3])7list_of_vectors = [string0, string1]8matrix_from_vectors = np.array(list_of_vectors)910print(matrix_from_vectors)
1# A matrix from data frames23import pandas as pd4import numpy as np56matrix = df.values7print(matrix)89# matrix dimensions10print('Size:', matrix.shape)11# vector is the second row of the matrix12print('Row 2:', matrix[2, :])13# vector is the first row of the matrix14print('Column 1:', matrix[:, 1])
1# Matrix by vector multiplication23import numpy as np45A = np.array([6 [1, 2, 3],7 [4, 5, 6]])89b = np.array([7, 8, 9])1011print(np.dot(A, b))12print(A.dot(b))
1# Matrix by matrix multiplication23import numpy as np45print(A.dot(B))6print(np.dot(A,B))7print(A @ B)
1# Transpose of matrix23print(matrix.T)
1# Creating a class23class ClassName:4 def fit(self, arg1, arg2, ...): # class method5 # method content