Data science and artificial intelligence technology note I, vectors, matrices and arrays

Keywords: Lambda

I. vector, matrix and array

Author: Chris Albon

Translator: Flying dragon

Agreement: CC BY-NC-SA 4.0

Transpose matrix or vector

# Loading Library
import numpy as np

# Create vector
vector = np.array([1, 2, 3, 4, 5, 6])

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Transpose vector
vector.T

# array([1, 2, 3, 4, 5, 6]) 

# Transpose matrix
matrix.T

'''
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]]) 
'''

Select elements in array

# Loading Library
import numpy as np

# Create row vector
vector = np.array([1, 2, 3, 4, 5, 6])

# Select the second element
vector[1]

# 2 

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Select second row, second column
matrix[1,1]

# 5 

# Create matrix
tensor = np.array([
                    [[[1, 1], [1, 1]], [[2, 2], [2, 2]]],
                    [[[3, 3], [3, 3]], [[4, 4], [4, 4]]]
                  ])

# Select the second element of each of the three dimensions
tensor[1,1,1]

# array([4, 4]) 

Array distortion

# Loading Library
import numpy as np

# Create 4x3 matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9],
                   [10, 11, 12]])

# Transform matrix into 2x6 matrix
matrix.reshape(2, 6)

'''
array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12]]) 
'''

Inverse of matrix

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 4],
                   [2, 5]])

# Inverse of computational matrix
np.linalg.inv(matrix)

'''
array([[-1.66666667,  1.33333333],
       [ 0.66666667, -0.33333333]]) 
'''

Get matrix diagonal

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Return diagonal element
matrix.diagonal()

# array([1, 5, 9]) 

# Trace to create matrix
matrix.diagonal().sum()

# 15 

Expansion matrix

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Expansion matrix
matrix.flatten()

# array([1, 2, 3, 4, 5, 6, 7, 8, 9]) 

Finding the rank of matrix

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Return the rank of matrix
np.linalg.matrix_rank(matrix)

# 2 

Find The Maximum And Minimum

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Return maximum element
np.max(matrix)

# 9 

# Return minimum element
np.min(matrix)

# 1 

# Find the largest element in each column
np.max(matrix, axis=0)

# array([7, 8, 9]) 

# Find the largest element in each row
np.max(matrix, axis=1)

# array([3, 6, 9]) 

Description array

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12]])

# View number of rows and columns
matrix.shape

# (3, 4) 

# Number of viewing elements (row multiply column)
matrix.size

# 12 

# Viewing dimension
matrix.ndim

# 2 

Create vector

# Loading Library
import numpy as np

# Create row vector
vector_row = np.array([1, 2, 3])

# Create column vector
vector_column = np.array([[1],
                          [2],
                          [3]])

Create sparse matrix

# Load libraries
import numpy as np
from scipy import sparse

# Create matrix
matrix = np.array([[0, 0],
                   [0, 1],
                   [3, 0]])

# Create a compressed sparse row (CSR) matrix
matrix_sparse = sparse.csr_matrix(matrix)

Note: there are many types of sparse matrices. In the above example, we use CSR, but the types we use should reflect our use cases.

Create matrix

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 4],
                   [2, 5]])

Note that NumPy's mat data structure is not very flexible for our purposes and should be avoided.

Convert dictionary to matrix

# Loading Library
from sklearn.feature_extraction import DictVectorizer

# Our data dictionary
data_dict = [{'Red': 2, 'Blue': 4},
             {'Red': 4, 'Blue': 3},
             {'Red': 1, 'Yellow': 2},
             {'Red': 2, 'Yellow': 2}]

# Create a DictVectorizer object
dictvectorizer = DictVectorizer(sparse=False)

# Convert dictionary to 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.]]) 
'''

# To view the column names of a characteristic matrix
dictvectorizer.get_feature_names()

# ['Blue', 'Red', 'Yellow'] 

Trace of calculation matrix

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Trace of calculation matrix
matrix.diagonal().sum()

# 15 

Determinant of calculation matrix

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Return the determinant of matrix
np.linalg.det(matrix)

# -9.5161973539299405e-16 

Calculate mean, variance and standard deviation

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Return mean
np.mean(matrix)

# 5.0 

# Return variance
np.var(matrix)

# 6.666666666666667 

# Return standard deviation
np.std(matrix)

# 2.5819888974716112 

Calculate the dot product of two vectors

# Loading Library
import numpy as np

# Create two vectors
vector_a = np.array([1,2,3])
vector_b = np.array([4,5,6])

# Computed dot product
np.dot(vector_a, vector_b)

# 32 

# Computed dot product
vector_a @ vector_b

# 32 

Apply action to element

# Loading Library
import numpy as np

# Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Create a function with 100
add_100 = lambda i: i + 100

# Create a vectorized function
vectorized_add_100 = np.vectorize(add_100)

# Apply functions to all elements of a matrix
vectorized_add_100(matrix)

'''
array([[101, 102, 103],
       [104, 105, 106],
       [107, 108, 109]]) 
'''

Addition and subtraction of matrix

# Loading Library
import numpy as np

# Create matrix
matrix_a = np.array([[1, 1, 1],
                     [1, 1, 1],
                     [1, 1, 2]])

# Create matrix
matrix_b = np.array([[1, 3, 1],
                     [1, 3, 1],
                     [1, 3, 8]])

# Add two matrices
np.add(matrix_a, matrix_b)

'''
array([[ 2,  4,  2],
       [ 2,  4,  2],
       [ 2,  4, 10]]) 
'''

# Subtract two matrices
np.subtract(matrix_a, matrix_b)

'''
array([[ 0, -2,  0],
       [ 0, -2,  0],
       [ 0, -2, -6]]) 
'''

Posted by sports on Sun, 08 Dec 2019 03:19:25 -0800