【问题标题】:How do I show a plot of Random.gammavariate distribution?如何显示 Random.gammavariate 分布图?
【发布时间】:2017-09-10 21:47:46
【问题描述】:

我对所有不同的分布函数(即 numpy、scipy、matploglib.mlab 和 random)有点困惑

我正在尝试获取伽玛分布数的样本以用于我的程序,并绘制出正在使用的分布。

到目前为止,我正在做:

import random
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import scipy.special as sps
import numpy as np

k = 2.0
theta = 2.0
random.gammavariate(k, theta) # this gives me results of the gamma distribution

# but unfortunately, this doesn't work
# plt.plot(random.gammavariate(k, theta))
# plt.show()

# but this works somehow even if I don't specify 
# what bins and y is - just copied from a https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.gamma.html
# and seems to work
s = np.random.gamma(k, theta, 1000)
plt.plot(bins, y, linewidth=2, color='r')

非常感谢您向我解释这一点的任何帮助。

【问题讨论】:

  • "...并且还绘制出正在使用的分布。" 您是要绘制随机样本的直方图,还是要绘制抽取样本的基础分布的 PDF(或 CDF)(例如 en.wikipedia.org/wiki/Gamma_distribution 处显示的图)?

标签: numpy matplotlib scipy distribution


【解决方案1】:

您正在尝试绘制一维数据的曲线(或散点图)。 您可以通过绘制直方图来显示分布:

plt.hist(np.random.gamma(k, theta,100 ))

请注意,1000 会给您 1000 分。 如果您想从直方图中提取信息,例如 bin:

 count, bins, ignored = plt.hist(np.random.gamma(k, theta, 100))

然后您可以使用以 2D 作为输入的plt.plot 进行绘图:

plt.plot(bins, y)

其中 y 由下式给出:

import scipy.special as sps
import numpy as np
y = bins**(k-1)*(np.exp(-bins/theta) /(sps.gamma(k)*theta**k))

这是伽马函数。

【讨论】:

  • 好的,谢谢您的回复。在运行plt.hist() 后,我不确定在plt.plot() 中添加什么。
  • plt.hist 将绘制垃圾箱。之后不需要plt.plot
  • 准确,plt.plot 将根据您分布的 bin 将相应的分布绘制为传统的线图
  • 但这具有误导性。如果你愿意,你应该计算一个 KDE。
  • 我只是参考了scipy函数并尝试解释它..但是你是对的,谢谢你指出它。
猜你喜欢
  • 2020-05-29
  • 1970-01-01
  • 2020-06-21
  • 2022-11-25
  • 2021-03-13
  • 2018-02-08
  • 2016-10-19
  • 2012-05-10
  • 2019-07-11
相关资源
最近更新 更多