隨機抽樣

使用 Python 分析問卷資料

EbunOluwa Andrew

Data Scientist

問卷資料分析中的抽樣

  • 抽樣=自大型母體取小樣本
    • 對更大母體做推論
    • 抽樣 → 資料更易處理
    • 抽樣 → 產生抽樣誤差
    • 增大樣本可減少誤差

Photo by Patrick Fore on Unsplash - 小孩拿著糖果

使用 Python 分析問卷資料

隨機抽樣

  • 每位成員被選中的機率相同
  • 可降低偏誤
  • 高內部效度
  • 高外部效度

彩券

使用 Python 分析問卷資料

.sample() 方法

  • DataFrame.sample(n = _None_, frac = _None_, random_state = _None_)
  • n = 要抽取的筆數
  • frac = 要回傳的比例(0–1)
  • random_state = 隨機種子,確保可重現
使用 Python 分析問卷資料

隨機抽樣範例

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample(n=100) print(sample)
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 3244  | fffe330     | Female | Yes         |
| 21339 | fffe310     | Male   | Yes         |
| 1122  | fffe390     | Male   | Yes         |
| 4363  | fffe313     | Female | Yes         |
使用 Python 分析問卷資料

隨機抽樣範例

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample(frac = 0.1) print(sample)
|     | employee_id | gender | onsite_work |
|-----|-------------|--------|-------------|
| 142 | fffe800     | Female | Yes         |
| 710 | fffe900     | Female | Yes         |
| 242 | fffe700     | Female | Yes         |
| 114 | fffe600     | Female | Yes         |
使用 Python 分析問卷資料

隨機抽樣範例

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample( n = 100, random_state = 123)
import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample( frac = 0.1, random_state = 123)
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 21383 | fffe3       | Female | Yes         |
| 82    | fffe0       | Male   | Yes         |
| 20739 | fffe2       | Male   | Yes         |
| 7662  | fffe9       | Female | Yes         |
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 21383 | fffe3       | Female | Yes         |
| 82    | fffe0       | Male   | Yes         |
| 20739 | fffe2       | Male   | Yes         |
| 7662  | fffe9       | Female | Yes         |
使用 Python 分析問卷資料

一起來練習吧!

使用 Python 分析問卷資料

Preparing Video For Download...