2012-03-29 78 views
4

我试图将一个数组拆分为n个部分。有时这些零件的尺寸相同,有时它们的尺寸不同。蟒蛇numpy拆分数组到不等的子阵列

我试图使用方法:

split = np.split(list, size) 

这时候大小均分到列表中,但在其他方面没有正常工作。有没有办法做到这一点,将“最终阵列”与“额外”几个元素“贴”?

回答

2
def split_padded(a,n): 
    padding = (-len(a))%n 
    return np.split(np.concatenate((a,np.zeros(padding))),n) 
15

您是否在寻找np.array_split? 这里是文档字符串:

Split an array into multiple sub-arrays. 

Please refer to the ``split`` documentation. The only difference 
between these functions is that ``array_split`` allows 
`indices_or_sections` to be an integer that does *not* equally 
divide the axis. 

See Also 
-------- 
split : Split array into multiple sub-arrays of equal size. 

Examples 
-------- 
>>> x = np.arange(8.0) 
>>> np.array_split(x, 3) 
    [array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7.])] 

http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.array_split.html