無作為抽出

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

EbunOluwa Andrew

Data Scientist

調査データ分析におけるサンプリング

  • サンプリング=母集団から少数を抽出
    • 母集団について推論する
    • サンプリングで扱いやすくする
    • サンプリングには誤差がある
    • 大規模母集団で誤差を抑える

Unsplash の Patrick Fore の写真 - 砂糖菓子を持つ子ども

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...