显著性检验python

2023-12-20 05:38
文章标签 python 检验 显著性

本文主要是介绍显著性检验python,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Table of Contents

  • 1  信用特征检验/模型稳健性检验的代码实现
    • 1.1  常用的检验实现
      • 1.1.1  ttest_ind
      • 1.1.2  曼-惠特尼U检验(Mann-Whitney U test)
      • 1.1.3  KS_检验
      • 1.1.4  非参数统计Wald-Wolfowitz游程检验
      • 1.1.5  Wilcoxon rank-sum statistic
      • 1.1.6  chi-square test
      • 1.1.7  Fridman检验
      • 1.1.8  Nemenyi检验
    • 1.2  信用特征检验

信用特征检验/模型稳健性检验的代码实现

目的:

(1)让大家掌握**区域/所有权等信用特征检验的方法
**

(2)让大家掌握 F r i d m a n 检 验 Fridman检验 Fridman N e m e n y i 检 验 Nemenyi检验 Nemenyi 这两种常见的精度对比校验方法

代码: 师兄写了现成的信用特征检验Excel输出的代码。详见:https://github.com/AnyBrother/Significance_character_test_ykp

reference

.. [1] J. Demsar (2006), Statistical comparisons of classifiers overmultiple data sets, Journal of Machine Learning Research, 7, 1-30... [2] P. Nemenyi (1963) Distribution-free Multiple Comparisons. Ph.D.thesis, Princeton University... [3] L. Sachs (1997), Angewandte Statistik. Berlin: Springer.Pages: 668-675.
import pandas as pd
df=pd.read_excel("model_performance.xlsx", header=0, index_col=0)
df
Model_1Model_2Model_3
dataset_10.450.850.95
dataset_20.670.870.97
dataset_30.460.860.96
dataset_40.560.860.96
dataset_50.470.870.97

   分析工作者常常用标准方法与自己所用的分析方法进行对照试验,然后用统计学方法检验两种结果是否存在显著性差异。若存在显著性差异而又肯定测定过程中没有错误,可以认定自己所用的方法有不完善之处,即存在较大的系统误差。

  因此分析结果的差异需进行统计检验或显著性检验。

常用的检验实现

设第一个总体的均值为 u 1 u_1 u1,第二个总体的均值为 u 2 u_2 u2,则有:

**单侧检验:**有先验知识,一个是否比另一个好/差

1)Ho: u 1 u_1 u1 u 2 u_2 u2,H1: u 1 u_1 u1 > u 2 u_2 u2 if Z< -Za, 拒绝 Ho;

2)Ho: u 1 u_1 u1 u 2 u_2 u2,H1: u 1 u_1 u1 < u 2 u_2 u2 if Z> -Za, 拒绝 Ho;

**双侧检验:**两样本是否存在显著差异,常用

3)Ho: u 1 u_1 u1 = u 2 u_2 u2, H1: u 1 u_1 u1 != u 2 u_2 u2 if Z> -Za / 2,拒绝 Ho。

P值碰巧的概率对无效假设统计意义
P>0.1碰巧出现的可能性大于5%不能否定无效假设两组差别无显著意义
P<0.05碰巧出现的可能性小于5%可以否定无效假设两组差别有显著意义
P <0.01碰巧出现的可能性小于1%可以否定无效假设两者差别有非常显著意义

ttest_ind

Calculates the T − t e s t T-test Ttest for the means of TWO INDEPENDENT samples of scores.

计算两个独立样本得分的平均值的T检验。

