Python如何检验样本是否服从正态分布

 更新时间:2024年02月26日 09:36:18   作者:烟雨风渡  
这篇文章主要介绍了Python如何检验样本是否服从正态分布问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

在进行t检验、F检验之前,我们往往要求样本大致服从正态分布,下面介绍两种检验样本是否服从正态分布的方法。

可视化

我们可以通过将样本可视化,看一下样本的概率密度是否是正态分布来初步判断样本是否服从正态分布。

代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
# 使用pandas和numpy生成一组仿真数据
s = pd.DataFrame(np.random.randn(500),columns=['value'])
print(s.shape)      # (500, 1)
 
# 创建自定义图像
fig = plt.figure(figsize=(10, 6))
# 创建子图1
ax1 = fig.add_subplot(2,1,1)
# 绘制散点图
ax1.scatter(s.index, s.values)
plt.grid()      # 添加网格
 
# 创建子图2
ax2 = fig.add_subplot(2, 1, 2)
# 绘制直方图
s.hist(bins=30,alpha=0.5,ax=ax2)
# 绘制密度图
s.plot(kind='kde', secondary_y=True,ax=ax2)     # 使用双坐标轴
plt.grid()      # 添加网格
 
# 显示自定义图像
plt.show()

可视化图像如下:

从图中可以初步看出生成的数据近似服从正态分布。

为了得到更具说服力的结果,我们可以使用统计检验的方法,这里使用的是.scipy.stats中的函数。

统计检验

1)kstest

scipy.stats.kstest函数可用于检验样本是否服从正态、指数、伽马等分布,函数的源代码为:

def kstest(rvs, cdf, args=(), N=20, alternative='two-sided', mode='approx'):
    """
    Perform the Kolmogorov-Smirnov test for goodness of fit.
    This performs a test of the distribution F(x) of an observed
    random variable against a given distribution G(x). Under the null
    hypothesis the two distributions are identical, F(x)=G(x). The
    alternative hypothesis can be either 'two-sided' (default), 'less'
    or 'greater'. The KS test is only valid for continuous distributions.
    Parameters
    ----------
    rvs : str, array or callable
        If a string, it should be the name of a distribution in `scipy.stats`.
        If an array, it should be a 1-D array of observations of random
        variables.
        If a callable, it should be a function to generate random variables;
        it is required to have a keyword argument `size`.
    cdf : str or callable
        If a string, it should be the name of a distribution in `scipy.stats`.
        If `rvs` is a string then `cdf` can be False or the same as `rvs`.
        If a callable, that callable is used to calculate the cdf.
    args : tuple, sequence, optional
        Distribution parameters, used if `rvs` or `cdf` are strings.
    N : int, optional
        Sample size if `rvs` is string or callable.  Default is 20.
    alternative : {'two-sided', 'less','greater'}, optional
        Defines the alternative hypothesis (see explanation above).
        Default is 'two-sided'.
    mode : 'approx' (default) or 'asymp', optional
        Defines the distribution used for calculating the p-value.
          - 'approx' : use approximation to exact distribution of test statistic
          - 'asymp' : use asymptotic distribution of test statistic
    Returns
    -------
    statistic : float
        KS test statistic, either D, D+ or D-.
    pvalue :  float
        One-tailed or two-tailed p-value.

2)normaltest

scipy.stats.normaltest函数专门用于检验样本是否服从正态分布,函数的源代码为:

def normaltest(a, axis=0, nan_policy='propagate'):
    """
    Test whether a sample differs from a normal distribution.
    This function tests the null hypothesis that a sample comes
    from a normal distribution.  It is based on D'Agostino and
    Pearson's [1]_, [2]_ test that combines skew and kurtosis to
    produce an omnibus test of normality.
    Parameters
    ----------
    a : array_like
        The array containing the sample to be tested.
    axis : int or None, optional
        Axis along which to compute test. Default is 0. If None,
        compute over the whole array `a`.
    nan_policy : {'propagate', 'raise', 'omit'}, optional
        Defines how to handle when input contains nan. 'propagate' returns nan,
        'raise' throws an error, 'omit' performs the calculations ignoring nan
        values. Default is 'propagate'.
    Returns
    -------
    statistic : float or array
        ``s^2 + k^2``, where ``s`` is the z-score returned by `skewtest` and
        ``k`` is the z-score returned by `kurtosistest`.
    pvalue : float or array
       A 2-sided chi squared probability for the hypothesis test.

3)shapiro

scipy.stats.shapiro函数也是用于专门做正态检验的,函数的源代码为:

