
正文
numpy 知识汇总
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1、增加维度
高纬度打印出来很不好观察,所以打印出来shape更加容易理解维度的增加,
此外一维向量a=np.array([1,2,3]), a[:,None],相当于变为二维并转置了shape=(3,1)
b
Out[16]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])b.shape
Out[17]: (3, 3)
b[None,:,:].shape
Out[18]: (1, 3, 3)
b[:,None,:].shape
Out[19]: (3, 1, 3)
b[:,:,None].shape
Out[20]: (3, 3, 1)
2、维度叠加np.stack()中axis参数的理解
假设A,B,C的shape = (3,3)
axis = 0 将(3x3)的矩阵看为整体单位,从上往下堆
axis = 1 将A、B、C中的行作为整体单位,从上往下堆
axis = 2 将A、B、C中的单个数作为整体,从左往右堆A = np. array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
B = np. array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
C = np. array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.stack([A,B,C], axis=0)
np.stack([A,B,C], axis=1)
np.stack([A,B,C], axis=2)
opencv的图像拼接
import cv2
import numpy as npdata1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])hdata = cv2.hconcat([data1,data1,data1])#水平方向拼接
vdata=cv2.vconcat([data1,data1,data1])#垂直方向拼接
print(hdata)
print("\n")
print(vdata)

3 四舍五入
np.array([1.1,2.2]).round()






