在NumPy中检索具有频率数组的数组元素 [英] Retrieving array elements with an array of frequencies in NumPy
本文介绍了在NumPy中检索具有频率数组的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数字数组,a
.我有第二个数组b
,指定我要检索a
中相应元素的次数.如何做到这一点?在这种情况下,输出的顺序并不重要.
I have an array of numbers, a
. I have a second array, b
, specifying how many times I want to retrieve the corresponding element in a
. How can this be achieved? The ordering of the output is not important in this case.
import numpy as np
a = np.arange(5)
b = np.array([1,0,3,2,0])
# desired output = [0,2,2,2,3,3]
# i.e. [a[0], a[2], a[2], a[2], a[3], a[3] ]
推荐答案
这正是np.arange(5).repeat([1,0,3,2,0])
的作用.
这篇关于在NumPy中检索具有频率数组的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文