我正在寻找一种快速的方法来迭代地从数组中随机选择索引,如果它们满足某些条件。这里的特定应用程序是一个人口统计模型,其中我有年龄的预期分布数据,例如。
import numpy as np
popsize = int(1e6)
age_pyramid_edges = np.linspace(0,100,101) # An age pyramid with single-year age bins from 0-100
age_pyramid_data = (np.array([0.01] * 100)*popsize).astype(int) # Suppose there's 1% of the population in each age bin
我也有关于人们实际年龄的数据。
actual_ages = np.random.uniform(0, 100, size=popsize)
现在我想比较年龄的预期分布和实际分布,每当实际分布超过年龄A的预期M时,我想随机选择年龄为A的人的M个指数。
comparable_ages = np.digitize(actual_ages, age_pyramid_edges)-1 # Digitize the ages to match the data
counts_of_actual_ages = np.bincount(comparable_ages, minlength=len(age_pyramid_edges)-1)
age_diffs = counts_of_actual_ages-age_pyramid_data
inds_to_flag = []
for age,age_diff in enumerate(age_diffs):
if age_diff>0:
inds_this_age = (comparable_ages==age).nonzero()[-1]
inds = np.random.choice(inds_this_age, age_diff, replace=False).tolist()
inds_to_flag.append(inds)
但是这样做很慢,我想不使用循环来完成它。有没有什么方法可以不使用循环来完成它?
1条答案
按热度按时间62o28rlo1#
如果你正在寻找一种快速的方法来从数组中选择一个随机元素,你可以尝试这样的方法:
输出: