当前位置: 首页 > news >正文

scipy实现单因素方差分析

经典例题

某校高二年级共有四个班,采用四种不同的教学方法进行数学教学,为了比较这四种教学法的效果是否存在明显的差异,期末统考后,从这四个班中各抽取 5 名考生的成绩,如下所示。

班级

一班

二班

三班

四班

1

75

93

65

72

2

77

80

67

70

3

70

85

77

71

4

88

90

68

65

5

72

84

65

81

6

80

86

64

72

7

79

85

62

68

8

81

81

68

74

问这四种教学法的效果是否存在显著性差异(α =0.05)?

1.计算F值

import numpy as np
from scipy.stats import f_oneway# Data for the four classes
class1 = [75, 77, 70, 88, 72, 80, 79, 81]
class2 = [93, 80, 85, 90, 84, 86, 85, 81]
class3 = [65, 67, 77, 68, 65, 64, 62, 68]
class4 = [72, 70, 71, 65, 81, 72, 68, 74]# Perform one-way ANOVA
f_statistic, p_value = f_oneway(class1, class2, class3, class4)# Output the results
print("F-statistic:", f_statistic)
print("P-value:", p_value)# Interpret the results
alpha = 0.05
if p_value < alpha:print("There is a significant difference in the effectiveness of the teaching methods.")
else:print("There is no significant difference in the effectiveness of the teaching methods.")
F-statistic: 22.045992451864645
P-value: 1.5622062333927252e-07
There is a significant difference in the effectiveness of the teaching methods.

2.计算SS、df和F值

import numpy as np
import pandas as pd
from scipy.stats import f_oneway, f# Data for the four classes
class1 = [75, 77, 70, 88, 72, 80, 79, 81]
class2 = [93, 80, 85, 90, 84, 86, 85, 81]
class3 = [65, 67, 77, 68, 65, 64, 62, 68]
class4 = [72, 70, 71, 65, 81, 72, 68, 74]# Perform one-way ANOVA
f_statistic, p_value = f_oneway(class1, class2, class3, class4)# Degrees of freedom
num_groups = 4
num_samples = len(class1) + len(class2) + len(class3) + len(class4)
df_between = num_groups - 1
df_within = num_samples - num_groups# Calculate sum of squares (SS)
mean_total = np.mean([np.mean(class1), np.mean(class2), np.mean(class3), np.mean(class4)])
ss_total = np.sum((np.concatenate([class1, class2, class3, class4]) - mean_total) ** 2)
ss_between = np.sum([len(class1) * (np.mean(class1) - mean_total) ** 2,len(class2) * (np.mean(class2) - mean_total) ** 2,len(class3) * (np.mean(class3) - mean_total) ** 2,len(class4) * (np.mean(class4) - mean_total) ** 2])
ss_within = np.sum((class1 - np.mean(class1)) ** 2) + \np.sum((class2 - np.mean(class2)) ** 2) + \np.sum((class3 - np.mean(class3)) ** 2) + \np.sum((class4 - np.mean(class4)) ** 2)# Calculate mean squares (MS)
ms_between = ss_between / df_between
ms_within = ss_within / df_within# Calculate F-statistic
f_statistic_manual = ms_between / ms_within# Critical F-value
alpha = 0.05
f_crit = f.ppf(1 - alpha, df_between, df_within)# Create a DataFrame for better tabular representation
data = {'Class 1': class1,'Class 2': class2,'Class 3': class3,'Class 4': class4,
}df = pd.DataFrame(data)# Output the ANOVA results
print("Analysis of Variance (ANOVA):")
print("F-statistic (from scipy.stats):", f_statistic)
print("P-value (from scipy.stats):", p_value)
print("\nManual Calculation:")
print("SS Between:", ss_between)
print("SS Within:", ss_within)
print("DF Between:", df_between)
print("DF Within:", df_within)
print("MS Between:", ms_between)
print("MS Within:", ms_within)
print("F-statistic (manual calculation):", f_statistic_manual)
print("Critical F-value:", f_crit)# Interpret the results
if p_value < alpha:print("\nThere is a significant difference in the effectiveness of the teaching methods.")
else:print("\nThere is no significant difference in the effectiveness of the teaching methods.")
Manual Calculation:
SS Between: 1538.59375
SS Within: 651.375
DF Between: 3
DF Within: 28
MS Between: 512.8645833333334
MS Within: 23.263392857142858
F-statistic (manual calculation): 22.045992451864645
Critical F-value: 2.9466852660172655

http://www.lryc.cn/news/232315.html

相关文章:

  • 深度学习实战59-NLP最核心的模型:transformer的搭建与训练过程详解,手把手搭建与跑通
  • 一阶滤波器(一阶巴特沃斯滤波器)
  • .net core中前端vue HTML5 History 刷新页面404问题
  • 【152.乘积最大子数组】
  • 如何开发OA系统场景的系统架构
  • spring boot 集成 RedisSearch 和 RedisJSON
  • 【Kotlin精简】第8章 协程
  • 【MATLAB源码-第79期】基于蚯蚓优化算法(EOA)的栅格路径规划,输出做短路径图和适应度曲线。
  • RPC实现简单解析
  • 【Ubuntu】Ubuntu20.04下安装视频播放器vlc和录屏软件ssr
  • WMS仓储管理系统与TMS系统整合后的优势
  • 测试的专用
  • sqli-labs(Less-4) extractvalue闯关
  • Kafka简单汇总
  • 任务交给谁?委派模式告诉你最佳选择!
  • 【JavaEE】Servlet(创建Maven、引入依赖、创建目录、编写及打包、部署和验证、smart Tomcat)
  • 降低城市内涝风险,万宾科技内涝积水监测仪的作用
  • 水库大坝安全监测预警系统的重要作用
  • 【AI视野·今日NLP 自然语言处理论文速览 第六十五期】Mon, 30 Oct 2023
  • 腾讯云轻量服务器购买优惠,腾讯云轻量应用服务器优惠购买方法
  • zookeeper学习记录
  • C语言--字符串详解(多角度分析,什么是字符串?字符串如何存储?字符串如何应用?字符串常用的库函数有哪些?)
  • 【文件包含】任意文件包含的理解
  • 【ERROR】ERR_PNPM_NO_IMPORTER_MANIFEST_FOUND No package.json
  • Gitlab CI如何实现安全获取ssh-key拉取依赖项目,打包成品
  • C#匿名方法介绍
  • Linux C/C++全栈开发知识图谱(后端/音视频/游戏/嵌入式/高性能网络/存储/基础架构/安全)
  • pyTorch Hub 系列#2:VGG 和 ResNet
  • clip4clip:an empirical study of clip for end to end video clip retrieval
  • rocksdb中测试工具Benchmark.sh用法(基准、性能测试)