Introduction au bootstrap

L’échantillonnage en Python

James Chapman

Curriculum Manager, DataCamp

Avec ou sans

Échantillonnage sans remise :

Des cartes distribuées sur une table de casino.

Échantillonnage avec remise (« rééchantillonnage ») :

Quatre dés lancés.

L’échantillonnage en Python

Échantillonnage aléatoire simple sans remise

Population :

Grains de café en lignes et colonnes.

Échantillon :

Grains de café en lignes et colonnes, dont la plupart sont grisés.

L’échantillonnage en Python

Échantillonnage aléatoire simple avec remise

Population :

Grains de café en lignes et colonnes.

Rééchantillon :

Un échantillon aléatoire de grains de café, avec des doublons.

L’échantillonnage en Python

Pourquoi avec remise ?

  • coffee_ratings : un échantillon d’une population plus large de cafés
  • Chaque café de l’échantillon représente de nombreux cafés hypothétiques de la population
  • L’échantillonnage avec remise sert de proxy
L’échantillonnage en Python

Préparation des données café

coffee_focus = coffee_ratings[["variety", "country_of_origin", "flavor"]]
coffee_focus = coffee_focus.reset_index()
      index  variety country_of_origin  flavor
0         0     None          Ethiopia    8.83
1         1    Other          Ethiopia    8.67
2         2  Bourbon         Guatemala    8.50
3         3     None          Ethiopia    8.58
4         4    Other          Ethiopia    8.50
...     ...      ...               ...     ...
1333   1333     None           Ecuador    7.58
1334   1334     None           Ecuador    7.67
1335   1335     None     United States    7.33
1336   1336     None             India    6.83
1337   1337     None           Vietnam    6.67

[1338 rows x 4 columns]
L’échantillonnage en Python

Rééchantillonnage avec .sample()

coffee_resamp = coffee_focus.sample(frac=1, replace=True)
      index  variety country_of_origin  flavor
1140   1140  Bourbon         Guatemala    7.25
57       57  Bourbon         Guatemala    8.00
1152   1152  Bourbon            Mexico    7.08
621     621  Caturra          Thailand    7.50
44       44     SL28             Kenya    8.08
...     ...      ...               ...     ...
996     996   Typica            Mexico    7.33
1090   1090  Bourbon         Guatemala    7.33
918     918    Other         Guatemala    7.42
249     249  Caturra          Colombia    7.67
467     467  Caturra          Colombia    7.50

[1338 rows x 4 columns]
L’échantillonnage en Python

Cafés répétés

coffee_resamp["index"].value_counts()
658     5
167     4
363     4
357     4
1047    4
       ..
771     1
770     1
766     1
764     1
0       1
Name: index, Length: 868, dtype: int64
L’échantillonnage en Python

Cafés manquants

num_unique_coffees = len(coffee_resamp.drop_duplicates(subset="index"))
868
len(coffee_ratings) - num_unique_coffees
470
L’échantillonnage en Python

Bootstrap

L’inverse de l’échantillonnage depuis une population

Échantillonnage : passer d’une population à un échantillon plus petit

Bootstrap : construire une population théorique à partir de l’échantillon

Cas d’usage du bootstrap :

  • Comprendre la variabilité d’échantillonnage avec un seul échantillon

Une botte de cowboy.

L’échantillonnage en Python

Processus de bootstrap

  1. Créer un rééchantillon de même taille que l’original
  2. Calculer la statistique d’intérêt pour cet échantillon bootstrap
  3. Répéter les étapes 1 et 2 de nombreuses fois

Les statistiques obtenues sont des statistiques bootstrap et forment une distribution bootstrap

L’échantillonnage en Python

Bootstrap de la saveur moyenne du café

import numpy as np

mean_flavors_1000 = []
for i in range(1000):
mean_flavors_1000.append(
np.mean(coffee_sample.sample(frac=1, replace=True)['flavor'])
)
L’échantillonnage en Python

Histogramme de la distribution bootstrap

import matplotlib.pyplot as plt
plt.hist(mean_flavors_1000)
plt.show()

Distribution bootstrap de la saveur moyenne

L’échantillonnage en Python

Passons à la pratique !

L’échantillonnage en Python

Preparing Video For Download...