本文主要是介绍NumPy(六):数组堆叠:【vstack:垂直(按列顺序)堆叠数组】【hstack:水平(按列顺序)堆叠数组】【stack:axis=0/1/2】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先生成一些数,
import numpy as np
a = np.arange(1, 7).reshape((2, 3))
b = np.arange(7, 13).reshape((2, 3))
c = np.arange(13, 19).reshape((2, 3))print('a = \n', a)
print('b = \n', b)
print('c = \n', c)
即下面的形式
下面分别以不同的形式输出:
一、vstack
s = np.vstack((a, b, c))
print('vstack \n ', s.shape, '\n', s)
在竖直方向上进行堆叠,得到6×3的矩阵
vstack(tup):垂直(按列顺序)堆叠数组:
import numpy as np# 数组堆叠# numpy.vstack(tup):垂直(按列顺序)堆叠数组
a = np.arange(5)
b = np.arange(5, 10)
ar1 = np.vstack((a, b))
print('a = {0}, a.shape = {1}'.format(a, a.shape))
print('b = {0}, b.shape = {1}'.format(b, b.shape))
print('ar1 = {0}, ar1.shape = {1}'.format(ar1, ar1.shape))
a = np.array([[1], [2], [3]])
b = np.array([['a'], ['b'], ['c'], ['d']])
ar2 = np.vstack((a, b)) # 这里形状可以不一样
print('a = {0}, a.shape = {1}'.format(a, a.shape))
print('b = {0}, b.shape = {1}'.format(b, b.shape))
print('ar2 = {0}, ar2.shape = {1}'.format(ar2, ar2.shape))
打印结果:
a = [0 1 2 3 4], a.shape = (5,)
b = [5 6 7 8 9], b.shape = (5,)
ar1 = [[0 1 2 3 4][5 6 7 8 9]], ar1.shape = (2, 5)
--------------------------------------------------
a = [[1][2][3]], a.shape = (3, 1)
b = [['a']['b']['c']['d']], b.shape = (4, 1)
ar2 = [['1']['2']['3']['a']['b']['c']['d']], ar2.shape = (7, 1)
二、hstack
s = np.hstack((a, b, c))
print('hstack \n ', s.shape, '\n', s)
hstack(tup):水平(按列顺序)堆叠数组:
import numpy as np# 数组堆叠# numpy.hstack(tup):水平(按列顺序)堆叠数组
a = np.arange(5) # a为一维数组,5个元素
b = np.arange(5, 9) # b为一维数组,4个元素
ar1 = np.hstack((a, b)) # 注意:((a,b)),这里形状可以不一样
print('a = {0}, a.shape = {1}'.format(a, a.shape))
print('b = {0}, b.shape = {1}'.format(b, b.shape))
print('ar1 = {0}, ar1.shape = {1}'.format(ar1, ar1.shape))
print('-' * 50)
a = np.array([[1], [2], [3]]) # a为二维数组,3行1列
b = np.array([['a'], ['b'], ['c']]) # b为二维数组,3行1列
ar2 = np.hstack((a, b)) # 注意:((a,b)),这里形状必须一样
print('a = {0}, a.shape = {1}'.format(a, a.shape))
print('b = {0}, b.shape = {1}'.format(b, b.shape))
print('ar2 = {0}, ar2.shape = {1}'.format(ar2, ar2.shape))
打印结果:
a = [0 1 2 3 4], a.shape = (5,)
b = [5 6 7 8], b.shape = (4,)
ar1 = [0 1 2 3 4 5 6 7 8], ar1.shape = (9,)
--------------------------------------------------
a = [[1][2][3]], a.shape = (3, 1)
b = [['a']['b']['c']], b.shape = (3, 1)
ar2 = [['1' 'a']['2' 'b']['3' 'c']], ar2.shape = (3, 2)
三、stack
1、axis=0
s = np.stack((a, b, c), axis=0)
print('axis = 0 \n ', s.shape, '\n', s)
就是下面的形式
具体为3组2×3矩阵
stack(arrays, axis=0):沿着新轴连接数组的序列,形状必须一样!
重点解释axis参数的意思,假设两个数组[1 2 3]和[4 5 6],shape均为(3,0)
- axis=0:[[1 2 3] [4 5 6]],shape为(2,3)
- axis=1:[[1 4] [2 5] [3 6]],shape为(3,2)
import numpy as np# 数组堆叠
# numpy.stack(arrays, axis=0):沿着新轴连接数组的序列,形状必须一样!
# 重点解释axis参数的意思,假设两个数组[1 2 3]和[4 5 6],shape均为(3,0)
# axis=0:[[1 2 3] [4 5 6]],shape为(2,3)
# axis=1:[[1 4] [2 5] [3 6]],shape为(3,2)
a = np.arange(5)
b = np.arange(5, 10)
ar1 = np.stack((a, b))
ar2 = np.stack((a, b), axis=1)
print('a = {0}, a.shape = {1}'.format(a, a.shape))
print('b = {0}, b.shape = {1}'.format(b, b.shape))
print('ar1 = {0}, ar1.shape = {1}'.format(ar1, ar1.shape))
print('ar2 = {0}, ar2.shape = {1}'.format(ar2, ar2.shape))
打印结果:
a = [0 1 2 3 4], a.shape = (5,)
b = [5 6 7 8 9], b.shape = (5,)
ar1 = [[0 1 2 3 4][5 6 7 8 9]], ar1.shape = (2, 5)
ar2 = [[0 5][1 6][2 7][3 8][4 9]], ar2.shape = (5, 2)
2、axis=1
s = np.stack((a, b, c), axis=1)
print('axis = 1 \n ', s.shape, '\n', s)
即将每个矩阵的每一行进行堆叠,放在一个矩阵里(一行对应一个矩阵)就是下图的红色的放一起,绿色的放一起
3、axis=2
s = np.stack((a, b, c), axis=2)
print('axis = 2 \n ', s.shape, '\n', s)
即将每行的进行竖排,放在一个矩阵里(一行对应一个矩阵)
这篇关于NumPy(六):数组堆叠:【vstack:垂直(按列顺序)堆叠数组】【hstack:水平(按列顺序)堆叠数组】【stack:axis=0/1/2】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!