如何显示 Random.gammavariate 分布图?
How do I show a plot of Random.gammavariate distribution?
我对所有不同的分布函数感到有点困惑(即 numpy、scipy、matploglib.mlab 和随机)
我正在尝试获取 gamma 分布数字的样本以用于我的程序,并绘制出正在使用的分布。
到目前为止我在做:
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')
非常感谢任何帮助我解释这一点的人。
您正在尝试绘制一维数据的曲线(或散点图)。
您可以通过绘制直方图来显示分布:
plt.hist(np.random.gamma(k, theta,100 ))
注意 1000 会给你 1000 分。
如果你想从直方图中提取信息,比如 bins:
count, bins, ignored = plt.hist(np.random.gamma(k, theta, 100))
然后您可以使用 plt.plot
进行绘图,将 2D 作为输入:
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))
这是伽马函数。
我对所有不同的分布函数感到有点困惑(即 numpy、scipy、matploglib.mlab 和随机)
我正在尝试获取 gamma 分布数字的样本以用于我的程序,并绘制出正在使用的分布。
到目前为止我在做:
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')
非常感谢任何帮助我解释这一点的人。
您正在尝试绘制一维数据的曲线(或散点图)。 您可以通过绘制直方图来显示分布:
plt.hist(np.random.gamma(k, theta,100 ))
注意 1000 会给你 1000 分。 如果你想从直方图中提取信息,比如 bins:
count, bins, ignored = plt.hist(np.random.gamma(k, theta, 100))
然后您可以使用 plt.plot
进行绘图,将 2D 作为输入:
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))
这是伽马函数。