简体   繁体   English

numpy:将频率阵列分配

[英]Numpy: frequency array to distribution

In python with numpy what is the fastest way to turn an array like 在带有numpy的python中,最快的方法是将数组变成

array([0,2,3,1,0,0,1])

into another array 放入另一个数组

array([1,1,2,2,2,3,6])

where the first array gives the frequency of each index (ie index 0 has a frequency of 0, index 1 has a frequency of 2, index 2 has a frequency of 3, and so on) and the second repeats each index as many times as specified in the first array. 其中第一个数组给出每个索引的频率(即,索引0的频率为0,索引1的频率为2,索引2的频率为3,依此类推),第二个数组重复每个索引的次数为在第一个数组中指定。

Background: I use this to 'enflate' (I can't find any better word for it) a k by k Matrix M (sparse or not) given a length k frequency vector f : 背景:在给定长度为k频率矢量f我用它来“填充”(我找不到更好的词)一个k × k矩阵M (稀疏与否):

f  = np.array([0,2,3,1,0,0,1])
f_ = np.array([1,1,2,2,2,3,6])
M_ = M[f_[:,None],f_]

Use np.repeat on the range array covering the length of the input array with the array itself for the count of repetitions - 在范围数组上使用np.repeat覆盖输入数组的长度以及数组本身的重复次数-

np.repeat(np.arange(len(a)), a)

Sample run - 样品运行-

In [109]: a
Out[109]: array([0, 2, 3, 1, 0, 0, 1])

In [110]: np.repeat(np.arange(len(a)), a)
Out[110]: array([1, 1, 2, 2, 2, 3, 6])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM