Échantillonnage en grappes

L’échantillonnage en Python

James Chapman

Curriculum Manager, DataCamp

Stratifié vs en grappes

Échantillonnage stratifié

  • Diviser la population en sous-groupes
  • Tirage aléatoire simple dans chaque sous-groupe

Échantillonnage en grappes

  • Tirage aléatoire simple pour choisir quelques sous-groupes
  • Tirage aléatoire simple uniquement dans ces sous-groupes
L’échantillonnage en Python

Variétés de café

Grains de café disposés en lignes et colonnes.

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']
L’échantillonnage en Python

Étape 1 : échantillonner les sous-groupes

Grains de café en lignes et colonnes, tous grisés sauf trois.

import random
varieties_samp = random.sample(varieties_pop, k=3)
['Hawaiian Kona', 'Bourbon', 'SL28']
L’échantillonnage en Python

Étape 2 : échantillonner chaque groupe

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)
L’échantillonnage en Python

Sortie de l’étape 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   
L’échantillonnage en Python

Échantillonnage à plusieurs degrés

  • L’échantillonnage en grappes est un type d’échantillonnage à plusieurs degrés
  • Peut avoir > 2 étapes
  • Ex. les enquêtes nationales peuvent échantillonner États, comtés, villes, quartiers
L’échantillonnage en Python

Passons à la pratique !

L’échantillonnage en Python

Preparing Video For Download...