比较简单,直接就能看懂,不做赘述
1:reshape 维度变换
import numpy as np
def Reshape():
""" 维度变换 """
nList = np.arange(24)
print(f'原数据:\n{nList}\n')
print(f'转化成4*6的矩阵:\n{nList.reshape(4, 6)}\n')
print(f'转化成 6*自动生成的相应的shape值(此时为4) 的矩阵:\n{nList.reshape(6, -1)}\n')
Reshape()
输出:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
2:diag 对角线操作
def Diag():
""" 对角线操作 """
nList = np.arange(4, 32).reshape(4, -1)
print(f'原数据:\n{nList}\n')
print(f'获取对角线:\n{np.diag(nList)}\n')
print(f'获取对角线(向上偏移1位):\n{np.diag(nList, 1)}\n')
nList = 1 + np.arange(5)
print(f'以nList为对角线(向上偏移1位 )创建一个矩阵:\n{np.diag(nList, 1)}\n')
print(f'以nList为对角线(向下偏移2位 )创建一个矩阵:\n{np.diag(nList, -2)}')
Diag()
输出:
[[ 4 5 6 7 8 9 10]
[11 12 13 14 15 16 17]
[18 19 20 21 22 23 24]
[25 26 27 28 29 30 31]]
[ 4 12 20 28]
[ 5 13 21 29]
[[0 1 0 0 0 0]
[0 0 2 0 0 0]
[0 0 0 3 0 0]
[0 0 0 0 4 0]
[0 0 0 0 0 5]
[0 0 0 0 0 0]]
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[1 0 0 0 0 0 0]
[0 2 0 0 0 0 0]
[0 0 3 0 0 0 0]
[0 0 0 4 0 0 0]
[0 0 0 0 5 0 0]]
3:linspace 等差数列
def LinSpace():
""" 等差数列 """
print(f'创建一个等差数列(值:0-10, 个数:5, 显示间距):\n{np.linspace(0, 10, 5, retstep=True)}')
LinSpace()
输出:
(array([ 0. , 2.5, 5. , 7.5, 10. ]), 2.5)
4:logspace 等比数列
def LogSpace():
""" 等比数列 """
print(f'创建一个等比数列(值:2^1-2^10, 个数:20):\n{np.logspace(1, 10, num=20, base=2)}')
LogSpace()
输出:
[ 2. 2.77730229 3.85670399 5.3556164 7.43708284
10.32751358 14.34131354 19.91508143 27.65510059 38.40328703
53.32876841 74.05505519 102.836637 142.80421347 198.30523421
275.37679007 382.40229418 531.02338276 737.40622722 1024. ]
5:ravel 降维
def Ravel():
""" 降维 """
print(np.ones((2, 3, 4), dtype=int).ravel())
Ravel()
输出:
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
6:vstack和hstack 纵横合并
def VHStack():
nList = np.arange(12).reshape(3, -1)
print(f'nList:\n{nList}')
nList2 = np.arange(5, 17).reshape(3, -1)
print(f'nList2:\n{nList2}\n')
print(f'纵向合并:\n{np.vstack([nList, nList2])}')
print(f'横向合并:\n{np.hstack([nList, nList2])}')
VHStack()
输出:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
[[ 0 1 2 3 5 6 7 8]
[ 4 5 6 7 9 10 11 12]
[ 8 9 10 11 13 14 15 16]]
最后附一个参考链接:https://www.jianshu.com/p/ecd3a42edd91