避免由numpy.vsplit添加额外的尺寸 [英] Avoid extra dimension added by numpy.vsplit
问题描述
我们可以使用vstack
(或hstack
)连接多个一维数组,例如D = np.vstack([a,b,c])
.
反向操作为[a2,b2,c2] = np.vsplit(D, 3)
.
但是维度在往返过程中会发生变化:
We can join several 1d arrays with vstack
(or hstack
), e.g. D = np.vstack([a,b,c])
.
The reverse operation is [a2,b2,c2] = np.vsplit(D, 3)
.
But the dimensionality changes in the round-trip:
import numpy as np
a = np.random.rand(10,)
b = np.random.rand(10,)
c = np.random.rand(10,)
D = np.vstack([a,b,c])
[a2,b2,c2] = np.vsplit(D, 3)
>>> a.shape
(10,)
>>> a2.shape
(1, 10)
我知道挤压以移除尺寸:
I know about squeeze to remove a dimension:
>>> a2.squeeze().shape
(10,)
但这很麻烦,尤其是在拆分多个数组时.
But this is cumbersome, especially when splitting more than a couple of arrays.
有什么方法可以自动"执行挤压操作,或者以其他方式控制vsplit的输出以避免尺寸不匹配?
Is there any way to 'automatically' perform a squeeze, or otherwise control the output of vsplit to avoid the mismatch in dimensions?
(拆分文档就我所知,没有提及任何控制输出尺寸的方法)
(the split docs do not mention any way to control the output dimensions as far as I can tell)
推荐答案
In [98]: D = np.arange(12).reshape(4,3)
In [99]: np.vsplit(D, 4)
Out[99]:
[array([[0, 1, 2]]),
array([[3, 4, 5]]),
array([[6, 7, 8]]),
array([[ 9, 10, 11]])]
split
使用切片来选择行,从而保留了该维度
split
is using a slice to select rows, thus preserving that dimension
[D[i:i+1,:] for i in range(4)]
这是一种常规行为,它可以返回其他尺寸分割.
That's a general behavior that lets it return other size splits.
但是看来您想一次返回一行.有很多方法可以做到这一点:
But it appears you want to return one row at a time. There are many ways of doing this:
很容易迭代地应用squeeze
(而且成本也不高,因为split
已经在迭代):
It's easy to apply squeeze
iteratively (and not much more expensive, since split
is already iterating):
In [100]: [np.squeeze(x) for x in np.vsplit(D, 4)]
Out[100]: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
或者您可以使用纯列表理解:
Or you can use a plain list comprehension:
In [101]: [x for x in D]
Out[101]: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
或将数组转换为列表(与D.tolist()
不同:
Or convert the array to a list (this is different from D.tolist()
:
In [102]: list(D)
Out[102]: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
或按索引迭代.类似于split
,但是使用标量索引而不是切片.很高兴理解D[i,:]
和D[i:i+1, :]
之间的区别.
Or iteration by index. This is like split
, but uses a scalar index rather than the slice. It's good to understand the difference between D[i,:]
and D[i:i+1, :]
.
In [103]: [D[i] for i in range(4)]
Out[103]: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
由于使用的是开箱包装,因此不需要任何包装.打开包装将为您完成迭代"行:
Since you are using unpacking, you don't need any of this. The unpacking will do the row 'iteration' for you:
In [106]: a,b,c,d = D
In [107]: a,b,c,d
Out[107]: (array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11]))
这篇关于避免由numpy.vsplit添加额外的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!