Python 使用 Numpy 将数组划分为 N 个块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14406567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:19:38  来源:igfitidea点击:

Paritition array into N chunks with Numpy

pythonnumpy

提问by Eiyrioü von Kauyf

There is this How do you split a list into evenly sized chunks?for splitting an array into chunks. Is there anyway to do this more efficiently for giant arrays using Numpy?

有这个你如何将一个列表分成大小均匀的块?用于将数组拆分为块。无论如何,对于使用 Numpy 的巨型数组,是否可以更有效地执行此操作?

回答by mgilson

I believe that you're looking for numpy.splitor possibly numpy.array_splitif the number of sections doesn't need to divide the size of the array properly.

我相信您正在寻找numpy.split或者可能numpy.array_split是否需要部分的数量不需要正确划分数组的大小。

回答by Prashant Kumar

Try numpy.array_split.

试试numpy.array_split

From the documentation:

从文档:

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

Identical to numpy.split, but won't raise an exception if the groups aren't equal length.

与 相同numpy.split,但如果组的长度不相等,则不会引发异常。

If number of chunks > len(array) you get blank arrays nested inside, to address that - if your split array is saved in a, then you can remove empty arrays by:

如果块数 > len(array) 你得到嵌套在里面的空白数组,以解决这个问题 - 如果你的拆分数组保存在 中a,那么你可以通过以下方式删除空数组:

[x for x in a if x.size > 0]

Just save that back in aif you wish.

a如果您愿意,只需将其保存回原处。

回答by Theodros Zelleke

Just some examples on usage of array_split, split, hsplitand vsplit:

对使用只是一些例子array_splitsplithsplitvsplit

n [9]: a = np.random.randint(0,10,[4,4])

In [10]: a
Out[10]: 
array([[2, 2, 7, 1],
       [5, 0, 3, 1],
       [2, 9, 8, 8],
       [5, 7, 7, 6]])

Some examples on using array_split:
If you give an array or list as second argument you basically give the indices (before) which to 'cut'

关于使用的一些例子array_split
如果你给出一个数组或列表作为第二个参数,你基本上给出了要“剪切”的索引(之前)

# split rows into 0|1 2|3
In [4]: np.array_split(a, [1,3])
Out[4]:                                                                                                                       
[array([[2, 2, 7, 1]]),                                                                                                       
 array([[5, 0, 3, 1],                                                                                                         
       [2, 9, 8, 8]]),                                                                                                        
 array([[5, 7, 7, 6]])]

# split columns into 0| 1 2 3
In [5]: np.array_split(a, [1], axis=1)                                                                                           
Out[5]:                                                                                                                       
[array([[2],                                                                                                                  
       [5],                                                                                                                   
       [2],                                                                                                                   
       [5]]),                                                                                                                 
 array([[2, 7, 1],                                                                                                            
       [0, 3, 1],
       [9, 8, 8],
       [7, 7, 6]])]

An integer as second arg. specifies the number of equalchunks:

一个整数作为第二个参数。指定相等块的数量:

In [6]: np.array_split(a, 2, axis=1)
Out[6]: 
[array([[2, 2],
       [5, 0],
       [2, 9],
       [5, 7]]),
 array([[7, 1],
       [3, 1],
       [8, 8],
       [7, 6]])]

splitworks the same but raises an exception if an equal split is not possible

split工作相同,但如果不可能进行相等的拆分,则会引发异常

In addition to array_splityou can use shortcuts vsplitand hsplit.
vsplitand hsplitare pretty much self-explanatry:

除了array_split您可以使用快捷方式vsplithsplit.
vsplit并且hsplit几乎不言自明:

In [11]: np.vsplit(a, 2)
Out[11]: 
[array([[2, 2, 7, 1],
       [5, 0, 3, 1]]),
 array([[2, 9, 8, 8],
       [5, 7, 7, 6]])]

In [12]: np.hsplit(a, 2)
Out[12]: 
[array([[2, 2],
       [5, 0],
       [2, 9],
       [5, 7]]),
 array([[7, 1],
       [3, 1],
       [8, 8],
       [7, 6]])]

回答by Jaime

Not quite an answer, but a long comment with nice formatting of code to the other (correct) answers. If you try the following, you will see that what you are getting are views of the original array, not copies, and that was not the case for the accepted answer in the question you link. Be aware of the possible side effects!

不完全是一个答案,而是一个很长的评论,对其他(正确)答案的代码格式很好。如果您尝试以下操作,您将看到您得到的是原始数组的视图,而不是副本,而您链接的问题中接受的答案并非如此。注意可能的副作用!

>>> x = np.arange(9.0)
>>> a,b,c = np.split(x, 3)
>>> a
array([ 0.,  1.,  2.])
>>> a[1] = 8
>>> a
array([ 0.,  8.,  2.])
>>> x
array([ 0.,  8.,  2.,  3.,  4.,  5.,  6.,  7.,  8.])
>>> def chunks(l, n):
...     """ Yield successive n-sized chunks from l.
...     """
...     for i in xrange(0, len(l), n):
...         yield l[i:i+n]
... 
>>> l = range(9)
>>> a,b,c = chunks(l, 3)
>>> a
[0, 1, 2]
>>> a[1] = 8
>>> a
[0, 8, 2]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8]

回答by Nilani Algiriyage

How about this? Here you split the array using the length you want to have.

这个怎么样?在这里,您使用想要的长度拆分数组。

a = np.random.randint(0,10,[4,4])

a
Out[27]: 
array([[1, 5, 8, 7],
       [3, 2, 4, 0],
       [7, 7, 6, 2],
       [7, 4, 3, 0]])

a[0:2,:]
Out[28]: 
array([[1, 5, 8, 7],
       [3, 2, 4, 0]])

a[2:4,:]
Out[29]: 
array([[7, 7, 6, 2],
       [7, 4, 3, 0]])