import scipy
import matplotlib.pyplot as plt
# generate data with norm(mean = 0,std = 15)
data = scipy.stats.norm.rvs(loc = 0,scale = 15,size = 1000,random_state = 0)
执行KS测试
# perform KS test on your sample versus norm(10,5.5)
D, p = scipy.stats.kstest(data, 'norm', args= (10, 5.5))
test = 'Reject' if p < 0.05 else 'Not reject'
print(f'D-statistics: {D:.4f},\np-value: {p:.4f}, \ntest-result: {test}')
输出: 'D统计量:0.5091,p值:0.0000,检验结果:拒绝'
添加包含分布和数据的图
# draw a plot to see the distribution and your data hist
# draw a plot to see the distribution and your data hist
fig, ax = plt.subplots(1, 1)
x = np.linspace(data.min(),data.max(),100)
ax.plot(x, scipy.stats.norm.pdf(x,loc = 10, scale = 5.5), 'r-', color='green', lw=1, alpha=0.6, label='norm(10,5.5) pdf')
ax.hist(data, normed=True, histtype='stepfilled', bins=20, alpha=0.2, label='my data distribution')
ax.legend(loc='best', frameon=False)
plt.title('norm(10,5.5) vs. data')
plt.show()
2条答案
按热度按时间zpqajqem1#
生成数据集
执行KS测试
输出:
'D统计量:0.5091,p值:0.0000,检验结果:拒绝'
添加包含分布和数据的图
bvpmtnay2#
如果发生错误,则'Polygon'对象没有属性'normed'
如果找到该属性,则normed=True,该属性已弃用,
更改为使用“密度=真”。
它将工作。