[英]NumPy stack, vstack and sdtack usage
I am trying to better understand hstack, vstack, and dstack in NumPy.我试图更好地理解 NumPy 中的 hstack、vstack 和 dstack。
a = np.arange(96).reshape(2,4,4,3)
print(a)
print(f"dimensions of a:", np.ndim(a))
print(f"Shape of a:", a.shape)
b = np.arange(201,225).reshape(2,4,3)
print(f"Shape of b:", b)
c = np.arange(101,133).reshape(2,4,4)
print(c)
print(f"dimensions of c:", np.ndim(c))
print(f"Shape of c:", c.shape)
a
is: a
是:
[[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
[[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]]
[[24 25 26]
[27 28 29]
[30 31 32]
[33 34 35]]
[[36 37 38]
[39 40 41]
[42 43 44]
[45 46 47]]]
[[[48 49 50]
[51 52 53]
[54 55 56]
[57 58 59]]
[[60 61 62]
[63 64 65]
[66 67 68]
[69 70 71]]
[[72 73 74]
[75 76 77]
[78 79 80]
[81 82 83]]
[[84 85 86]
[87 88 89]
[90 91 92]
[93 94 95]]]]
and c
is:和
c
是:
[[[101 102 103 104]
[105 106 107 108]
[109 110 111 112]
[113 114 115 116]]
[[117 118 119 120]
[121 122 123 124]
[125 126 127 128]
[129 130 131 132]]]
and b
is:和
b
是:
[[[201 202 203]
[204 205 206]
[207 208 209]
[210 211 212]]
[[213 214 215]
[216 217 218]
[219 220 221]
[222 223 224]]]
How do I reshape c
so that I can use hstack
correctly: I wish to add one column for each row in each of the dimensions.如何重塑
c
以便我可以正确使用hstack
:我希望为每个维度中的每一行添加一列。
How do I reshape b
so that I can use vstack
correctly: I wish one row for each column in each of the dimensions.如何重塑
b
以便我可以正确使用vstack
:我希望每个维度中的每一列都有一行。
I would like to come up with a general rule on the dimensions to check for the array that needs to be added to an existing array.我想提出一个关于维度的一般规则,以检查需要添加到现有数组中的数组。
You can concatenate
to a (2,4,4,3)
a您可以
concatenate
到(2,4,4,3)
a
(1,4,4,3) axis 0
(2,1,4,3) with axis=1
(2,4,1,3) axis 2
(2,4,4,1) axis 3
Read and reread as needed, the np.concatenate
docs.根据需要阅读和重新阅读
np.concatenate
文档。
In previous post(s) I've summarized the code of hstack
and vstack
, though you easily read that via the [source] link in the official docs.在之前的帖子中,我总结了
hstack
和vstack
的代码,尽管您可以通过官方文档中的 [source] 链接轻松阅读。
When should I use hstack/vstack vs append vs concatenate vs column_stack? 我什么时候应该使用 hstack/vstack vs append vs concatenate vs column_stack?
hstack
makes sure all arguments are atleast_1d
and does a concatenate on axis 0 or 1. vstack
makes sure all are atleast_2d
, and does a concatenate on axis 0. hstack
确保所有参数都是atleast_1d
并在轴 0 或 1 上进行连接。 vstack
确保所有参数都是atleast_2d
并在轴 0 上进行连接。
Maybe I should have insisted on seeing your attempts and any errors (and attempts to understand the errors).也许我应该坚持看到您的尝试和任何错误(并尝试理解错误)。
For adding c
to a
:将
c
添加到a
:
In [58]: np.hstack((a,c))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [58], in <cell line: 1>()
----> 1 np.hstack((a,c))
File <__array_function__ internals>:5, in hstack(*args, **kwargs)
File ~\anaconda3\lib\site-packages\numpy\core\shape_base.py:345, in hstack(tup)
343 return _nx.concatenate(arrs, 0)
344 else:
--> 345 return _nx.concatenate(arrs, 1)
File <__array_function__ internals>:5, in concatenate(*args, **kwargs)
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 4 dimension(s) and the array at index 1 has 3 dimension(s)
Notice, the error was raised by concatenate
, and focuses on the number of dimensions - 4d and 3d.请注意,错误是由
concatenate
引发的,并且主要关注维度的数量 - 4d 和 3d。 The hstack
wrapper did not change inputs at all. hstack
包装器根本没有改变输入。
If I add a trailing dimension to c
, I get:如果我向
c
添加一个尾随维度,我会得到:
In [62]: c[...,None].shape
Out[62]: (2, 4, 4, 1)
In [63]: np.concatenate((a, c[...,None]),axis=3).shape
Out[63]: (2, 4, 4, 4)
Similarly for b
:同样对于
b
:
In [64]: np.concatenate((a, b[...,None,:]),axis=2).shape
Out[64]: (2, 4, 5, 3)
The hstack/vstack
docs specify 2nd and 1st axis concatenate. hstack/vstack
文档指定第 2 轴和第 1 轴连接。 But you want to use axis 2 or 3. So those 'stack' functions don't apply, do they?但是您想使用轴 2 或轴 3。所以那些“堆栈”功能不适用,是吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.