最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python數(shù)據(jù)正態(tài)性檢驗實現(xiàn)過程

 更新時間:2020年04月18日 09:43:57   作者:落日峽谷  
這篇文章主要介紹了Python數(shù)據(jù)正態(tài)性檢驗實現(xiàn)過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

在做數(shù)據(jù)分析或者統(tǒng)計的時候,經(jīng)常需要進行數(shù)據(jù)正態(tài)性的檢驗,因為很多假設都是基于正態(tài)分布的基礎之上的,例如:T檢驗。

在Python中,主要有以下檢驗正態(tài)性的方法:

1.scipy.stats.shapiro ——Shapiro-Wilk test,屬于專門用來做正態(tài)性檢驗的模塊,其原假設:樣本數(shù)據(jù)符合正態(tài)分布。

注:適用于小樣本。

其函數(shù)定位為:

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.

x參數(shù)為樣本值序列,返回值中第一個為檢驗統(tǒng)計量,第二個為P值,當P值大于指定的顯著性水平,則接受原假設。

2.scipy.stats.kstest(K-S檢驗):可以檢驗多種分布,不止正態(tài)分布,其原假設:數(shù)據(jù)符合正態(tài)分布。

其函數(shù)定義為:

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 G(x) of an observed
  random variable against a given distribution F(x). Under the null
  hypothesis the two distributions are identical, G(x)=F(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.

參數(shù)是:

rvs:待檢驗數(shù)據(jù)。

cdf:檢驗分布,例如'norm','expon','rayleigh','gamma'等分布,設置為'norm'時表示正態(tài)分布。

alternative:默認為雙側檢驗,可以設置為'less'或'greater'作單側檢驗。

model:'approx'(默認值),表示使用檢驗統(tǒng)計量的精確分布的近視值;'asymp':使用檢驗統(tǒng)計量的漸進分布。

其返回值中第一個為統(tǒng)計量,第二個為P值。

3.scipy.stats.normaltest:正態(tài)性檢驗,其原假設:樣本來自正態(tài)分布。

其函數(shù)定義為:

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.

其參數(shù):

axis=None 可以表示對整個數(shù)據(jù)做檢驗,默認值是0。

nan_policy:當輸入的數(shù)據(jù)中有nan時,'propagate',返回空值;'raise' 時,拋出錯誤;'omit' 時,忽略空值。

其返回值中,第一個是統(tǒng)計量,第二個是P值。

4.scipy.stats.anderson:由 scipy.stats.kstest 改進而來,用于檢驗樣本是否屬于某一分布(正態(tài)分布、指數(shù)分布、logistic 或者 Gumbel等分布)

其函數(shù)定義為:

def anderson(x, dist='norm'):
  """
  Anderson-Darling test for data coming from a particular distribution

  The Anderson-Darling tests the null hypothesis that a sample is
  drawn from a population that follows a particular distribution.
  For the Anderson-Darling test, the critical values depend on
  which distribution is being tested against. This function works
  for normal, exponential, logistic, or Gumbel (Extreme Value
  Type I) distributions.

  Parameters
  ----------
  x : array_like
    array of sample data
  dist : {'norm','expon','logistic','gumbel','gumbel_l', gumbel_r',
    'extreme1'}, optional
    the type of distribution to test against. The default is 'norm'
    and 'extreme1', 'gumbel_l' and 'gumbel' are synonyms.

  Returns
  -------
  statistic : float
    The Anderson-Darling test statistic
  critical_values : list
    The critical values for this distribution
  significance_level : list
    The significance levels for the corresponding critical values
    in percents. The function returns critical values for a
    differing set of significance levels depending on the
    distribution that is being tested against.

其參數(shù):

x和dist分別表示樣本數(shù)據(jù)和分布。

返回值有三個,第一個表示統(tǒng)計值,第二個表示評價值,第三個是顯著性水平;評價值和顯著性水平對應。

對于不同的分布,顯著性水平不一樣。

Critical values provided are for the following significance levels:

  normal/exponenential
    15%, 10%, 5%, 2.5%, 1%
  logistic
    25%, 10%, 5%, 2.5%, 1%, 0.5%
  Gumbel
    25%, 10%, 5%, 2.5%, 1%

關于統(tǒng)計值與評價值的對比:當統(tǒng)計值大于這些評價值時,表示在對應的顯著性水平下,原假設被拒絕,即不屬于某分布。

If the returned statistic is larger than these critical values then for the corresponding significance level, the null hypothesis that the data come from the chosen distribution can be rejected.

5.skewtest 和kurtosistest 檢驗:用于檢驗樣本的skew(偏度)和kurtosis(峰度)是否與正態(tài)分布一致,因為正態(tài)分布的偏度=0,峰度=3。

偏度:偏度是樣本的標準三階中心矩。

峰度:峰度是樣本的標準四階中心矩。

6. 代碼如下:

import numpy as np
from scipy import stats

a = np.random.normal(0,2,50)
b = np.linspace(0, 10, 100)

# Shapiro-Wilk test
S,p = stats.shapiro(a)
print('the shapiro test result is:',S,',',p)

# kstest(K-S檢驗)
K,p = stats.kstest(a, 'norm')
print(K,p)

# normaltest
N,p = stats.normaltest(b)
print(N,p)

# Anderson-Darling test
A,C,p = stats.anderson(b,dist='norm')
print(A,C,p)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Tkinter canvas的畫布參數(shù),刪除組件,添加垂直滾動條詳解

    Tkinter canvas的畫布參數(shù),刪除組件,添加垂直滾動條詳解

    這篇文章主要介紹了python tkinter 畫布參數(shù),刪除組件,添加垂直滾動條使用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2021-10-10
  • 基于python實現(xiàn)圖書管理系統(tǒng)

    基于python實現(xiàn)圖書管理系統(tǒng)

    這篇文章主要為大家詳細介紹了基于python實現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Python將Word文檔轉為PDF的兩種方法

    Python將Word文檔轉為PDF的兩種方法

    這篇文章主要介紹了兩種將docx和doc文件轉換為PDF的方法,方法一使用了docx2pdf模塊,方法二使用了win32com模塊,文中通過代碼及圖文介紹的非常詳細,需要的朋友可以參考下
    2024-12-12
  • Python3使用pywinauto如何檢測需要獲取程序元素

    Python3使用pywinauto如何檢測需要獲取程序元素

    這篇文章主要為大家詳細介紹了Python3使用pywinauto如何檢測需要獲取程序元素,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
    2025-02-02
  • python開發(fā)一個解析protobuf文件的簡單編譯器

    python開發(fā)一個解析protobuf文件的簡單編譯器

    這篇文章主要介紹了python如何開發(fā)一個解析protobuf文件的簡單編譯器,幫助大家更好的理解和學習python,感興趣的朋友可以了解下
    2020-11-11
  • python pyqtgraph 保存圖片到本地的實例

    python pyqtgraph 保存圖片到本地的實例

    這篇文章主要介紹了python pyqtgraph 保存圖片到本地的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python日志syslog使用原理詳解

    Python日志syslog使用原理詳解

    這篇文章主要介紹了Python日志syslog使用原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • ?python 中的條件判斷語句的使用介紹

    ?python 中的條件判斷語句的使用介紹

    這篇文章主要介紹了?python 中的條件判斷語句的使用,主要學習內容有封裝過于復雜的邏輯判斷,不同分支下的重復代碼等,更多相關內容,需要的小伙伴可以參考下面文章詳細介紹內容
    2022-03-03
  • PyCharm搭建Spark開發(fā)環(huán)境實現(xiàn)第一個pyspark程序

    PyCharm搭建Spark開發(fā)環(huán)境實現(xiàn)第一個pyspark程序

    這篇文章主要介紹了PyCharm搭建Spark開發(fā)環(huán)境實現(xiàn)第一個pyspark程序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • Python實現(xiàn)如何根據(jù)文件后綴進行分類

    Python實現(xiàn)如何根據(jù)文件后綴進行分類

    本文主要為大家詳細介紹了如何通過python實現(xiàn)根據(jù)文件后綴實現(xiàn)分類,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以關注一下
    2021-12-12

最新評論

孟州市| 靖州| 陵川县| 朝阳区| 定襄县| 桃园市| 临江市| 阿图什市| 门源| 莲花县| 英超| 龙陵县| 青阳县| 津南区| 台江县| 阿拉尔市| 巴彦淖尔市| 莱西市| 白河县| 彭山县| 哈巴河县| 南华县| 东至县| 布尔津县| 兴仁县| 衢州市| 隆德县| 修武县| 庐江县| 都江堰市| 茌平县| 隆尧县| 巴东县| 青海省| 龙岩市| 康马县| 汉沽区| 芜湖县| 于田县| 武安市| 徐州市|