91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何實現Python數據正態性檢驗

發布時間:2020-08-03 09:42:37 來源:億速云 閱讀:343 作者:小豬 欄目:開發技術

這篇文章主要講解了如何實現Python數據正態性檢驗,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

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

在Python中,主要有以下檢驗正態性的方法:

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

注:適用于小樣本。

其函數定位為:

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參數為樣本值序列,返回值中第一個為檢驗統計量,第二個為P值,當P值大于指定的顯著性水平,則接受原假設。

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

其函數定義為:

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.

參數是:

rvs:待檢驗數據。

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

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

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

其返回值中第一個為統計量,第二個為P值。

3.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.

其參數:

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

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

其返回值中,第一個是統計量,第二個是P值。

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

其函數定義為:

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.

其參數:

x和dist分別表示樣本數據和分布。

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

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

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%

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

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(峰度)是否與正態分布一致,因為正態分布的偏度=0,峰度=3。

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

如何實現Python數據正態性檢驗

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

如何實現Python數據正態性檢驗

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)

看完上述內容,是不是對如何實現Python數據正態性檢驗有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黔西县| 洪湖市| 桂林市| 平武县| 纳雍县| 如东县| 海城市| 孙吴县| 福海县| 克什克腾旗| 蓬莱市| 本溪市| 彰化市| 林周县| 乳山市| 正定县| 观塘区| 丹阳市| 霍城县| 台山市| 湖南省| 汝阳县| 玉环县| 阿合奇县| 宜川县| 长汀县| 五莲县| 安顺市| 泊头市| 芜湖市| 合川市| 金乡县| 孟州市| 喀什市| 赤峰市| 离岛区| 辰溪县| 大悟县| 喀喇沁旗| 普定县| 大余县|