显著性检验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和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以