numpy 矩阵对角线_Python中机器学习的Numpy指南

本文是Python中机器学习的Numpy指南,旨在帮助读者快速回顾Numpy库。介绍了如何创建向量、矩阵,特别是创建稀疏矩阵和操作矩阵的对角线,包括计算矩阵的迹和特征值。文章还涵盖了矩阵的转置、行列式、秩、点积等核心操作。
摘要由CSDN通过智能技术生成

f226f824afd54aa1a28633f57ad20f53.png

Python中机器学习的Numpy指南

由于了解Numpy是数据预处理的起点,也是后来实施ML算法的起点,因此您可以成为即将在不久的将来学习机器学习或刚刚开始并希望获得更多实践经验的人ML的Numpy。

因为我们大多数人都倾向于忘记(对于已经实施了ML算法的人而言),各种库函数最终会使用纯粹的逻辑为预先存在的函数编写代码,这在这种情况下既浪费时间又浪费精力,如果人们了解机器学习各个库之间的的细微差别,有效合理的选择库函数解决实际中的编码问题。就变得至关重要。因此,Numpy作为机器学习必不可少的库之一。同样也要有它独有的定位。

热门AI文章

1. 2019年人工智能(AI)的十大趋势

2.打破行话泡沫—深度学习

3.我们如何改善数据质量?

4.使用Python和代码进行逻辑回归的机器学习

本文目的

在撰写本文时,我的主要重点是使它成为Numpy的快速入门,以供那些有使用该库经验但需要快速回顾的人参考。

Numpy库的定位

Numpy是Python编程语言的库,它增加了对大型多维数组和矩阵的支持,以及用于在这些数组上进行操作的大量高级数学函数的集合。

48a87042257235d435521aff264b7cc4.png

核心操作

4.1 创建向量

在这里,我们使用Numpy创建一维数组,然后将其称为向量。

#Load Library import numpy as np
#将向量创建为行
vector_row = np.array([1,2,3])
#将向量创建为列
vector_column = np.array([[1],[2],[ 3]])

4.2 建立矩阵

我们在Numpy中创建一个二维数组,并将其称为矩阵。它包含2行3列。

#Load Libraryimport numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6]])
print(matrix)

4.3 创建稀疏矩阵(Sparse Matrix)

给定的数据具有非常少的非零值,您想要有效地表示它。

机器学习中的一种常见情况是拥有大量数据。但是数据中的大多数元素都是零。例如,假设有一个矩阵,其中的列是Amazon上的所有产品,而行则表示给定的用户是否曾经购买过该商品。就像您可能已经猜到的那样,到现在为止甚至只有一次都没有购买过许多产品,因此绝大多数元素将为零。

稀疏矩阵仅存储非零元素,并假设所有其他值将为零,从而节省了大量计算量。

#Load Libraryimport numpy as np
#Create a Matrix
matrix = np.array([[0,0],[0,1],[3,0]])
print(matrix)

#Create Compressed Sparse Row(CSR) matrix
matrix_sparse = sparse.csr_matrix(matrix)
print(matrix_sparse)

4.4 选择元素

当您需要选择向量或矩阵中的一个或多个元素时

#Load Library
import numpy as np
#Create a vector as a Row
vector_row = np.array([ 1,2,3,4,5,6 ])
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)
#Select 3rd element of Vector
print(vector_row[2])
#Select 2nd row 2nd column
print(matrix[1,1])

#Select all elements of a vector
print(vector_row[:])

#Select everything up to and including the 3rd element
print(vector_row[:3])

#Select the everything after the 3rd element
print(vector_row[3:])

#Select the last element
print(vector[-1])

#Select the first 2 rows and all the columns of the matrix
print(matrix[:2,:])

#Select all rows and the 2nd column of the matrix
print(matrix[:,1:2])

4.5 描述矩阵

当您想了解矩阵的形状大小和尺寸时。

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

#View the Number of Rows and Columns
print(matrix.shape)

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

#View the number of Dimensions(2 in this case)
print(matrix.ndim)

4.6 操作元素

您想将某些函数应用于数组中的多个元素。

Numpy的vectorize类将函数转换为可以应用于数组或数组切片中的多个元素的函数。

