python - 如何根据分布选择样本?
问题描述
我有一个元素数组 [a_1, a_2, ... a_n] 和与此元素 [p_1, p_2, ..., p_n] 关联的概率数组。
我想根据概率 [p_1,p_2,...,p_n] 从 [a_1,...a_n], k << n 中选择“k”个元素。
如何在 python 中编写代码?非常感谢,我没有编程经验
解决方案
利用numpy.random.choice
例子:
from numpy.random import choice
sample_space = np.array([a_1, a_2, ... a_n]) # substitute the a_i's
discrete_probability_distribution = np.array([p_1, p_2, ..., p_n]) # substitute the p_i's
# picking N samples
N = 10
for _ in range(N):
print(choice(sample_space, discrete_probability_distribution)
推荐阅读
- azure - 如何在将文件上传到 azure blob 存储容器时使用添加元数据/标签并将其用于在 blob 触发器中进行过滤
- html - 有没有办法在引导程序中使用 jquery 插件“rdmGrid”?
- javascript - 为什么我的 for 循环只读取我创建的数组的最后一个元素?
- java-8 - 如何在 R2dbcRepository 中使用排序和可分页查询子选择
- java - 如何在java中存储来自API调用的变量
- cypress - 赛普拉斯:TypeError window.parent.$ 不是函数
- flutter - 在颤动的一个屏幕中的选项卡之间共享数据?
- azure-active-directory - MSAL 的 AcquireTokenSilent 不适用于 Azure AD 用户
- algorithm - 这个问题的最佳解决方案是什么?
- php - 如何将 VSCode 连接到 Docker 容器内的 xdebug 3?