在numpy中堆叠数组 [英] Stacking arrays in numpy

查看:34
本文介绍了在numpy中堆叠数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

A = np.array([1, 2, 3])
B = np.array([2, 3, 4])
C = np.stack((A, B), axis=0)

print C.shape
(2, 3)

形状不应该是 (6,) 吗?

推荐答案

使用 np.stack() 函数,您可以指定您希望考虑哪个 axis索引轴.因此,如您所见,对于此示例,您永远不会获得 6 的形状,只有 (2,3)(3,2)取决于您选择的轴.

Using the np.stack() function you can specify which axis would you like to be considered the index axis. So as you can see you will never get a shape of 6, only (2,3) or (3,2) for this example depending on what axis you chose.

见下文:

A = np.array([1, 2, 3])
B = np.array([2, 3, 4])
arrays = [A, B]

使用此代码:

print(np.stack(arrays, axis=0))

你得到这个输出:

[[1 2 3]
 [2 3 4]]

使用此代码:

print(np.stack(arrays, axis=1))

你得到这个输出:

[[1 2]
 [2 3]
 [3 4]]

这篇关于在numpy中堆叠数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