显著性检验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将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专