电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Python數據正態性檢驗實現過程

瀏覽:27日期:2022-07-29 15:25:40

在做數據分析或者統計的時候,經常需要進行數據正態性的檢驗,因為很多假設都是基于正態分布的基礎之上的,例如: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 npfrom scipy import statsa = np.random.normal(0,2,50)b = np.linspace(0, 10, 100)# Shapiro-Wilk testS,p = stats.shapiro(a)print(’the shapiro test result is:’,S,’,’,p)# kstest(K-S檢驗)K,p = stats.kstest(a, ’norm’)print(K,p)# normaltestN,p = stats.normaltest(b)print(N,p)# Anderson-Darling testA,C,p = stats.anderson(b,dist=’norm’)print(A,C,p)

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 天津市能谱科技有限公司-专业的红外光谱仪_红外测油仪_紫外测油仪_红外制样附件_傅里叶红外光谱技术生产服务厂商 | 宜兴市恺瑞德环保科技有限公司 | 南京蜂窝纸箱_南京木托盘_南京纸托盘-南京博恒包装有限公司 | 造价工程师网,考试时间查询,报名入口信息-网站首页 | 517瓜水果特产网|一个专注特产好物的网站 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 361°官方网站| 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 沉降天平_沉降粒度仪_液体比重仪-上海方瑞仪器有限公司 | 航空障碍灯_高中低光强航空障碍灯_民航许可认证航空警示灯厂家-东莞市天翔航天科技有限公司 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 深圳市万色印象美业有限公司 | 三效蒸发器_多效蒸发器价格_四效三效蒸发器厂家-青岛康景辉 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | 西点培训学校_法式西点培训班_西点师培训_西点蛋糕培训-广州烘趣西点烘焙培训学院 | 家德利门业,家居安全门,别墅大门 - 安徽家德利门业有限公司 | 回收二手冲床_金丰旧冲床回收_协易冲床回收 - 大鑫机械设备 | 糖衣机,除尘式糖衣机,全自动糖衣机,泰州市长江制药机械有限公司 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 北京公寓出租网-北京酒店式公寓出租平台 | 会议会展活动拍摄_年会庆典演出跟拍_摄影摄像直播-艾木传媒 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 专业生产动态配料系统_饲料配料系统_化肥配料系统等配料系统-郑州鑫晟重工机械有限公司 | 真空上料机(一种真空输送机)-百科| 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 媒介云-全网整合营销_成都新闻媒体发稿_软文发布平台 | 蓝莓施肥机,智能施肥机,自动施肥机,水肥一体化项目,水肥一体机厂家,小型施肥机,圣大节水,滴灌施工方案,山东圣大节水科技有限公司官网17864474793 | 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 辽宁资质代办_辽宁建筑资质办理_辽宁建筑资质延期升级_辽宁中杭资质代办 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 电液推杆生产厂家|电动推杆|液压推杆-扬州唯升机械有限公司 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 压滤机-洗沙泥浆处理-压泥机-山东创新华一环境工程有限公司 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 气动绞车,山东气动绞车,气动绞车厂家-烟台博海石油机械有限公司 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 【同风运车官网】一站式汽车托运服务平台,验车满意再付款 | 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 |