亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

1.scipy.stats.shapiro ——Shapiro-Wilk test,屬于專門用來做正態(tài)性檢驗的模塊,其原假設(shè):樣本數(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值,當(dāng)P值大于指定的顯著性水平,則接受原假設(shè)。

2.scipy.stats.kstest(K-S檢驗):可以檢驗多種分布,不止正態(tài)分布,其原假設(shè):數(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'等分布,設(shè)置為'norm'時表示正態(tài)分布。

alternative:默認(rèn)為雙側(cè)檢驗,可以設(shè)置為'less'或'greater'作單側(cè)檢驗。

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

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

3.scipy.stats.normaltest:正態(tài)性檢驗,其原假設(shè):樣本來自正態(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ù)做檢驗,默認(rèn)值是0。

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

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

4.scipy.stats.anderson:由 scipy.stats.kstest 改進(jìn)而來,用于檢驗樣本是否屬于某一分布(正態(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)計值,第二個表示評價值,第三個是顯著性水平;評價值和顯著性水平對應(yī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%

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

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。

偏度:偏度是樣本的標(biāo)準(zhǔn)三階中心矩。

峰度:峰度是樣本的標(biāo)準(zhǔn)四階中心矩。

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)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實現(xiàn)獲取系統(tǒng)臨時目錄及臨時文件的方法示例

    Python實現(xiàn)獲取系統(tǒng)臨時目錄及臨時文件的方法示例

    這篇文章主要介紹了Python實現(xiàn)獲取系統(tǒng)臨時目錄及臨時文件的方法,結(jié)合實例形式分析了Python文件與目錄操作相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下
    2019-06-06
  • python實現(xiàn)俄羅斯方塊游戲(改進(jìn)版)

    python實現(xiàn)俄羅斯方塊游戲(改進(jìn)版)

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)俄羅斯方塊游戲的改進(jìn)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Django怎么在admin后臺注冊數(shù)據(jù)庫表

    Django怎么在admin后臺注冊數(shù)據(jù)庫表

    這篇文章主要介紹了Django怎么在admin后臺注冊數(shù)據(jù)庫表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Python中文件的寫入讀取以及附加文字方法

    Python中文件的寫入讀取以及附加文字方法

    今天小編就為大家分享一篇Python中文件的寫入讀取以及附加文字方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解

    VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解

    這篇文章主要介紹了VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法,較為詳細(xì)的分析了VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的具體步驟、相關(guān)命令與操作注意事項,需要的朋友可以參考下
    2019-07-07
  • pytest-fixture簡介及其用法講解

    pytest-fixture簡介及其用法講解

    這篇文章主要介紹了pytest-fixture及其用法,最基本的用法就是一個fixture作為一個測試用例的參數(shù)傳入,然后就可以在該測試用例中使用該fixture,需要的朋友可以參考下
    2023-01-01
  • Python的numpy選擇特定行列的方法

    Python的numpy選擇特定行列的方法

    這篇文章主要介紹了Python的numpy選擇特定行列的方法,有時需要抽取矩陣中特定行的特定列,比如,需要抽取矩陣x的0,1行的0,3列,結(jié)果為矩陣域,需要的朋友可以參考下
    2023-08-08
  • Python實現(xiàn)復(fù)制文檔數(shù)據(jù)

    Python實現(xiàn)復(fù)制文檔數(shù)據(jù)

    我們百度搜索一些東西得時候,經(jīng)常找到文檔里面然后就會發(fā)現(xiàn)需要充值才能復(fù)制!怎么可以不花錢也保存呢?今天就分享給大家一個python獲取文檔數(shù)據(jù)得方法,需要的可以收藏一下
    2022-12-12
  • Python中跨越多個文件使用全局變量的方法

    Python中跨越多個文件使用全局變量的方法

    全局變量是不屬于函數(shù)范圍的變量,可以在整個程序中使用,這表明全局變量也可以在函數(shù)體內(nèi)部或外部使用,這篇文章主要介紹了Python中跨越多個文件使用全局變量,需要的朋友可以參考下
    2023-09-09
  • PyQt實現(xiàn)異步數(shù)據(jù)庫請求的實戰(zhàn)記錄

    PyQt實現(xiàn)異步數(shù)據(jù)庫請求的實戰(zhàn)記錄

    開發(fā)軟件的時候不可避免要和數(shù)據(jù)庫發(fā)生交互,但是有些 SQL 請求非常耗時,如果在主線程中發(fā)送請求,可能會造成界面卡頓,本文將介紹一種讓數(shù)據(jù)庫請求變得和前端的 ajax 請求一樣簡單,希望對大家有所幫助
    2023-12-12

最新評論