双样本 t 检验

使用 Python 分析调查数据

EbunOluwa Andrew

Data Scientist

比较宜人性

握手的人

group_a.agreeableness.mean()
4.011701199563795
group_b.agreeableness.mean()
4.03669574700109
使用 Python 分析调查数据

定义双样本 t 检验

  • 检验两独立组的均值是否存在显著差异
  • 判断差异是否由随机产生

标有 A 和 B 的两瓶

使用 Python 分析调查数据

双样本 t 检验的前提

  • 独立
  • 正态分布
    • Shapiro-Wilk 检验
    • stats.shapiro()
    • p 值 > 0.05 -> 近似正态
  • 方差齐性
    • Levene 检验
    • stats.levene()
    • p 值 > 0.05 -> 方差相等

木质数字方块

使用 Python 分析调查数据

调查结果

group_a

| userid | agreeableness |
|--------|---------------|
|    895 |          4.78 |
|    a06 |          3.40 |
|    e94 |          3.66 |
|    ee6 |          5.41 |
|    521 |          4.58 |
|    f4c |          3.24 |
...

1 = 不友善

group_b

| userid | agreeableness |
|--------|---------------|
|    b7e | 4.43          |
|    030 | 2.92          |
|    f91 | 4.01          |
|    36f | 2.20          |
|    875 | 3.83          |
|    750 | 4.95          |
...

7 = 友善

使用 Python 分析调查数据

独立分组

两组

使用 Python 分析调查数据

正态分布的分组

from scipy.stats import shapiro
import scipy.stats as stats

norm_A = stats.shapiro(
  group_a.agreeableness)

ShapiroResult(
statistic=0.997467577457428,
pvalue=0.16834689676761627)
from scipy.stats import shapiro
import scipy.stats as stats

norm_B = stats.shapiro(
  group_b.agreeableness)

ShapiroResult(
statistic=0.9987381100654602,
pvalue=0.7757995128631592)
使用 Python 分析调查数据

方差齐性

import scipy.stats as stats

var_test = stats.levene(group_a.agreeableness, group_b.agreeableness)
LeveneResult(statistic=0.40492634057696597, pvalue=0.5246354858484796)
使用 Python 分析调查数据

前提已检验

  • 独立分组
    • 个体不重叠
  • 正态分布的分组
  • 方差齐性
    • 两组方差无显著差异

Apple Pencil-照片来源:Unsplash 的 Dose Media

使用 Python 分析调查数据

使用 statsmodels 的双样本 t 检验

from scipy import stats

stats.ttest_ind(group_a.agreeableness, group_b.agreeableness)
使用 Python 分析调查数据

使用 statsmodels 的双样本 t 检验

Ttest_indResult(statistic=0.7746406648066304, pvalue=0.4386519848366188)
使用 Python 分析调查数据

进一步分析

group_a_mean = 4.011701199563795
group_b_mean = 4.03669574700109

加州住房

使用 Python 分析调查数据

让我们练习吧!

使用 Python 分析调查数据

Preparing Video For Download...