使用Kolmogorov检验检查正态分布

huangapple go评论106阅读模式
英文:

Check Normal distribution with Kolmogorov test

问题

我正在使用Python学习统计学,我有一个任务要检查数据是否符合均值为10、方差为5.5的正态分布。我已经查看了scipy.stats.kstest函数,但我不明白如何解释结果,以及在哪里传递均值和方差参数。

谢谢你的帮助。

英文:

I am a learning statistics using python, and I have a task to check that data have Normal Distribution with mean=10 and dispersion=5.5.
I've checked scipy.stats.kstest function, but I don't understand how to interpret the results, and where I should pass mean and dispersion args.

Thank you, for your help

答案1

得分: 1

  1. # 生成数据集
  2. ```python
  3. import scipy
  4. import matplotlib.pyplot as plt
  5. # 生成具有正态分布(均值=0,标准差=15)的数据
  6. data = scipy.stats.norm.rvs(loc=0, scale=15, size=1000, random_state=0)

进行KS检验

  1. # 对你的样本执行KS检验,与norm(10, 5.5)进行比较
  2. D, p = scipy.stats.kstest(data, 'norm', args=(10, 5.5))
  3. test = '拒绝' if p < 0.05 else '不拒绝'
  4. print(f'D-统计量: {D:.4f},\np-值: {p:.4f}, \n检验结果: {test}')

输出:
> 'D-统计量: 0.5091, p-值: 0.0000, 检验结果: 拒绝'

添加包含分布和数据的图表

  1. # 绘制图表以查看分布和数据的直方图
  2. fig, ax = plt.subplots(1, 1)
  3. x = np.linspace(data.min(), data.max(), 100)
  4. 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) 概率密度函数')
  5. ax.hist(data, normed=True, histtype='stepfilled', bins=20, alpha=0.2, label='我的数据分布')
  6. ax.legend(loc='best', frameon=False)
  7. plt.title('norm(10,5.5) vs. 数据')
  8. plt.show()

使用Kolmogorov检验检查正态分布

  1. <details>
  2. <summary>英文:</summary>
  3. # Generate a dataset

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)

  1. # Perfrom KS-test

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}')

  1. Out:
  2. &gt; &#39;D-statistics:0.5091,p-value:0.0000,test-result:Reject&#39;
  3. # Add a plot with distributions and your data

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()

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/DJV7U.png
  3. </details>
  4. # 答案2
  5. **得分**: -2
  6. 如果发生错误,'Polygon' 对象没有 'normed' 属性。
  7. 发现了 'normed=True',此属性已被弃用。
  8. 请改用 'density=True'
  9. 它会起作用。
  10. <details>
  11. <summary>英文:</summary>
  12. if error occured,‘Polygon object has no property normed
  13. it was foundnormed=True , this property is deprecated
  14. change to use &#39;density=True&#39;.
  15. it will work.
  16. </details>

huangapple
  • 本文由 发表于 2020年1月6日 20:29:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定