層化無作為抽出法

Pythonで学ぶアンケートデータ分析

EbunOluwa Andrew

Data Scientist

層化無作為抽出法とは

  • 層化抽出は母集団をより正確に反映
  • 類似属性で母集団を離散的な単位(層)に分ける手法
  • サンプルを再抽出し、比率を母集団に合わせる

大集団の変化として多様性が変化する人口動態

Pythonで学ぶアンケートデータ分析

なぜ層化無作為抽出法を使うのか

  • 選択バイアスを最小化
  • 特定の集団の代表性を高める
  • 例:
    • 異なる地域での所得推定
    • 世論調査の推定
    • 平均寿命の推定

手持ちのカウンターで人数をカウント

Pythonで学ぶアンケートデータ分析

層化無作為抽出法を使わない場合

  • 下位集団は重複しないこと
    • 複数の群に入る対象は誤表現の原因
  • 質問の重複例
    • 現在の勤務先での勤続年数は?
      • 1〜2年
      • 2〜4年

考えている女性

Pythonで学ぶアンケートデータ分析

ABC社の出社状況アンケート結果

| employee_id | gender | onsite_work |
|-------------|--------|-------------|
|    fffe6838 | Male   | Yes         |
|   fffe12184 | Female | Yes         |
|    fffe9404 | Female | Yes         |
|   fffe17578 | Male   | Yes         |
|   fffe22257 | Female | Yes         |
|    fffe6217 | Male   | Yes         |
|    fffe7828 | Female | Yes         |
|   fffe18192 | Male   | Yes         |
|    fffe2839 | Female | Yes         |
|   fffe16173 | Female | Yes         |

Pythonで学ぶアンケートデータ分析

母集団の比率を確認

survey.gender.value_counts(normalize=True)
Female    0.556
Male      0.444
Name: gender, dtype: float64
Pythonで学ぶアンケートデータ分析

母集団の比率を可視化

import pandas as pd
import matplotlib.pyplot as plt

survey.gender.value_counts().plot.pie()

調査の女性対男性の比率

Pythonで学ぶアンケートデータ分析

層化抽出の例

strat_sample = 
survey.groupby(
  'gender', group_keys = False).apply(
  lambda x: x.sample(frac = 0.1))
| employee_id | gender | onsite_work |
|-------------|--------|-------------|
|    fffe4934 | Female | Yes         |
|    fffe3958 | Female | Yes         |
|      fffe18 | Female | Yes         |
|     fffe283 | Female | Yes         |
|   fffe20382 | Female | Yes         |
|    fffe8721 | Male   | Yes         |
|    fffe5955 | Male   | Yes         |
|    fffe7081 | Male   | Yes         |
|     fffe353 | Male   | Yes         |
|    fffe1765 | Male   | Yes         |

Pythonで学ぶアンケートデータ分析

サンプルの比率を確認

元の母集団

survey.gender.value_counts(normalize=True)

層化サンプル

strat_sample.gender.value_counts(
  normalize=True))
Female    0.556
Male      0.444
Name: gender, dtype: float64
Female    0.56
Male      0.44
Name: gender, dtype: float64
Pythonで学ぶアンケートデータ分析

Passons à la pratique !

Pythonで学ぶアンケートデータ分析

Preparing Video For Download...