Python - 为整数采样创建偏斜的离散正态概率分布

问题描述 投票:1回答:1

与以下问题类似: Create random numbers with left skewed probability distribution

通过陈述最大值和方差,我想从一些给定范围中采样整数。

例如,对于范围 - {0,1,...,1000}(又名range(1001)),最大值为100,因此采样数最有可能来自[90-110]的范围,不太可能是数字将被采样的是[80-89]和[111-120]等。

python numpy scikit-learn probability normal-distribution
1个回答
1
投票

以下代码将执行此操作:

import scipy.stats as ss
import numpy as np
import matplotlib.pyplot as plt

center = 100
n = 1001
std = 20
x = np.arange(0, n)
prob = ss.norm.pdf(x,loc=center, scale = std )
prob = prob / prob.sum() #normalize the probabilities so their sum is 1    

nums = np.random.choice(x, 20, p=prob)
nums
© www.soinside.com 2019 - 2024. All rights reserved.