#Load Libraryimport numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)
#Create a function that adds 100 to something
add_100 =lambda i: i+100
#Convert it into a vectorized function
vectorized_add_100= np.vectorize(add_100)
#Apply function to all elements in matrix
print(vectorized_add_100(matrix))

4.7 查找最大值和最小值

我们使用Numpy的max和min函数:

#Load Library
import numpy as np
#创建一个矩阵
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#返回max element
print(np.max(matrix))

#返回最小元素
print(np.min(matrix))

#查找每一列中的max元素
print(np.max(matrix,axis = 0))

#查找每行中的最大元素
print(np.max(matrix,axis = 1))

4.8 计算平均值,方差和标准偏差

#Load Library
import numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#Mean
print(np.mean(matrix))

#Standard Dev.
print(np.std(matrix))

#Variance
print(np.var(matrix))

4.9 重塑数组

当您想改变数组的形状(更改行数和列数)而不更改元素时。

#Load Library
import numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#Reshape
print(matrix.reshape(9,1))

#Here -1 says as many columns as needed and 1 row
print(matrix.reshape(1,-1))

#If we provide only 1 value Reshape would return a 1-d array of that length

print(marix.reshape(9))

#We can also use the Flatten method to convert a matrix to 1-d array

print(matrix.flatten())

4.10 矩阵的转置

#Load Library
import numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#Transpose the matrix
print(matrix.T)

4.11 查找矩阵的行列式和秩

矩阵的秩是其行或列所跨越的向量空间的维数。

#Load Library
import numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#Calculate the Determinant
print(np.linalg.det(matrix))

#Calculate the Rank
print(np.linalg.matrix_rank(matrix))

4.12 计算矩阵的对角线

提取矩阵的对角线元素

#Load Library
import numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#Print the Principal diagonal
print(matrix.diagonal())

#Print the diagonal one above the Principal diagonal
print(matrix.diagonal(offset=1))

#Print the diagonal one below Principal diagonal
print(matrix.diagonal(offset=-1))

4.13 计算矩阵的trace

矩阵的迹线是矩阵主对角线上的元素之和。

#Load Library
import numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#Print the Trace
print(matrix.trace())

4.14 查找矩阵的特征值和特征向量

特征向量在机器学习库中被广泛使用。直观地给出由矩阵A表示的线性变换,特征向量是应用该变换时仅在比例上(而不是方向)变化的向量。

Av = Kv

这里A是一个方矩阵,K包含特征值,v包含特征向量。

#Load Libraryimport numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)
# Calculate the Eigenvalues and Eigenvectors of that Matrix
eigenvalues ,eigenvectors=np.linalg.eig(matrix)
print(eigenvalues)
print(eigenvectors)

4.15 计算矩阵的点积

#Load Library
import numpy as np
#Create vector-1
vector_1 = np.array([ 1,2,3 ])

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

#Calculate Dot Product
print(np.dot(vector_1,vector_2))

#Alternatively you can use @ to calculate dot products
print(vector_1 @ vector_2)

4.16 矩阵的四则运算

#Load Library
import numpy as np
#Create Matrix-1
matrix_1 = np.array([[1,2,3],[4,5,6],[7,8,9]])

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

#Add the 2 Matrices
print(np.add(matrix_1,matrix_2))

#Subtraction
print(np.subtract(matrix_1,matrix_2))

#Multiplication(Element wise, not Dot Product)
print(matrix_1*matrix_2)

4.17 求矩阵的逆运算

#Load Library
import numpy as np
#Create a Matrix
matrix = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(matrix)

#Calculate its inverse
print(np.linalg.inv(matrix))

4.18 产生随机数

Numpy提供了多种生成随机数的方法。

此外,有时返回相同的随机数以获得可预测的,可重复的结果可能会很有用。我们可以通过设置伪随机数生成器的“种子”(整数)来实现。具有相同种子的随机过程将始终产生相同的结果。

#Load Library
import numpy as np

#Set seed
np.random.seed(1)

#Generate 3 random integers b/w 1 and 10


print(np.random.randint(0,11,3))

#Draw 3 numbers from a normal distribution with mean 1.0 and std 2.0
print(np.random.normal(1.0,2.0,3))

因此,这几乎涵盖了您使用Python启动机器学习之旅所需的所有标准Numpy操作。对于其他人,我希望这对您在该领域的现有知识有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值