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

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON: