用Numpy将数组分割成N个块 [英] Paritition array into N chunks with Numpy

查看:1911
本文介绍了用Numpy将数组分割成N个块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有如何做您将列表分成大小均匀的块吗? 用于将数组拆分为多个块.无论如何,对于使用Numpy的巨型阵列,它是否可以更有效地做到这一点?

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?

推荐答案

尝试从文档中:

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

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

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

如果块数> len(array),则会在内部嵌套空白数组,以解决此问题-如果将拆分数组保存在a中,则可以通过以下方式删除空数组:

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:

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

只要愿意,只需将其保存回a中即可.

Just save that back in a if you wish.

这篇关于用Numpy将数组分割成N个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