这是针对零假设(两个独立样本具有相同的平均(预期)值)的原边检验。 假 设 两 样 本 正 态 分 布 且 具 有 相 同 的 方 差 。 \color{#FF0000}{假设两样本正态分布且具有相同的方差。}

from scipy import stats
statistic, pvalue=stats.mstats.ttest_ind(df["Model_1"],df["Model_2"])
print(statistic)
print(pvalue)
-8.086075400626394
4.042721798234637e-05
import numpy as np
np.random.seed(12345678)
#Test with sample with identical means:rvs1 = stats.norm.rvs(loc=5,scale=10,size=500)
rvs2 = stats.norm.rvs(loc=5,scale=10,size=400)
statistic, pvalue=stats.ttest_ind(rvs1,rvs2)
print(statistic)
print(pvalue)
0.4119830500614155
0.6804501671011296

曼-惠特尼U检验(Mann-Whitney U test)

每 组 样 本 量 必 须 大 于 20 \color{#FF0000}{每组样本量必须大于20} 20

H 0 : u 1 = u 2 , H 1 : u 1 ! = u 2 H_0: u_1 = u_2, H_1:u_1 != u_2 H0u1=u2,H1u1!=u2

$ if Z> -Za / 2,拒绝 H_0$。

group1=[28,31,36,35,32,33,21,12,12,23,19,13,20,17,14,19]
group2=[12,18,19,14,20,19,12,11,8,9,10,15,16,17,10,16]statistic, pvalue= stats.mannwhitneyu(group1, group2)
print(statistic)
print(pvalue)
46.5
0.001107347927116896

KS_检验

This tests whether 2 samples are drawn from the same distribution. Note that, like in the case of the one-sample K-S test, the distribution is assumed to be continuous.

The test uses the two-sided asymptotic K o l m o g o r o v − S m i r n o v Kolmogorov-Smirnov KolmogorovSmirnov distribution.

If the K-S statistic is small or the p-value is high, then we cannot reject the hypothesis that the distributions of the two samples are the same.

from scipy import stats
np.random.seed(12345678)  #fix random seed to get the same result
n1 = 200  # size of first sample
n2 = 300  # size of second sample
#For a different distribution, we can reject the null hypothesis since the pvalue is below 1%:rvs1 = stats.norm.rvs(size=n1, loc=0., scale=1)
rvs2 = stats.norm.rvs(size=n2, loc=0.5, scale=1.5)
statistic, pvalue=stats.ks_2samp(rvs1, rvs2)
print(statistic)
print(pvalue)
0.20833333333333334
5.129279597815284e-05

非参数统计Wald-Wolfowitz游程检验

非 参 数 统 计 W a l d − W o l f o w i t z 游 程 检 验 \color{#FF0000}{非参数统计Wald-Wolfowitz游程检验} WaldWolfowitz

from statsmodels.sandbox.stats.runs import runstest_2samp
x=[104,253,300,308,315,323,331,396,414,452]
y=[184,196,197,248,260,279,355,386,393,432,450]
statistic, pvalue=runstest_2samp(x,y)
print(statistic)
print(pvalue)
-0.8870032598620701
0.37507714541523396

Wilcoxon rank-sum statistic

Compute the Wilcoxon rank-sum statistic for two samples.

T h e W i l c o x o n r a n k − s u m t e s t \color{#FF0000}{The Wilcoxon rank-sum test} TheWilcoxonranksumtest tests the null hypothesis that two sets of measurements are drawn from the same distribution. The alternative hypothesis is that values in one sample are more likely to be larger than the values in the other sample.**

from scipy.stats import ranksums
sample1 = np.random.uniform(-1, 1, 200)
print(sample1[:10])
sample2 = np.random.uniform(-0.5, 1.5, 300) # a shifted distribution
print(sample2[:10])
statistic, pvalue=ranksums(sample1, sample2)
print(statistic)
print(pvalue)
[-0.57746919 -0.05972207  0.89157307 -0.47111938  0.21487712  0.21566889-0.09707397 -0.67379604 -0.77341795 -0.75565369]
[ 1.22562954 -0.02125675  0.79309106  0.36379193  0.9209503   0.82417966-0.06000881  0.69224626 -0.20661069 -0.08388529]
-8.42221423467549
3.694347239802868e-17

chi-square test

from scipy.stats import chi2
import numpy as npT = np.array([[36, 14], [30, 25]])
def chi2_get_p_value_sl(T):det = T[0,0]*T[1,1] - T[0,1]*T[1,0]c2 = float(det) / T[0].sum() * det / T[1].sum() * T.sum() / T[:,0].sum() / T[:,1].sum()p = 1 - chi2.cdf(x=c2, df=1)return p
chi2_get_p_value_sl(T)
0.06450186480705422

Fridman检验

Due to the assumption that the test statistic has a chi squared distribution, the p-value is only reliable for n > 10 and more than 6 repeated measurements.

FriedmanchisquareResult = stats.friedmanchisquare(df.iloc[:,0], df.iloc[:,1], df.iloc[:,2])
print('Friedmanchisquare Result: stat:{}, p-value:{}'.format(FriedmanchisquareResult[0], FriedmanchisquareResult[1]))
Friedmanchisquare Result: stat:10.0, p-value:0.006737946999085468

Nemenyi检验

说明: Fridman检验只能说明模型精度之间存在差别, 但不能说明那个模型更好。因此,需要Nemenyi检验进一步验证两两模型之间的精度是否 有 显 著 差 异 \color{#FF0000}{有显著差异}

import scikit_posthocs as spresult=sp.posthoc_nemenyi_friedman(df)
print(result)
result.to_excel("result.xlsx")#结果输出到result.xlsx中
          Model_1   Model_2   Model_3
Model_1  1.000000  0.254114  0.004467
Model_2  0.254114  1.000000  0.254114
Model_3  0.004467  0.254114  1.000000

信用特征检验

# 运行这个代码框前需要将excel中的数据替换即可
import osos.system("python ./Significance_character_test_Regions.py")#区域的信用特征检验
os.system("python ./Significance_character_test_Provinces.py")#省份的信用特征检验
os.system("python ./Significance_character_test_Industries.py")#行业的信用特征检验
#所有权的信用特征检验

好 用 就 给 个 三 连 吧 ! ! ! \color{#FF0000}{好用就给个三连吧!!!}
好 用 就 给 个 三 连 吧 ! ! ! \color{#FF0000}{好用就给个三连吧!!!}
好 用 就 给 个 三 连 吧 ! ! ! \color{#FF0000}{好用就给个三连吧!!!}

这篇关于显著性检验python的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/514919

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四