• 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值

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

    2.计算SS、df和F值

    1. import numpy as np
    2. import pandas as pd
    3. from scipy.stats import f_oneway, f
    4. # Data for the four classes
    5. class1 = [75, 77, 70, 88, 72, 80, 79, 81]
    6. class2 = [93, 80, 85, 90, 84, 86, 85, 81]
    7. class3 = [65, 67, 77, 68, 65, 64, 62, 68]
    8. class4 = [72, 70, 71, 65, 81, 72, 68, 74]
    9. # Perform one-way ANOVA
    10. f_statistic, p_value = f_oneway(class1, class2, class3, class4)
    11. # Degrees of freedom
    12. num_groups = 4
    13. num_samples = len(class1) + len(class2) + len(class3) + len(class4)
    14. df_between = num_groups - 1
    15. df_within = num_samples - num_groups
    16. # Calculate sum of squares (SS)
    17. mean_total = np.mean([np.mean(class1), np.mean(class2), np.mean(class3), np.mean(class4)])
    18. ss_total = np.sum((np.concatenate([class1, class2, class3, class4]) - mean_total) ** 2)
    19. ss_between = np.sum([len(class1) * (np.mean(class1) - mean_total) ** 2,
    20. len(class2) * (np.mean(class2) - mean_total) ** 2,
    21. len(class3) * (np.mean(class3) - mean_total) ** 2,
    22. len(class4) * (np.mean(class4) - mean_total) ** 2])
    23. ss_within = np.sum((class1 - np.mean(class1)) ** 2) + \
    24. np.sum((class2 - np.mean(class2)) ** 2) + \
    25. np.sum((class3 - np.mean(class3)) ** 2) + \
    26. np.sum((class4 - np.mean(class4)) ** 2)
    27. # Calculate mean squares (MS)
    28. ms_between = ss_between / df_between
    29. ms_within = ss_within / df_within
    30. # Calculate F-statistic
    31. f_statistic_manual = ms_between / ms_within
    32. # Critical F-value
    33. alpha = 0.05
    34. f_crit = f.ppf(1 - alpha, df_between, df_within)
    35. # Create a DataFrame for better tabular representation
    36. data = {
    37. 'Class 1': class1,
    38. 'Class 2': class2,
    39. 'Class 3': class3,
    40. 'Class 4': class4,
    41. }
    42. df = pd.DataFrame(data)
    43. # Output the ANOVA results
    44. print("Analysis of Variance (ANOVA):")
    45. print("F-statistic (from scipy.stats):", f_statistic)
    46. print("P-value (from scipy.stats):", p_value)
    47. print("\nManual Calculation:")
    48. print("SS Between:", ss_between)
    49. print("SS Within:", ss_within)
    50. print("DF Between:", df_between)
    51. print("DF Within:", df_within)
    52. print("MS Between:", ms_between)
    53. print("MS Within:", ms_within)
    54. print("F-statistic (manual calculation):", f_statistic_manual)
    55. print("Critical F-value:", f_crit)
    56. # Interpret the results
    57. if p_value < alpha:
    58. print("\nThere is a significant difference in the effectiveness of the teaching methods.")
    59. else:
    60. print("\nThere is no significant difference in the effectiveness of the teaching methods.")
    1. Manual Calculation:
    2. SS Between: 1538.59375
    3. SS Within: 651.375
    4. DF Between: 3
    5. DF Within: 28
    6. MS Between: 512.8645833333334
    7. MS Within: 23.263392857142858
    8. F-statistic (manual calculation): 22.045992451864645
    9. Critical F-value: 2.9466852660172655

  • 相关阅读:
    PyTorch深度学习(七)【循环神经网络-提高】
    使用js怎么设置视频背景
    CSS盒子模型
    基于SSM的地方文创特产在线商城【数据库设计、源码、开题报告】
    深度解读RAGFlow的深度文档理解DeepDoc
    MySQL实现的一点总结(三)
    Stable Diffusion进阶玩法说明
    libOpenDRIVE
    [ubuntu系统下的文本编辑器nano,vim,gedit,文件使用,以及版本更新问题]
    leetcode 31. 下一个排列(实现next_permutation 函数)
  • 原文地址:https://blog.csdn.net/m0_46306264/article/details/134452121