最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

为数组x中的每个元素选择n个项(从泊松分布中得出)的快速方法

SEO心得admin218浏览0评论
本文介绍了为数组x中的每个元素选择n个项(从泊松分布中得出)的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在解决遇到的问题时遇到了麻烦.

I am having some trouble with solving a problem I encountered.

我有一个包含价格的数组:

I have an array with prices:

>>> x = np.random.randint(10, size=10) array([6, 1, 7, 6, 9, 0, 8, 2, 1, 8])

以及(随机)生成的Poisson分布式到达数组:

And a (randomly) generated array of Poisson distributed arrivals:

>>> arrivals = np.random.poisson(1, size=10) array([4, 0, 1, 1, 3, 2, 1, 3, 2, 1])

每个单次到达都应与相同指数的价格相关联.因此,在上述情况下,第一个元素(x [0])应该被选择4次(y [0]).第二个元素(x [1])应该被选择0次(y [1])...因此结果应该是:

Each single arrival should be associated with the price at the same index. So in the case above, the first element ( x[0] ) should be selected 4 times ( y[0] ). The second element ( x[1] ) should be selected 0 times ( y[1] )... The result thus should be:

array([6, 6, 6, 6, 7, 6, 9, 9, 9, 0, 0, 8, 2, 2, 2, 1, 1, 8])

是否有任何(快速)方法可以完成此任务,而无需遍历数组?任何帮助将不胜感激.

Is there any (fast) way to accomplish this, without iterating over the arrays? Any help would be greatly appreciated.

推荐答案

您可以使用 np.repeat :

In [43]: x = np.array([6, 1, 7, 6, 9, 0, 8, 2, 1, 8]) In [44]: arrivals = np.array([4, 0, 1, 1, 3, 2, 1, 3, 2, 1]) In [45]: np.repeat(x, arrivals) Out[45]: array([6, 6, 6, 6, 7, 6, 9, 9, 9, 0, 0, 8, 2, 2, 2, 1, 1, 8])

但请注意,对于某些计算,可能有必要避免形成此中间数组.例如,请参见 scipy.stats.binned_statistic .

but note that for certain calculations, it might be possible to avoid having to form this intermediate array. See for example, scipy.stats.binned_statistic.

发布评论

评论列表(0)

  1. 暂无评论