简体   繁体   English

如何从 Python 中的双峰分布生成 n 个随机值?

[英]How can I generate n random values from a bimodal distribution in Python?

I tried generating and combining two unimodal distributions but think there's something wrong in my code.我尝试生成并组合两个单峰分布,但认为我的代码有问题。

N=400
mu, sigma = 100, 5
mu2, sigma2 = 10, 40
X1 = np.random.normal(mu, sigma, N)
X2 = np.random.normal(mu2, sigma2, N)
w = np.random.normal(0.5, 1, N)
X = w*X1 + (1-w)*X2
X = X.reshape(-1,2)

When I plot XI don't get a bimodal distribution当我绘制 XI 时没有得到双峰分布

It's unclear where your problem is;不清楚你的问题在哪里; it's also unclear what the purpose of the variable w is, and it's unclear how you judge you get an incorrect result, since we don't see the plot code, or any other code to confirm or reject a binomial distribution.也不清楚变量w的目的是什么,也不清楚你如何判断你得到了不正确的结果,因为我们没有看到绘图代码,或任何其他代码来确认或拒绝二项分布。
That is, your example is too incomplete to exactly answer your question.也就是说,您的示例太不完整,无法准确回答您的问题。 But I can make an educated guess.但我可以做出有根据的猜测。

If I do the following below:如果我执行以下操作:

import numpy as np
import matplotlib.pyplot as plt

N=400
mu, sigma = 100, 5
mu2, sigma2 = 10, 40
X1 = np.random.normal(mu, sigma, N)
X2 = np.random.normal(mu2, sigma2, N)
X = np.concatenate([X1, X2])
plt.hist(X)

and that yields the following figure:这产生了下图:

X 的直方图

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

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