クラスターサンプリング

Pythonで学ぶサンプリング

James Chapman

Curriculum Manager, DataCamp

層化サンプリング vs. クラスターサンプリング

層化サンプリング

  • 母集団をサブグループに分割
  • 各サブグループに単純無作為抽出を適用

クラスターサンプリング

  • 単純無作為抽出でサブグループを選択
  • 選択したサブグループのみに単純無作為抽出を適用
Pythonで学ぶサンプリング

コーヒーの品種

行列に並べられたコーヒー豆。

varieties_pop = list(coffee_ratings['variety'].unique())
[None, 'Other', 'Bourbon', 'Catimor', 
'Ethiopian Yirgacheffe','Caturra', 
'SL14', 'Sumatra', 'SL34', 'Hawaiian Kona',
'Yellow Bourbon', 'SL28', 'Gesha', 'Catuai',
'Pacamara', 'Typica', 'Sumatra Lintong',
'Mundo Novo', 'Java', 'Peaberry', 'Pacas',
'Mandheling', 'Ruiru 11', 'Arusha',
'Ethiopian Heirlooms', 'Moka Peaberry',
'Sulawesi', 'Blue Mountain', 'Marigojipe', 
'Pache Comun']
Pythonで学ぶサンプリング

第1段階:サブグループのサンプリング

行列に並べられたコーヒー豆。3種を除いてグレーアウトされている。

import random
varieties_samp = random.sample(varieties_pop, k=3)
['Hawaiian Kona', 'Bourbon', 'SL28']
Pythonで学ぶサンプリング

第2段階:各グループのサンプリング

variety_condition = coffee_ratings['variety'].isin(varieties_samp)
coffee_ratings_cluster = coffee_ratings[variety_condition]
coffee_ratings_cluster['variety'] = coffee_ratings_cluster['variety'].cat.remove_unused_categories()
coffee_ratings_cluster.groupby("variety")\
    .sample(n=5, random_state=2021)
Pythonで学ぶサンプリング

第2段階の出力

                    total_cup_points        variety       country_of_origin  ...
variety                                                                       
Bourbon       575              82.83        Bourbon               Guatemala   
              560              82.83        Bourbon               Guatemala   
              524              83.00        Bourbon               Guatemala   
              1140             79.83        Bourbon               Guatemala   
              318              83.67        Bourbon                  Brazil   
Hawaiian Kona 1291             73.67  Hawaiian Kona  United States (Hawaii)   
              1266             76.25  Hawaiian Kona  United States (Hawaii)   
              488              83.08  Hawaiian Kona  United States (Hawaii)   
              461              83.17  Hawaiian Kona  United States (Hawaii)   
              117              84.83  Hawaiian Kona  United States (Hawaii)   
SL28          137              84.67           SL28                   Kenya   
              452              83.17           SL28                   Kenya   
              224              84.17           SL28                   Kenya   
              66               85.50           SL28                   Kenya   
              559              82.83           SL28                   Kenya   
Pythonで学ぶサンプリング

多段階サンプリング

  • クラスターサンプリングは多段階サンプリングの一種
  • 3段階以上も可能
  • 例:全国調査では州・郡・市・地区を順にサンプリング
Pythonで学ぶサンプリング

練習しましょう!

Pythonで学ぶサンプリング

Preparing Video For Download...