这是什么';数组序列';由numpy&x27创建;Python中的snp.stack函数?
我正在阅读np.stack上的: 沿新轴连接一系列阵列。这是什么';数组序列';由numpy&x27创建;Python中的snp.stack函数?,python,arrays,numpy,Python,Arrays,Numpy,我正在阅读np.stack上的: 沿新轴连接一系列阵列。 输出:Ndaray 所以np.stack将接受,比如说,2个numpy数组并返回什么?它将返回一个新数组,其中包含一个,嗯,数组序列 我无法想象由一系列数组组成的数组是什么,所以我来做个小实验怎么样: import numpy as np from random import randint arrays = [2.5 * np.random.randn(1,2)+ 3 for _ in range(1,3)] arrays = [a
输出:Ndaray 所以
np.stack
将接受,比如说,2个numpy数组并返回什么?它将返回一个新数组,其中包含一个,嗯,数组序列
我无法想象由一系列数组组成的数组是什么,所以我来做个小实验怎么样:
import numpy as np
from random import randint
arrays = [2.5 * np.random.randn(1,2)+ 3 for _ in range(1,3)]
arrays = [a.astype(int) for a in arrays]
arrays
这给了我:
[array([[1, 2]]), array([[2, 3]])]
那么
np.stack(数组,轴=0)
给予
假设打印输出基本上不可读(10个方括号,真的吗?),我看到了一个数组中的2个数组,按顺序排列。我想文档是正确的,但是我仍然没有想象这个物体看起来像什么
也许我应该看看尺寸:
np.stack(arrays, axis=0).shape
给予
我们有两行,一列和两层?那不是一个数组吗
我的问题是:
- “数组序列”到底是什么?数组如何具有顺序的概念,正如序列的定义一样
- 为什么会有人想要一个“数组序列”,不管它是什么,而不是将多个数组连接成一个(正如
所暗示的那样).shape
- 为什么他们称这个函数为“stack”,这个词的通俗用法是如何帮助他们的
import numpy
a = np.array([1,2,3])
a.shape gives (3,) -> indicates a 1D array
b = np.array([2, 3, 4])
np.stack((a,b),axis=0) -->Stack the two arrays row-wise which means just put the first arrow on top of the second array
np.stack((a,b),axis=1) -->columns wise stack
现在按同样的顺序回答您的问题:
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.stack((a,b),axis=0) ->error
np.concatenate((a,b),axis=0) -->no error
seq[i]
这样的索引。Python是duck类型的,因此任何行为类似于序列的东西都可以称为一个序列。例如,它可以是列表或元组。在这种情况下,订购绝对重要l=[a,b]
其中a
和b
是数组。如果形状兼容,stack(l)
所做的是在“b
”之上创建一个新的数组。当然也有连接
,但它不一样<代码>堆栈创建一个新的维度,但如名称所示,串联
沿指定的轴连接从发行说明中: 新函数
np.stack
提供了一个通用接口,用于沿新轴连接一系列数组,补充了沿现有轴连接的np.concatenate
我认为您的问题始于
np.random.randn(1,2)
可能无法满足您的期望。这将为您提供一个(1,2)
数组
我认为这是一个嵌套列表。“外部”列表有一个项目,而这个项目是两个项目的“内部”列表(事实上,这正是它在array()
包装器中的表示方式)
现在,您将列出其中两个阵列。这些括号位于数组
包装器之外,因此它只是一个列表。然后,np.stack
命令根据axis
命令以某种方式移动包装器内的括号。在这种情况下,axis=0
,0轴中的元素数成为外部列表中的项目数。其他两个维度移过去,形状变为(2,1,2)
作为一个列表,这将是一个包含两个项目的列表,每个项目都是一个单一项目的列表,这个单一项目是一个包含两个数字的列表
除了堆栈
之外,还有许多不同的方法来排列这些数组。另一个主要的是np。concatenate
,它将允许您沿着现有轴连接它们(axis=0
将有一个shape(2,2)
,而axis=1
将有shape(1,4)
堆栈用于您希望新轴连接它们时。- “数组序列”到底是什么?数组如何具有顺序的概念,正如序列的定义一样?
- 序列是一种抽象数据类型,正如您直觉所示,它是项的有序集合,可以假设一个序列实现
和\uuu getitem\uuuuuuuu
,也就是说,它支持括号索引,例如\uuuu len\uuuuu
或seq[0]
,并且具有seq[1]
。它还实现了len
\u包含
、
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
、
,索引
列表
,元组
,str
,字节
和范围
。A内存视图
是一个numpy.ndarray
import numpy a = np.array([1,2,3]) a.shape gives (3,) -> indicates a 1D array b = np.array([2, 3, 4]) np.stack((a,b),axis=0) -->Stack the two arrays row-wise which means just put the first arrow on top of the second array np.stack((a,b),axis=1) -->columns wise stack
a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) np.stack((a,b),axis=0) ->error np.concatenate((a,b),axis=0) -->no error
In [1]: import numpy as np # Ten pieces of 3x4 paper, contained in a list, or "sequence" In [2]: arrays = [np.random.randn(3, 4) for _ in xrange(10)] # Stack the paper so you get a paperstack with thickness 10 ;) In [3]: np.stack(arrays, axis=0).shape Out[3]: (10, 3, 4) # Join each piece's long side with the next one, so you get a larger piece of paper In [4]: np.concatenate(arrays, axis=0).shape Out[4]: (30, 4)
>>> a = np.array([1, 2, 3]) >>> b = np.array([2, 3, 4]) >>> np.stack((a, b), axis=0) array([[1, 2, 3], [2, 3, 4]]) >>> np.stack((a, b), axis=1) array([[1, 2], [2, 3], [3, 4]]) >>>
>>> np.concatenate((a,b)) array([1, 2, 3, 2, 3, 4])
In [325]: arrays = [np.array([[1, 2]]), np.array([[2, 3]])] In [326]: arrays Out[326]: [array([[1, 2]]), array([[2, 3]])] In [327]: arrays[0].shape Out[327]: (1, 2) In [328]: np.array(arrays) Out[328]: array([[[1, 2]], [[2, 3]]]) In [329]: _.shape Out[329]: (2, 1, 2)
np.array([[[1, 2]],[[2, 3]]])
In [332]: np.stack(arrays).shape Out[332]: (2, 1, 2)
In [335]: np.stack(arrays, axis=0).shape Out[335]: (2', 1, 2) In [336]: np.stack(arrays, axis=1).shape Out[336]: (1, 2', 2) In [337]: np.stack(arrays, axis=2).shape Out[337]: (1, 2, 2')