def shapiro(x):
    """
    Perform the Shapiro-Wilk test for normality.
    The Shapiro-Wilk test tests the null hypothesis that the
    data was drawn from a normal distribution.
    Parameters
    ----------
    x : array_like
        Array of sample data.
    Returns
    -------
    W : float
        The test statistic.
    p-value : float
        The p-value for the hypothesis test.

下面我们使用第一部分生成的仿真数据,用这三种统计检验函数检验生成的样本是否服从正态分布(p > 0.05),代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
# 使用pandas和numpy生成一组仿真数据
s = pd.DataFrame(np.random.randn(500),columns=['value'])
print(s.shape)      # (500, 1)
 
# 计算均值
u = s['value'].mean()
# 计算标准差
std = s['value'].std()  # 计算标准差
print('scipy.stats.kstest统计检验结果:----------------------------------------------------')
print(stats.kstest(s['value'], 'norm', (u, std)))
print('scipy.stats.normaltest统计检验结果:----------------------------------------------------')
print(stats.normaltest(s['value']))
print('scipy.stats.shapiro统计检验结果:----------------------------------------------------')
print(stats.shapiro(s['value']))

统计检验结果如下:

scipy.stats.kstest统计检验结果:----------------------------------------------------
KstestResult(statistic=0.01596290473494305, pvalue=0.9995623150120069)
scipy.stats.normaltest统计检验结果:----------------------------------------------------
NormaltestResult(statistic=0.5561685865675511, pvalue=0.7572329891688141)
scipy.stats.shapiro统计检验结果:----------------------------------------------------
(0.9985257983207703, 0.9540967345237732)

可以看到使用三种方法检验样本是否服从正态分布的结果中p-value都大于0.05,说明服从原假设,即生成的仿真数据服从正态分布。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python Flask 和 Django 的区别与适用场景示例分析

    Python Flask 和 Django 的区别与适用场景示例分析

    Flask和Django是两个流行的Python Web框架,但设计哲学、功能和用法有很大区别,Flask是一个轻量级框架,简单灵活,适合小型项目和快速原型开发,本文给大家介绍Python Flask 和 Django 的区别与适用场景示例分析,感兴趣的朋友跟随小编一起看看吧
    2024-10-10
  • Python+DeOldify实现老照片上色功能

    Python+DeOldify实现老照片上色功能

    DeOldify是一种技术,以彩色和恢复旧的黑白图像,甚至电影片段。它是由一个叫Jason Antic的人开发和更新的。本文将利用DeOldify实现老照片上色功能,感兴趣的可以了解一下
    2022-06-06
  • Python使用uv整合Pip、Pyenv和Venv的实战指南

    Python使用uv整合Pip、Pyenv和Venv的实战指南

    Python 的包管理一直是个让开发者又爱又恨的话题,从 pip 到 virtualenv,再到 poetry、conda、pdm,工具层出不穷,但似乎总觉得差点意思,最近,Python 圈子杀出了一匹黑马uv,本文给大家介绍了Python使用uv整合Pip、Pyenv和Venv的实战指南,需要的朋友可以参考下
    2026-05-05
  • python-docx的简单使用示例教程

    python-docx的简单使用示例教程

    这篇文章主要介绍了python-docx的简单使用,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • Keras在mnist上的CNN实践,并且自定义loss函数曲线图操作

    Keras在mnist上的CNN实践,并且自定义loss函数曲线图操作

    这篇文章主要介绍了Keras在mnist上的CNN实践,并且自定义loss函数曲线图操作,具有很好的参考价值,希望对大家有所帮助。
    2021-05-05
  • Python完全识别验证码自动登录实例详解

    Python完全识别验证码自动登录实例详解

    今天小编就为大家分享一篇Python完全识别验证码自动登录实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • python使用NumPy文件的读写操作

    python使用NumPy文件的读写操作

    这篇文章主要介绍了python使用NumPy读写文本文件。想了解第三方库文件操作的同学,来看一下吧
    2021-04-04
  • Python中的Django基本命令实例详解

    Python中的Django基本命令实例详解

    这篇文章主要介绍了Python之Django基本命令 ,需要的朋友可以参考下
    2018-07-07
  • 6个实用的Python自动化脚本详解

    6个实用的Python自动化脚本详解

    每天你都可能会执行许多重复的任务,例如阅读 pdf、播放音乐、查看天气、打开书签、清理文件夹等等,使用自动化脚本,就无需手动一次又一次地完成这些任务,非常方便。快跟随小编一起试一试吧
    2022-01-01
  • python爬取热搜制作词云

    python爬取热搜制作词云

    这篇文章主要介绍了python爬取百度热搜制作词云,首先爬取百度热搜,至少间隔1小时,存入文件,避免重复请求,如果本1小时有了不再请求,存入数据库,供词云包使用,爬取热搜,具体流程请需要的小伙伴参考下面文章内容
    2021-12-12

最新评论