使用Kolmogorov检验检查正态分布

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

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

# 生成数据集

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

进行KS检验

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

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

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

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

使用Kolmogorov检验检查正态分布


<details>
<summary>英文:</summary>

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


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

Out:
&gt; &#39;D-statistics:0.5091,p-value:0.0000,test-result:Reject&#39;

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

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/DJV7U.png

</details>



# 答案2
**得分**: -2

如果发生错误,'Polygon' 对象没有 'normed' 属性。

发现了 'normed=True',此属性已被弃用。

请改用 'density=True'。

它会起作用。

<details>
<summary>英文:</summary>

if error occured,‘Polygon’ object has no property ‘normed’

it was found,normed=True , this property is deprecated,

change to use &#39;density=True&#39;.

it will work.

</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:

确定