python机器学习基础笔记1之数组矩阵(cook book)

创建vector

# Load library
import numpy as np

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

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

创建 matrix

# Load library
import numpy as np

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


创建稀疏矩阵

# Load libraries
import numpy as np
from scipy import sparse

# Create a matrix
matrix = np.array([[0, 0],
				   [0, 1],
				   [3, 0]])
							 
# Create compressed sparse row (CSR) matrix
matrix_sparse = sparse.csr_matrix(matrix)

元素定位

# Load library
import numpy as np

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

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

# Select third element of vector
vector[2]

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

# Select all elements of a vector
vector[:]

# Select everything up to and including the third element
vector[:3]

# Select everything after the third element
vector[3:]

# Select the last element
vector[-1]

# Select the first two rows and all columns of a matrix

matrix[:2,:]

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

# Select all rows and the second column
matrix[:,1:2]
array([[2],
	   [5],
       [8]])

描述矩阵

# View number of rows and columns
matrix.shape

# View number of elements (rows * columns)
matrix.size

# View number of dimensions
matrix.ndim

对元素进行运算

# Add 100 to all elements
matrix + 100
array([[101, 102, 103],
       [104, 105, 106],
       [107, 108, 109]])

最大最小值

# Load 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 maximum element in each column
np.max(matrix, axis=0)
array([7, 8, 9])

# Find maximum element in each row
np.max(matrix, axis=1)
array([3, 6, 9])

平均值,方差

np.mean(matrix)

# Return variance
np.var(matrix)

# Return standard deviation
np.std(matrix)

# Find the mean value in each column
np.mean(matrix, axis=0)
array([ 4., 5., 6.])

重塑array

# Load library
import numpy as np

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

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

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

matrix.size
12

'''
One useful argument in reshape is -1, which effectively means “as many as needed,”
so reshape(-1, 1) means one row and as many columns as needed:
'''
matrix.reshape(1, -1)
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]])

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

转置T

matrix.T

# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
				   [4, 5, 6],
				   [7, 8, 9]])
# Transpose matrix
matrix.T

array([[1, 4, 7],
	   [2, 5, 8],
	   [3, 6, 9]])
# Transpose vector
np.array([1, 2, 3, 4, 5, 6]).T
array([1, 2, 3, 4, 5, 6])

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

flattening matrix

matrix.flatten()

# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
				   [4, 5, 6],
	 			   [7, 8, 9]])
# Flatten matrix
matrix.flatten()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

matrix.reshape(1, -1)
array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])

np.linalg.matrix_rank( matrix )

# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 1, 1],
			 	   [1, 1, 10],
				   [1, 1, 15]])
# Return matrix rank
np.linalg.matrix_rank(matrix)
2

行列式

np.linalg.det(matrix)

# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
	    	       [2, 4, 6],
			       [3, 8, 9]])
# Return determinant of matrix
np.linalg.det(matrix)
0.0

对角矩阵

matrix.diagonal()

# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
				   [2, 4, 6],
			       [3, 8, 9]])
# Return diagonal elements
matrix.diagonal()
array([1, 4, 9])

# Return diagonal one above the main diagonal
matrix.diagonal(offset=1)
array([2, 6])
# Return diagonal one below the main diagonal
matrix.diagonal(offset=-1)
array([2, 8])

迹(trace)

matrix.trace()

# Load library
import numpy as np
# Create matrix
matrix = np.array([[1, 2, 3],
	 			   [2, 4, 6],
				   [3, 8, 9]])
# Return trace
matrix.trace()

# Return diagonal and sum elements
sum(matrix.diagonal())

特针值

eigenvalues, eigenvectors = np.linalg.eig(matrix)

# Load library
import numpy as np

# Create matrix
matrix = np.array([[1, -1, 3],
				   [1, 1, 6],
			       [3, 8, 9]])
			       
# Calculate eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(matrix)

# View eigenvalues
eigenvalues
array([ 13.55075847, 0.74003145, -3.29078992])

# View eigenvectors
eigenvectors
array([[-0.17622017, -0.96677403, -0.53373322],
[-0.435951 , 0.2053623 , -0.64324848],
[-0.88254925, 0.15223105, 0.54896288]])

点乘(dot product)

np.dot(vector_a, vector_b)

# Load library
import numpy as np

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

# Calculate dot product
np.dot(vector_a, vector_b)
32

或者用@
# Calculate dot product
vector_a @ vector_b
32

加减

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

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

# Add two matrices
matrix_a + matrix_b

矩阵乘积

# dort prduct
 Multiply two matrices
matrix_a @ matrix_b


#element-wise multiplication
matrix_a * matrix_b

逆矩阵

np.linalg.inv(matrix)


np.linalg.inv(matrix)

产生随机数值

np.random.seed(0)
np.random.random(3)

# Load library
import numpy as np

# Set seed
np.random.seed(0)

# Generate three random floats between 0.0 and 1.0

np.random.random(3)
array([ 0.5488135 , 0.71518937, 0.60276338])
# Generate three random integers between 1 and 10
np.random.randint(0, 11, 3)

# Draw three numbers from a normal distribution with mean 0.0
# and standard deviation of 1.0
np.random.normal(0.0, 1.0, 3)

# Draw three numbers from a logistic distribution with mean 0.0 and scale of 1.0
np.random.logistic(0.0, 1.0, 3)

# Draw three numbers greater than or equal to 1.0 and less than 2.0
np.random.uniform(1.0, 2.0, 3)

猜你喜欢

转载自blog.csdn.net/weixin_43702920/article/details/95162440