2012-03-29 17 views
4

配列をn個の部分に分割しようとしています。時々、これらの部品は同じサイズであり、異なるサイズの部品もあります。python 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

関連する問題