import numpy as np
# 合并
A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A,B))
# 上下合并
print(A.shape)
print(C)
print(C.shape)
print('******************************')
# 左右合并
D = np.hstack((A,B))
print(D)
print(D.shape)
print('******************************')
# 改变维度
print(A[:,np.newaxis])
print('******************************')
E = np.concatenate((A,B,B,A),axis=0)
print(E)
结果:
(3,)
[[1 1 1]
[2 2 2]]
(2, 3)
[1 1 1 2 2 2]
(6,)
[[1]
[1]
[1]]
[1 1 1 2 2 2 2 2 2 1 1 1]
本文通过具体示例介绍了如何使用Python的NumPy库进行数组的合并与维度变化操作,包括了上下合并(vstack)、左右合并(hstack)及concatenate函数的应用,并展示了改变数组维度的方法。
4452

被折叠的 条评论
为什么被折叠?



