显著性检验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

相关文章

使用Python创建一个能够筛选文件的PDF合并工具

《使用Python创建一个能够筛选文件的PDF合并工具》这篇文章主要为大家详细介绍了如何使用Python创建一个能够筛选文件的PDF合并工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录背景主要功能全部代码代码解析1. 初始化 wx.Frame 窗口2. 创建工具栏3. 创建布局和界面控件4

一文详解如何在Python中使用Requests库

《一文详解如何在Python中使用Requests库》:本文主要介绍如何在Python中使用Requests库的相关资料,Requests库是Python中常用的第三方库,用于简化HTTP请求的发... 目录前言1. 安装Requests库2. 发起GET请求3. 发送带有查询参数的GET请求4. 发起PO

Python与DeepSeek的深度融合实战

《Python与DeepSeek的深度融合实战》Python作为最受欢迎的编程语言之一,以其简洁易读的语法、丰富的库和广泛的应用场景,成为了无数开发者的首选,而DeepSeek,作为人工智能领域的新星... 目录一、python与DeepSeek的结合优势二、模型训练1. 数据准备2. 模型架构与参数设置3

Python进行PDF文件拆分的示例详解

《Python进行PDF文件拆分的示例详解》在日常生活中,我们常常会遇到大型的PDF文件,难以发送,将PDF拆分成多个小文件是一个实用的解决方案,下面我们就来看看如何使用Python实现PDF文件拆分... 目录使用工具将PDF按页数拆分将PDF的每一页拆分为单独的文件将PDF按指定页数拆分根据页码范围拆分

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.