Technical Notes On Using
Data Science & Artificial Intelligence
To Fight For Something That Matters
Author: Chris Albon
Machine Learning
Vectors, Matrices, And Arrays
Transpose A Vector Or Matrix
Preliminaries
# Load library import numpy as np
Create Vector
# Create vector vector = np . array ([ , , , , , ])
Create Matrix
# Create matrix matrix = np . array ([[ , , ], [ , , ], [ , , ]])
Transpose Vector
# Tranpose vector vector . T
array([1, 2, 3, 4, 5, 6])
Transpose Matrix
# Transpose matrix matrix . T
array([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
Selecting Elements In An Array
Preliminaries
# Load library import numpy as np
Create Vector
# Create row vector vector = np . array ([ , , , , , ])
Select Element
# Select second element vector [ ]
2
Create Matrix
# Create matrix matrix = np . array ([[ , , ], [ , , ], [ , , ]])
Select Element
# Select second row, second column matrix [ , ]
5
Create Tensor
# Create matrix tensor = np . array ([ [[[ , ], [ , ]], [[ , ], [ , ]]], [[[ , ], [ , ]], [[ , ], [ , ]]] ])
Select Element
# Select second element of each of the three dimensions tensor [ , , ]
array([4, 4])
Reshape An Array
Preliminaries
# Load library import numpy as np
Create Array
# Create a 4x3 matrix matrix = np . array ([[ , , ], [ , , ], [ , , ], [ , , ]])
Reshape Array
# Reshape matrix into 2x6 matrix matrix . reshape ( , )
array([[ 1, 2, 3, 4, 5, 6], [ 7, 8, 9, 10, 11, 12]])
Invert A Matrix
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , ], [ , ]])
Invert Matrix
# Calculate inverse of matrix np . linalg . inv ( matrix )
array([[-1.66666667, 1.33333333], [ 0.66666667, -0.33333333]])
Getting The Diagonal Of A Matrix
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , , ], [ , , ], [ , , ]])
Get The Diagonal
# Return diagonal elements matrix . diagonal ()
array([1, 5, 9])
Calculate The Trace
# Calculate the tracre of the matrix matrix . diagonal () . sum ()
15
Flatten A Matrix
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , , ], [ , , ], [ , , ]])
Flatten Matrix
# Flatten matrix matrix . flatten ()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Find The Rank Of A Matrix
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , , ], [ , , ], [ , , ]])
Find Rank Of Matrix
# Return matrix rank np . linalg . matrix_rank ( matrix )
2
Find The Maximum And Minimum
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , , ], [ , , ], [ , , ]])
Find Maximum Element
# Return maximum element np . max ( matrix )
9
Find Minimum Element
# Return minimum element np . min ( matrix )
1
Find Maximum Element By Column
# Find the maximum element in each column np . max ( matrix , axis = )
array([7, 8, 9])
Find Maximum Element By Row
# Find the maximum element in each row np . max ( matrix , axis = )
array([3, 6, 9])
Describe An Array
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , , , ], [ , , , ], [ , , , ]])
View Shape
# View number of rows and columns matrix . shape
(3, 4)
View Total Elements
# View number of elements (rows * columns) matrix . size
12
View Number Of Dimensions
# View number of dimensions matrix . ndim
2
Create A Vector
Preliminaries
# Load library import numpy as np
Create Row Vector
# Create a vector as a row vector_row = np . array ([ , , ])
Create Column Vector
# Create a vector as a column vector_column = np . array ([[ ], [ ], [ ]])
Create A Sparse Matrix
Preliminaries
# Load libraries import numpy as np from scipy import sparse
Create Dense Matrix
# Create a matrix matrix = np . array ([[ , ], [ , ], [ , ]])
Convert To Sparse Matrix
# Create compressed sparse row (CSR) matrix matrix_sparse = sparse . csr_matrix ( matrix )
Note: There are many types of sparse matrices. In the example above we use CSR but the type we use should reflect our use case.
Create A Matrix
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , ], [ , ]])
Note NumPys mat
data structure is less flexible for our purposes and should be avoided.
Converting A Dictionary Into A Matrix
Preliminaries
# Load library from sklearn.feature_extraction import DictVectorizer
Create Dictionary
# Our dictionary of data data_dict = [{ 'Red' : , 'Blue' : }, { 'Red' : , 'Blue' : }, { 'Red' : , 'Yellow' : }, { 'Red' : , 'Yellow' : }]
Feature Matrix From Dictionary
# Create DictVectorizer object dictvectorizer = DictVectorizer ( sparse = False ) # Convert dictionary into feature matrix features = dictvectorizer . fit_transform ( data_dict ) # View feature matrix features
array([[ 4., 2., 0.], [ 3., 4., 0.], [ 0., 1., 2.], [ 0., 2., 2.]])
View column names
# View feature matrix column names dictvectorizer . get_feature_names ()
['Blue', 'Red', 'Yellow']
Calculate The Trace Of A Matrix
Preliminaries
# Load library import numpy as np
Create Matrix
# Create matrix matrix = np . array ([[ , , ], [ , , ], [ , , ]])