模块导入
import numpy as np
生成ndarray的几种方式
# 使用arrange转换
print(np.arange(0,10,2))
[0 2 4 6 8]
# 列表转换
list1 = [3.14,2.17,0,1,2]
nd1 = np.array(list1)
print(nd1)
print(type(nd1))
[3.14 2.17 0. 1. 2. ]
<class 'numpy.ndarray'>
# 嵌套类表转换多维ndarray
list2 = [[3.14,2.17,0,1,2],[1,2,3,4,5]]
nd2 = np.array(list2)
print(nd2)
print(type(nd2))
[[3.14 2.17 0. 1. 2. ]
[1. 2. 3. 4. 5. ]]
<class 'numpy.ndarray'>
# 使用ransom模块生成ndarray
np.random.seed(123)
nd3 = np.random.randn(2,3)
print(nd3)
print('-----shuffer-----')
np.random.shuffle(nd3)
print(nd3)
[[-1.0856306 0.99734545 0.2829785 ]
[-1.50629471 -0.57860025 1.65143654]]
-----shuffer-----
[[-1.50629471 -0.57860025 1.65143654]
[-1.0856306 0.99734545 0.2829785 ]]
# 创建特定形状的多维数组
nd4 = np.zeros([2,3])
nd5 = np.ones([3,2])
nd6 = np.eye(3) # 单位矩阵
nd7 = np.diag([1,2,3])
print(nd4)
print(nd5)
print(nd6)
print(nd7)
[[0. 0. 0.]
[0. 0. 0.]]
[[1. 1.]
[1. 1.]
[1. 1.]]
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[1 0 0]
[0 2 0]
[0 0 3]]
数据保存
nd8 = np.random.random([2,3])
np.savetxt('nd8.txt',nd8)
np.save('nd8.npy',nd8)
np.savez('nd8.npz',key_word=nd8)
nd9 = np.loadtxt('nd8.txt')
nd10 = np.load('nd8.npy')
nd11 = np.load('nd8.npz')['key_word']
print(nd9)
print(nd10)
print(nd11)
[[0.41092437 0.5796943 0.13995076]
[0.40101756 0.62731701 0.32415089]]
[[0.41092437 0.5796943 0.13995076]
[0.40101756 0.62731701 0.32415089]]
[[0.41092437 0.5796943 0.13995076]
[0.40101756 0.62731701 0.32415089]]
存取元素
# 一维
nd12 = np.arange(10)
print('nd12',nd12)
# 截取固定间隔数据 间隔为3
print(nd12[0:5:3])
# 倒序取数 最后一位绝对值为步长 +表示正序 -表示逆序
print(nd12[::-1])
nd12 [0 1 2 3 4 5 6 7 8 9]
[0 3]
[9 8 7 6 5 4 3 2 1 0]
# 多维
nd13 = np.arange(25).reshape([5,5])
print(nd13)
# 截取多维数组中的值域
nd13[(nd13>5) &(nd13<10)]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
array([6, 7, 8, 9])
random.choice 从指定样本中随机抽取数据
from numpy import random as nr
a = np.arange(1,25,dtype=float)
print(a)
c1 = nr.choice(a,size=(3,4)) # 随机可重复抽取
c2 = nr.choice(a,size=(3,4),replace=False) # 随机不可重复抽取
c3 = nr.choice(a,size=(3,4),p=a/np.sum(a)) # p表示被抽取的概率
print(c1)
print(c2)
print(c3)
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
- 24.]
[[21. 8. 12. 23.]
[ 8. 2. 12. 6.]
[24. 19. 21. 18.]]
[[16. 23. 1. 5.]
[22. 11. 3. 14.]
[ 8. 6. 15. 4.]]
[[22. 7. 21. 12.]
[11. 19. 8. 23.]
[19. 21. 3. 19.]]
- 24.]
矩阵操作
nd14 = np.arange(1,10).reshape([3,3])
# 矩阵转置
trans = np.transpose(nd14)
print(trans)
# 矩阵乘法
a = np.arange(12).reshape([3,4])
b = np.arange(12).reshape([4,3])
mat = a.dot(b)
print(mat)
# 矩阵的迹 矩阵对角线元素的和
print(a.trace())
# 计算矩阵的行列式
print(np.linalg.det(nd14))
# 计算矩阵的逆矩阵
print(np.linalg.solve(np.random.random([3,3]),np.eye(3)))
[[1 4 7]
[2 5 8]
[3 6 9]]
[[ 42 48 54]
[114 136 158]
[186 224 262]]
15
0.0
[[ -2.04392157 6.94469078 -4.85622941]
[ 20.617819 -34.13873869 22.12333169]
[ -7.44347078 10.19125571 -5.31023435]]
数据的合并和展平
a = np.arange(4).reshape(2,2)
b = np.arange(4).reshape(2,2)
c = np.append(a,b,axis=0)
print(c.shape)
d = c = np.append(a,b,axis=1)
print(d.shape)
e = np.concatenate([a,b],axis=1)
print(e.shape)
(4, 2)
(2, 4)
(2, 4)
nd15 = np.arange(12).reshape(3,4)
print(nd15)
# 按列优先展平
print(nd15.ravel('F'))
# 按行优先展平
print(nd15.ravel())
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[ 0 4 8 1 5 9 2 6 10 3 7 11]
[ 0 1 2 3 4 5 6 7 8 9 10 11]
广播机制 方便不同shape的数组运算
a = np.arange(10)
b = np.arange(10)
print(a+b)
print(a+3)
# 多维数组之间运算
c = np.arange(10).reshape([5,2])
d = np.arange(5).reshape([5,1]) # 补齐操作 只要有一个维度相等
print(c+d)
[ 0 2 4 6 8 10 12 14 16 18]
[ 3 4 5 6 7 8 9 10 11 12]
[[ 0 1]
[ 3 4]
[ 6 7]
[ 9 10]
[12 13]]
矩阵操作
# 范数计算
x = np.arange(4)
print(np.linalg.norm(x,1)) # 第一范数
print(np.linalg.norm(x,2)) # 第二范数
print(np.linalg.norm(x,np.inf)) # 无穷范数 每个向量中每个元素的绝对值的最大值
6.0
3.7416573867739413
3.0
# 特征分解
a =np.array([[1,2],[3,4]])
a1 = np.linalg.eigvals(a) # f返回特征值
a2,b = np.linalg.eig(a) # 返回特征值和特征向量
print(a1)
print(a2)
print(b)
[-0.37228132 5.37228132]
[-0.37228132 5.37228132]
[[-0.82456484 -0.41597356]
[ 0.56576746 -0.90937671]]
# 奇异值分解
# A = UDV U:m*m D:m*n V:n*n
# UV都为正交矩阵:所有行向量之间正交、且范数为1
a = np.mat([[1,1,1,0,0],
[2,2,2,0,0],
[3,3,3,0,0],
[5,5,3,2,2],
[0,0,0,3,3],
[0,0,0,6,6]])
u,sigma,vt = np.linalg.svd(a)
print(sigma)
[1.09824632e+01 8.79229347e+00 1.03974857e+00 1.41050174e-15
1.80635171e-32]