使用numpy读取txt文件
#导入numpy库以np作为其别名
import numpy as np
#使用numpy读取txt文件,文件在同一文件夹下使用相对路径,否则使用绝对路径,delimiter = ‘,’以中文逗号分隔,
#若txt文件中既有中文逗号又有英文逗号则分隔结果不同,dtype是数据类型,可以使用help函数直接查看官方文档
world_alcohol = np.genfromtxt('world_alcohol.txt',delimiter = ',',dtype = str)
print(world_alcohol)
print(help(np.genfromtxt))
['Year,WHO region,Country,Beverage Types,Display Value'
'1986,Western Pacific,Viet Nam,Wine,0' '1986,Americas,Uruguay,Other,0.5'
"1985,Africa,Cte d'Ivoire,Wine,1.62" '1986,Americas,Colombia,Beer,4.27'
.....
'1989,Americas,Bahamas,Wine,1.5' '1985,Africa,Malawi,Spirits,0.31']
创建数组、向量或者矩阵
#创建一维数组 在数学中也叫作向量
vector = np.array([1,2])
print(vector,type(vector),vector.shape)
# shape返回变量的结构 若a是一维数组直接返回它的个数 也可以使用reshape重新修改其结构
print(vector.reshape(2,1))
[1 2] (2,)
[[1]
[2]]
创建二维数组 在数学中也叫作矩阵
matrix = np.array([[1,2],[3,4]])
print(matrix,type(matrix),matrix.shape,matrix.dtype)
[[1 2]
[3 4]] (2, 2) int32
注意在创建矩阵时数据类型要保持一致,如果不一致则自动转换为高精度类型数据
创建二维数组 在数学中也叫作矩阵
matrix = np.array([[1,2.0],[3,4]])
print(matrix,type(matrix),matrix.shape,matrix.dtype)
[[1. 2.]
[3. 4.]] (2, 2) float64
numpy的索引取值和python的索引取值一样
一维索引取值
vector = np.array([1,2])
print(vector[1])
2
二维索引取值
matrix = np.array([[1,2],[3,4]])
print(matrix[1,1])
4
还可以判断变量中的某个元素是否与某一值相等,返回布尔值,进而可以通过布尔值索引,可以查看变量中某个值的个数
vector = np.array([1,2])
equal_to_2 = (vector == 2)
print(equal_to_2)
print(vector[equal_to_2])
[False True]
[2]
matrix = np.array([[1,2],[3,3]])
equal_to_3 = (matrix == 3)
print(equal_to_3)
print(matrix[equal_to_3])
[[False False]
[ True True]]
[3 3]
有时候在计算中需要更改数据类型
matrix = np.array([[1,2],[3,3]])
print(matrix,matrix.dtype)
matrix = matrix.astype(np.float16)
print(matrix,matrix.dtype)
[[1 2]
[3 3]] int32
[[1. 2.]
[3. 3.]] float16
求最值
vector = np.array([5,10,15,20])
print(np.max(vector),np.min(vector))
20 5
使用argmax查找最值
import numpy as np
a = np.sin(np.arange(20)).reshape(5,4)
print(a,a.shape)
b =a.argmax(axis = 0)
print(b,type(b))
print(a[b,range(0,4)])
[[ 0. 0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.28790332 -0.96139749 -0.75098725 0.14987721]] (5, 4)
[2 0 3 1]
[0.98935825 0.84147098 0.99060736 0.6569866 ]
使用max求最值
matrix = np.array([[1,2],[3,4]])
#axis 这一参数决定了求最值的方式,axis=1是求行,axis=0是求列
print(np.max(matrix,axis = 1),np.max(matrix,axis = 0))
[2 4] [3 4]
求和 sum
vector = np.array([5,10,15,20])
print(np.sum(vector))
matrix = np.array([[1,2],[3,4]])
#axis 这一参数决定了求最值的方式,axis=1是求行,axis=0是求列
print(np.sum(matrix,axis = 1),np.sum(matrix,axis = 0))
50
[3 7] [4 6]
使用arange函数快速生成矩阵或者向量 arange函数(开始,结束,步长)
vector = np.arange(5,20,5)
print(vector,type(vector))
[ 5 10 15]
matrix = np.arange(5,45,5).reshape(2,4)
print(matrix)
[[ 5 10 15 20]
[25 30 35 40]]
使用linspace快速生成矩阵或者向量 linspace默认生成50个数据,线性生成,linspace(开始,结束,num数据个数,endpoint决定是否包含最后一个数)
vector = np.linspace(1,10)
print(vector)
vector = np.linspace(1,5,num=10,endpoint= False)
print(vector)
matrix = vector.reshape(2,5)
print(matrix)
[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735
2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816
3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898
4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898
5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061
6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143
7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224
8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306
9.81632653 10. ]
[1. 1.4 1.8 2.2 2.6 3. 3.4 3.8 4.2 4.6]
[[1. 1.4 1.8 2.2 2.6]
[3. 3.4 3.8 4.2 4.6]]
随机生成矩阵或者向量 都是0-1之间的数字
vector = np.random.random(10)
print(vector)
matrix = np.random.random([2,3])
print(matrix)
[0.9911898 0.43842508 0.87541001 0.44869673 0.59948623 0.77041488
0.76872552 0.82115081 0.56340336 0.12221817]
[[0.41960949 0.66598529 0.42944425]
[0.26393659 0.22753426 0.08828196]]
矩阵或者向量的数学运算
对应位置加减、乘和向量乘法
vector_a = np.array([1,2,3])
vector_b = np.array([4,5,6])
#对应位置加,减
print(vector_a + vector_b,vector_b-vector_a)
#乘法 对应位置
print(vector_a * vector_b)
#向量乘法
print(vector_a.dot(vector_b))
[5 7 9] [3 3 3]
[ 4 10 18]
32
matrix_a = np.array([[1,1],[0,1]])
matrix_b = np.array([[2,0],[3,4]])
#对应位置加
print(matrix_a + matrix_b)
#对应位置减
print(matrix_b - matrix_a)
#对应位置乘
print(matrix_a * matrix_b)
#矩阵乘法
print(matrix_a.dot(matrix_b))
[[3 1]
[3 5]]
[[ 1 -1]
[ 3 3]]
[[2 0]
[0 4]]
[[5 4]
[3 4]]
求指数幂和开方
vector = np.arange(0,5,1)
print(np.exp(vector))
print(np.power(vector,0.5))
print(np.sqrt(vector))
[ 1. 2.71828183 7.3890561 20.08553692 54.59815003]
[0. 1. 1.41421356 1.73205081 2. ]
[0. 1. 1.41421356 1.73205081 2. ]
矩阵降维
#生成二维矩阵
matrix = np.arange(5,45,5).reshape(4,2)
print(matrix)
print(matrix.ravel())
#二维降成一维
[[ 5 10]
[15 20]
[25 30]
[35 40]]
[ 5 10 15 20 25 30 35 40]
使用ravel和flatten降维的区别
#生成三维数据 ravel():如果没有必要,不会产生源数据的副本 flatten():返回源数据的副本
matrix = np.array([[1,2,3],[4,5,6]])
print(matrix)
matrix.flatten()[1] = 88
#flatten() 返回源数据的副本,即拷贝,修改元素值,源数据不会改变
print(matrix)
print(matrix)
matrix.ravel()[1] = 88
#ravel 没有产生副本 修改元素值,源数据也会改变
print(matrix)
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
[[ 1 88 3]
[ 4 5 6]]
矩阵拼接
matrix_a = np.array([1,2,3,4])
matrix_b = np.array([5,6,7,8])
#按照列拼接
print(np.vstack((matrix_a,matrix_b)))
#按照行拼接
print(np.hstack((matrix_a,matrix_b)))
[[1 2 3 4]
[5 6 7 8]]
[1 2 3 4 5 6 7 8]
向量和矩阵切割
vector = np.arange(0,10)
print(vector)
print(np.split(vector,(3,4)))
[0 1 2 3 4 5 6 7 8 9]
[array([0, 1, 2]), array([3]), array([4, 5, 6, 7, 8, 9])]
matrix = np.arange(5,45,5).reshape(4,2)
print(matrix)
#按列切割
print(np.hsplit(matrix,(0,1)))
#按行切割
print(np.vsplit(matrix,(0,1)))
[[ 5 10]
[15 20]
[25 30]
[35 40]]
[array([], shape=(4, 0), dtype=int32), array([[ 5],
[15],
[25],
[35]]), array([[10],
[20],
[30],
[40]])]
[array([], shape=(0, 2), dtype=int32), array([[ 5, 10]]), array([[15, 20],
[25, 30],
[35, 40]])]
扩展向量或者矩阵
vector = np.arange(0,10)
print(vector)
print(np.tile(vector,(2,3)))
[0 1 2 3 4 5 6 7 8 9]
[[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]]
matrix = np.tile(vector,(2,2))
print(matrix)
[[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]]
快速生成0矩阵和全1矩阵
zero_matrix = np.zeros((3,2))
print(zero_matrix)
#如有需要可以使用astype方法重新设置数据类型
zero_matrix = zero_matrix.astype(int)
print(zero_matrix)
[[0. 0.]
[0. 0.]
[0. 0.]]
[[0 0]
[0 0]
[0 0]]
快速生成n * n的单位矩阵
identy_matrix = np.identity(4)
print(identy_matrix)
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
快速生成对角矩阵 默认k = 0表示主对角线,主对角线上的对角线k为正,主对角线下的对角线k为负
diag_matrix = np.diag([1,2,3,4])
print(diag_matrix)
diag_matrix = np.diag([1,2,3,4],k = 1)
print(diag_matrix)
diag_matrix = np.diag([1,2,3,4],k = -1)
print(diag_matrix)
[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
[[0 1 0 0 0]
[0 0 2 0 0]
[0 0 0 3 0]
[0 0 0 0 4]
[0 0 0 0 0]]
[[0 0 0 0 0]
[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]]
如果传入的是二维矩阵则返回对角线的值
#k = 0是主对角线大于0是主对角线上方的对角线小于0是下方的
diag_matrix = np.diag([[1,2,3],[3,4,5],[4,5,6]],k = 0)
print(diag_matrix)
diag_matrix = np.diag([[1,2,3],[3,4,5],[4,5,6]],k = 1)
print(diag_matrix)
[1 4 6]
[2 5]
矩阵转置
matrix = np.arange(0,9).reshape(3,3)
print(matrix)
print(matrix.T)
[[0 1 2]
[3 4 5]
[6 7 8]]
[[0 3 6]
[1 4 7]
[2 5 8]]
矩阵求逆
matrix = np.array([[1,2],[-1,-3]])
print(np.linalg.inv(matrix))
[[ 3. 2.]
[-1. -1.]]
求矩阵的解 A x = b Ax=b Ax=b
matrix_A = np.array([[0.001,2.,3.],[-1.,3.712,4.623],[-2.,1.072,5.643]])
print(matrix_A.shape)
matrix_b = np.array([[1.0],[2.],[3.]])
print(matrix_b.shape)
print(np.linalg.solve(matrix_A,matrix_b))
(3, 3)
(3, 1)
[[-0.49039646]
[-0.05103518]
[ 0.36752025]]
求解矩阵的特征值特征向量
matrix = np.array([[1,-3,3],[3,-5,3],[6,-6,4]])
eigenvalue,eigenvector = np.linalg.eig(matrix)
print(eigenvalue)
print(eigenvector)
[ 4.+0.00000000e+00j -2.+1.10465796e-15j -2.-1.10465796e-15j]
[[-0.40824829+0.j 0.24400118-0.40702229j 0.24400118+0.40702229j]
[-0.40824829+0.j -0.41621909-0.40702229j -0.41621909+0.40702229j]
[-0.81649658+0.j -0.66022027+0.j -0.66022027-0.j ]]