建立實驗

Python 的實驗設計

James Chapman

Curriculum Manager, DataCamp

實驗設計是什麼?

 

  • 一個流程
  • 有目標、可控的方法
  • 得出明確結論
    • 假設為依據

實驗設計示意圖

1 https://www.sciencedirect.com/topics/earth-and-planetary-sciences/experimental-design
Python 的實驗設計

建立有力的敘述

X 大概對 Y 有影響。可能存在風險的錯誤

 

  • 精確且量化的語言

P 值分析顯示,X 對 Y 有影響型一錯誤風險為 10%

  • 型一錯誤:錯誤地拒絕虛無假設

 

  • 目標:實驗設計統計分析
Python 的實驗設計

為什麼要做實驗設計?

 

適用於多個領域:

  • 醫學研究
  • 行銷
  • 產品分析
  • 農業
  • 政府政策

為什麼需要實驗設計?

Python 的實驗設計

一些術語…

     

  • 受試者(subjects)=我們要實驗的對象(人、員工、使用者等)

一位受試者:此例為人。

Python 的實驗設計

一些術語…

     

  • 受試者(subjects)=我們要實驗的對象(人、員工、使用者等)
  • 處置(treatment)=對一組施加的變更

對一組受試者施加處置。

Python 的實驗設計

一些術語…

     

  • 受試者(subjects)=我們要實驗的對象(人、員工、使用者等)
  • 處置(treatment)=對一組施加的變更

對處置組施加處置。

Python 的實驗設計

一些術語…

     

  • 受試者(subjects)=我們要實驗的對象(人、員工、使用者等)
  • 處置(treatment)=對一組施加的變更
  • 控制組(control)=未施加任何變更的一組

將受試者分為處置組與控制組。

Python 的實驗設計

將受試者分組

     

  • 要如何把受試者分組?
    • 選項 1-非隨機(「切分」DataFrame)
    • 選項 2-隨機分派

     

  • 範例:200 位受試者身高在 heights DataFrame 中
    id  height
0    0  177.98
1    1  174.17
2    2  178.89
Python 的實驗設計

非隨機分派

 

以切片方式分配 DataFrame

group1_nonrandom = heights.iloc[0:100,:]
group2_nonrandom = heights.iloc[100:,:]

compare_df = pd.concat( [group1_nonrandom['height'].describe(), group2_nonrandom['height'].describe()], axis=1) compare_df.columns = ['group1', 'group2'] print(compare_df)
  • 差很多!(平均差 9 公分)

 

 

       group1  group2
count  100.00  100.00
mean   170.32  179.19 <--
std      3.28    3.50
min    159.28  175.03
25%    168.06  176.57
50%    170.75  178.03
75%    173.09  180.79
max    174.92  191.32
Python 的實驗設計

隨機分派

     

  • 使用 .sample()
    • nfrac(比例 0-1)
group1 = heights.sample(frac=0.5,
                        replace=False,
                        random_state=42)

group2 = heights.drop(group1.index)
print(compare_df)
  • 差距小很多!(<1 公分)

 

       group1  group2
count  100.00  100.00
mean   175.10  174.41 <--
std      5.39    5.78
min    163.07  159.28
25%    171.32  170.17
50%    175.22  174.86
75%    178.32  177.85
max    189.78  191.32
Python 的實驗設計

分派重點整理

 

  • 受試者應_隨機_分派到各組
    • 才能正確歸因觀察到的變化

 

  • 隨機分派受試者.sample()
  • 檢查差異.describe()

將受試者隨機分派到處置組與控制組。

Python 的實驗設計

一起來練習吧!

Python 的實驗設計

Preparing Video For Download...