根据顺序将数组分成相等加权的块 [英] Split array into equally weighted chunks based on order

查看:75
本文介绍了根据顺序将数组分成相等加权的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,可以为我提供其他一些数字的累加百分比:

I have an array that gives me a cumulative percentage of some other number:

my_cumulative_array = np.asarray(range(0,50))/float(50) 

我想将此数组分成n个组,每个组权重相同:

I want to split this array into n groups with equal weight in each:

chunks    = [[row indexes 01-10], #First 20% based on order
            [row indexes 11-20],  #Second 20% based on order
            [row indexes 21-30],
            [row indexes 31-40],
            [row indexes 41-50]]

似乎应该有一个聪明的方法可以有效地做到这一点.

It seems like there should be a clever way to do this efficiently.

推荐答案

这个问题的定义不是很好,但是看起来很有趣.下面将把一个数组(arr)分成一个数组列表(chunks),其中chunks中每个数组的总和大致相等.

The question is not well defined, but looked fun. The following will split an array (arr) into a list of arrays (chunks) where the sum of each array in chunks is roughly equal.

splits = 5
arr = np.sin(np.random.rand(100)) + np.arange(100)/50.0
cum_arr = arr.cumsum() / arr.sum()
idx = np.searchsorted(cum_arr, np.linspace(0, 1, splits, endpoint=False)[1:])
chunks = np.split(arr, idx)

我们可以看到拆分索引的间距不相等:

We can observe the split indices are not equally spaced:

print idx
[37 59 74 88]

而块的总和为:

print [np.sum(x) for x in chunks]
[27.93830, 29.51562, 28.30718, 29.23604, 28.7935]

这篇关于根据顺序将数组分成相等加权的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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