如何使用Python在DataFrame中使用某一列数据绘制正态概率图

3

我有一个数据帧,包含两列名为“每年几千美元”和“EMPLOY”的数据。

我在这个数据帧中创建了一个名为“cubic_Root”的新变量,通过计算df ['thousands of dollars per year']中的数据。

df['cubic_Root'] = -1 / df['thousands of dollars per year'] ** (1. / 3)

df['cubic_Root']中的数据如下:

ID cubic_Root

1 -0.629961

2 -0.405480

3 -0.329317

4 -0.480750

5 -0.305711

6 -0.449644

7 -0.449644

8 -0.480750

现在,我该如何使用df['cubic_Root']中的数据绘制正态概率图?


请查看此链接:https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.stats.probplot.html - juanpa.arrivillaga
1个回答

6
你需要“概率”图。对于单个图,你需要如下内容。
import scipy.stats
import numpy as np
import matplotlib.pyplot as plt

# 100 values from a normal distribution with a std of 3 and a mean of 0.5
data = 3.0 * np.random.randn(100) + 0.5

counts, start, dx, _ = scipy.stats.cumfreq(data, numbins=20)
x = np.arange(counts.size) * dx + start

plt.plot(x, counts, 'ro')
plt.xlabel('Value')
plt.ylabel('Cumulative Frequency')

plt.show()

enter image description here

如果您想绘制一个分布图,并且已经知道它,那么请将其定义为一个函数,并按如下方式绘制:
import numpy as np
from matplotlib import pyplot as plt

def my_dist(x):
    return np.exp(-x ** 2)

x = np.arange(-100, 100)
p = my_dist(x)
plt.plot(x, p)
plt.show()

如果您没有精确的分布函数,可以生成大样本,绘制直方图并对数据进行平滑处理:
import numpy as np
from scipy.interpolate import UnivariateSpline
from matplotlib import pyplot as plt

N = 1000
n = N/10
s = np.random.normal(size=N)   # generate your data sample with N elements
p, x = np.histogram(s, bins=n) # bin it into n = N/10 bins
x = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centers
f = UnivariateSpline(x, p, s=n)
plt.plot(x, f(x))
plt.show()

你可以在UnivariateSpline函数调用中增加或减少s(平滑系数)以增加或减少平滑程度。例如,使用以下两个值:

enter image description here

事件到达时间的概率密度函数(PDF)。
import numpy as np
import scipy.stats

# generate data samples
data = scipy.stats.expon.rvs(loc=0, scale=1, size=1000, random_state=123)

可以通过简单调用获取内核密度估计。
scipy.stats.gaussian_kde(data,bw_method=bw)

其中bw是估计过程的(可选)参数。对于此数据集,考虑三个bw值,拟合结果如下所示。

# test values for the bw_method option ('None' is the default value)
bw_values =  [None, 0.1, 0.01]

# generate a list of kde estimators for each bw
kde = [scipy.stats.gaussian_kde(data,bw_method=bw) for bw in bw_values]


# plot (normalized) histogram of the data
import matplotlib.pyplot as plt 
plt.hist(data, 50, normed=1, facecolor='green', alpha=0.5);

# plot density estimates
t_range = np.linspace(-2,8,200)
for i, bw in enumerate(bw_values):
    plt.plot(t_range,kde[i](t_range),lw=2, label='bw = '+str(bw))
plt.xlim(-1,6)
plt.legend(loc='best')

enter image description here

参考资料:

Python: Matplotlib - 多个数据集的概率图

如何绘制事件间隔的概率密度函数(PDF)?


@PulkitKedia 请参考 https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.cumfreq.html。 - Tushar Gupta
正常概率图通常使用Z分数绘制在Y轴上,但这里使用了累积频率的分组值(指单个图)。为什么会这样呢? - Pulkit Kedia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接