t-testpython_Python-56 用numpy和scipy.stats 进行t-test检验 2020-09-05
对于两组数据之间真的存在差异,需要进行t检验。用python可以同样实现这个功能。具体代码与解释如下:
import numpy as np
group1 = np.array([12, 15])
group2 = np.array([15, 17])
#find variance for each group
print(np.var(group1), np.var(group2))
import scipy.stats as stats
#perform two sample t-test with equal variances
print(stats.ttest_ind(a=group1, b=group2, equal_var=True))
得到的结果为:
image.png
由于p值大于0.05,假设不成立,即两组之间不存在显著差异。
如果换了个数据,如下:
import numpy as np
group1 = np.array([11, 8])
group2 = np.array([19, 17])
#Next, we’ll use the ttest_ind() function from the scipy.
# stats library to conduct a two sample t-test,
# which uses the following syntax: ttest_ind(a, b, equal_var=True) where:
# a: an array of sample observations for group 1
# b: an array of sample observations for group 2 equal_var:
# if True, perform a standard independent 2 sample t-test
# that assumes equal population variances.
# If False, perform Welch’s t-test, which does not assume e
# qual population variances.
# This is True by default. Before we perform the test,
# we need to decide if we’ll assume the two populations have equal variances or not.
# As a rule of thumb, we can assume
# the populations have equal variances
# if the ratio of the larger sample variance to the smaller sample variance is
# less than 4:1.
#find variance for each group
print(np.var(group1), np.var(group2))
print( 2.25/1.0)
#The ratio of the
# larger sample variance to the smaller sample variance is 2.25 / 1.0 = 2.25<4,
#which is less than 4. This means we can assume that the population variances are equal.
#Thus, we can proceed to perform the two sample t-test with equal variances:
import scipy.stats as stats
#perform two sample t-test with equal variances
print(stats.ttest_ind(a=group1, b=group2, equal_var=True))
得到的结果为:
image.png
即:pvalue=0.042158511307681217<0.05 ,因此,两组数据存在显著差异。
对结果的解释方法为:
·此特定的两个样本t检验的两个假设如下:
·H0:µ1 = µ2(两个总体均值相等)
·HA:µ1≠µ2(两个总体均值不相等)
·由于我们测试的p值(0.042158511307681217)小于于alpha = 0.05,因此我们拒绝测试的原假设。
·我们足够的证据说两个种群之间植物的平均高度不同。
t-testpython_Python-56 用numpy和scipy.stats 进行t-test检验 2020-09-05相关推荐
- scipy.stats常见概率分布-正态分布与泊松分布
常见分布 总结统计工作中几个常用用法在python统计函数库scipy.stats的使用范例 常见术语 pdf,概率密度函数(Probability Density Function),连续型随机变量 ...
- Github上Pandas,Numpy和 Scipy三个库中20个最常用的函数
首发于Datartisan数据工匠 写文章 Github上Pandas,Numpy和 Scipy三个库中20个最常用的函数 Datartisan 9 个月前 几个月前,我看到一篇博客中列出了 Gith ...
- 简述 Python 的 Numpy、SciPy、Pandas、Matplotlib 的区别
From:https://www.jianshu.com/p/32cb09d84487 Numpy:基础的数学计算模块,以矩阵为主,纯数学. SciPy:基于Numpy,提供方法(函数库)直接计算结果 ...
- python可以构建sem模型_python-分组的熊猫DataFrames:如何将scipy.stats.sem应用于它们?...
我知道我可以通过执行以下操作来应用numpy方法: dataList是DataFrames的列表(相同的列/行). testDF = (concat(dataList, axis=1, keys=ra ...
- python scipy.stats.norm.cdf_Python stats.norm方法代码示例
本文整理汇总了Python中scipy.stats.norm方法的典型用法代码示例.如果您正苦于以下问题:Python stats.norm方法的具体用法?Python stats.norm怎么用?P ...
- scipy.stats —— 概率、随机变量与分布
import numpy as np import scipy.stats as st 几率(odds) p(B)=120p(B)=\frac1{20}p(B)=201:二十场比赛只赢一场 **od ...
- 如何画正太分布曲线_python scipy.stats实现各种常见的统计分布
python作为数据分析被大家熟悉.scipy作为数据分析包更是被广为熟知,scipy.stats用来做统计分析非常好用.scipy.stats包含了各种连续分布和离散分布模型.这篇小文使用scipy ...
- 50例源码Python scipy.stats.norm 模块,pdf()
Python scipy.stats.norm 模块,pdf() 实例源码 转:http://codingdict.com/sources/py/scipy.stats.norm/13753.html ...
- python k线斜率计算公式_python – 计算Numpy(或Scipy)中的斜率
我试图找到使用Numpy和Scipy计算斜率的最快和最有效的方法.我有一个包含三个Y变量和一个X变量的数据集,我需要计算它们各自的斜率.例如,我可以轻松地一次执行这一行,如下所示,但我希望有一种更有效 ...
最新文章
- 没学过python、但是还是有公司要-学习python 基础都会了 为什么还是做不出项目呢?...
- libuv 中文编程指南
- 百度智能云开物再收“一个奖状”
- boost::function_types::function_arity用法的测试程序
- FS_S5PC100 UBOOT-2011.12移植,支持DM9000
- 给定一个年份,判断是不是闰年
- mysql 窗口函数_MySQL数分:窗口函数
- 如何快速安装kafka-manager
- 09:向量点积计算【一维数组】
- VScode 环境 配置
- wxpython如何分开界面和运行代码_wxpython笔记:wxpython的界面布局方式(一)
- c语言2种软件下载,C语言代码自动整理工具
- 机器学习——基本原理
- 使用Windows批处理压缩文件
- 任正非:华为不会让CFO接班 干部选拔以李云龙为标杆
- 修改elementUI中面包屑文字颜色
- 【Flutter】返回首页
- 用REM解决多屏适配
- win10笔记本外接屏幕不清晰的解决方法
- 【vijos】1006 晴天小猪历险记之Hill(dijkstra)
热门文章
- 高性能web系统的架构和系统优化
- Telerik RadGridView 右键菜单如何设置?
- 经常用到的一些小病的预防的治疗
- 存储过程分页算法(收藏)
- python资源库——socket网络编程
- Mysql中各种常见数据库存储引擎对比
- sql server 中xml 数据类型的insert、update、delete
- Linux 动态库剖析
- openfeign远程调用不起作用解决_使用Spring Boot的spring.factories进行注入---SpringCloud Alibaba_若依微服务框架改造---工作笔记007
- 编程语言_JavaScript_面试题004