numpy 从数组中随机迭代选择索引的快速方法

1yjd4xko  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(158)

我正在寻找一种快速的方法来迭代地从数组中随机选择索引,如果它们满足某些条件。这里的特定应用程序是一个人口统计模型,其中我有年龄的预期分布数据,例如。

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)

但是这样做很慢,我想不使用循环来完成它。有没有什么方法可以不使用循环来完成它?

62o28rlo

62o28rlo1#

如果你正在寻找一种快速的方法来从数组中选择一个随机元素,你可以尝试这样的方法:

import random

def random_element_selector(l):
  last_idx = len(l) - 1
  random_idx = random.randint(0, last_idx)

  return l[random_idx]

输出:

>>> arr = ['a', 'b', 'c', 'd', 'e']
>>> random_element_selector(arr)
'c'
>>> random_element_selector(arr)
'a'
>>> random_element_selector(arr)
'e'
>>> random_element_selector(arr)

相关问题