Skip to content
Snippets Groups Projects
Commit 58aa92db authored by Alexandre Roulois's avatar Alexandre Roulois
Browse files

Exercise on sorted data bias

parent f35a6ede
No related branches found
No related tags found
1 merge request!22Exercise on sorted data bias
%% Cell type:markdown id:411ba7b3-7d56-45fe-b01e-205275e1988a tags: %% Cell type:markdown id:411ba7b3-7d56-45fe-b01e-205275e1988a tags:
# Des biais et des erreurs communes # Des biais et des erreurs communes
%% Cell type:markdown id:4e2fcf4b-d8aa-4bb2-8eab-dfe9a3210604 tags: %% Cell type:markdown id:4e2fcf4b-d8aa-4bb2-8eab-dfe9a3210604 tags:
Les exercices suivants sont destinés à vous familiariser avec les concepts appréhendés lors de l’introduction au *machine learning*. Avant toute chose, importez les librairies utiles : Les exercices suivants sont destinés à vous familiariser avec les concepts appréhendés lors de l’introduction au *machine learning*. Avant toute chose, importez les librairies utiles :
%% Cell type:code id:4bbfb43b-1feb-4366-b1e7-5536f0f5aacd tags: %% Cell type:code id:4bbfb43b-1feb-4366-b1e7-5536f0f5aacd tags:
``` python ``` python
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
import seaborn as sns import seaborn as sns
sns.set_context('notebook') sns.set_context('notebook')
``` ```
%% Cell type:markdown id:220410a9-d71d-4d16-b724-1f31539ed987 tags:
## Une étude de genre
%% Cell type:markdown id:cfc33885-ca65-4f89-8eac-04d519b8c6ab tags:
L’enquête [*Self-Reports of Height and Weight*](../0.about-datasets.ipynb#Self-Reports-of-Height-and-Weight) (Davis, 1990) compare une auto-évaluation de leurs tailles et poids d’individus engagés dans un programme d’exercices avec les mesures réalisées par l’équipe encadrante.
Imaginons un objectif où, en fonction des valeurs renseignées, on souhaiterait déduire l’étiquette *H* ou *F* qui leur est associée. Chargeons dans un premier temps les données et affichons un résumé :
%% Cell type:code id:2f7609ab-f6d7-459a-bdea-cfab3f255332 tags:
``` python
# load data
df = pd.read_csv("../files/davis.csv", sep="\t")
# select variables
target = "sex"
features = ["weight", "height", "repwt", "repht"]
# a copy of the data frame
data = df.copy()
data = data[[target] + features]
data.info()
```
%% Cell type:markdown id:e3479ed2-ec29-4a05-9554-1691a59f3e4d tags:
Le jeu de données est composée de 200 observations mais comme toutes ne sont pas remplies pour tous les champs, il convient dans un premier temps de s’en occuper. Nous retenons comme stratégie de les combler avec la valeur moyenne de la colonne :
%% Cell type:code id:638eaa6f-d30a-45f3-b888-d727eb00ef53 tags:
``` python
# mean value
repwt_mean = int(data.repwt.mean())
repht_mean = int(data.repht.mean())
# fill NA
data.repwt.fillna(repwt_mean, inplace=True)
data.repht.fillna(repht_mean, inplace=True)
data.info()
```
%% Cell type:markdown id:ee52c74a-9e9d-4998-99f6-f370419a7926 tags:
La seconde étape consiste à séparer le *dataset* en deux parties inégales : l’une pour le jeu d’entraînement, constituée de 80 % de l’ensemble ; et l’autre pour le jeu de test.
%% Cell type:code id:cece0234-c72c-4f7f-a0db-4805e0f98f0f tags:
``` python
limit = int(len(data) * 0.2)
# split
train = data[limit:]
test = data[:limit]
```
%% Cell type:markdown id:f3d0c802-2b5f-48bf-a34c-65ad0b30520b tags:
Attachons-nous à étudier le rapport entre le poids et la taille des individus. Intuitivement, on penserait que ces caractéristiques sont globalement liées par une corrélation positive : l’augmentation chez l’une entraîne une augmentation chez l’autre. Si nous affichons une droite de régression sur le jeu de données complet, on observe bien le phénomène attendu :
%% Cell type:code id:312afd57-af0f-4e38-9154-a05c0402715e tags:
``` python
_ = sns.regplot(data=data, x="weight", y="height")
```
%% Cell type:markdown id:84c8a544-351b-4b47-89ba-abfb9f1f031e tags:
Pour autant, il n’en va pas de même avec les jeux d’entraînement et de test :
%% Cell type:code id:cdc0d499-1ee3-4740-9c9d-2d3b4b0b5f90 tags:
``` python
figure, (col_1, col_2)= plt.subplots(1, 2, figsize=(12,4))
sns.regplot(data=train, x="weight", y="height", ax=col_1)
sns.regplot(data=test, x="weight", y="height", ax=col_2)
figure.suptitle("Relation entre le poids et la taille des individus", y=1.05)
col_1.set(title="Jeu d’entraînement")
col_2.set(title="Jeu de test")
sns.despine()
plt.show()
```
%% Cell type:markdown id:c500a2fa-07c5-45f4-a8c7-548abd3d0c9e tags:
À votre avis, quelles erreurs peuvent avoir faussé notre interprétation ?
%% Cell type:markdown id:61c8d84f-a791-425e-ae70-306f0da93a55 tags: %% Cell type:markdown id:61c8d84f-a791-425e-ae70-306f0da93a55 tags:
## Les relations à distance ## Les relations à distance
%% Cell type:markdown id:057d738a-a8a8-4d38-9dd2-b109d1325308 tags: %% Cell type:markdown id:057d738a-a8a8-4d38-9dd2-b109d1325308 tags:
Il paraît que l’univers est en expansion et que cette expansion va en s’accélérant. C’est en tout cas ce que l’étude de Wendy Freedman et al. a prouvé ([*Freedman, 2001*](../0.about-datasets.ipynb#Stellar-Objects)). Par conséquent, on s’attend à ce qu’un objet stellaire s’éloigne d’autant plus vite de nous que la distance qui nous sépare de lui est grande. Il paraît que l’univers est en expansion et que cette expansion va en s’accélérant. C’est en tout cas ce que l’étude de Wendy Freedman et al. a prouvé ([*Freedman, 2001*](../0.about-datasets.ipynb#Stellar-Objects)). Par conséquent, on s’attend à ce qu’un objet stellaire s’éloigne d’autant plus vite de nous que la distance qui nous sépare de lui est grande.
Chargeons le jeu de données en se concentrant sur des objets proches de nous (entre 30 000 et 100 000 années-lumières) : Chargeons le jeu de données en se concentrant sur des objets proches de nous (entre 30 000 et 100 000 années-lumières) :
%% Cell type:code id:1cf3ab56-418f-46e3-bc3f-36cf0eec0dbf tags: %% Cell type:code id:1cf3ab56-418f-46e3-bc3f-36cf0eec0dbf tags:
``` python ``` python
# load data # load data
df = pd.read_csv("../files/stellar-objects.csv", sep="\t") df = pd.read_csv("../files/stellar-objects.csv", sep="\t")
# distance: megaparsec (MPC) # distance: megaparsec (MPC)
# velocity: in km/s # velocity: in km/s
df["velocity"] = df.v_helio.fillna(df.v_flow.fillna(df.v_cmb)) df["velocity"] = df.v_helio.fillna(df.v_flow.fillna(df.v_cmb))
# objects close to earth, but not that close :) # objects close to earth, but not that close :)
data = df[(df.distance > 10) & (df.distance < 30)] data = df[(df.distance > 10) & (df.distance < 30)]
``` ```
%% Cell type:markdown id:f0a306e1-be3e-4431-84a3-32216340c326 tags: %% Cell type:markdown id:f0a306e1-be3e-4431-84a3-32216340c326 tags:
Affichons un nuage de points afin de vérifier la proposition de ces pontes de la NASA : Affichons un nuage de points afin de vérifier la proposition de ces pontes de la NASA :
%% Cell type:code id:1fb0d73f-62bd-4777-b4e4-276554e2a599 tags: %% Cell type:code id:1fb0d73f-62bd-4777-b4e4-276554e2a599 tags:
``` python ``` python
sns.scatterplot(data=data, x="distance", y="velocity") sns.scatterplot(data=data, x="distance", y="velocity")
sns.despine() sns.despine()
plt.title("Relation between distance and velocity of stellar objects") plt.title("Relation between distance and velocity of stellar objects")
plt.xlabel("Distance (MPC)") plt.xlabel("Distance (MPC)")
plt.ylabel("Velocity (km/s)") plt.ylabel("Velocity (km/s)")
plt.show() plt.show()
``` ```
%% Cell type:markdown id:fbb20849-4a22-4870-940b-8067fd06e548 tags: %% Cell type:markdown id:fbb20849-4a22-4870-940b-8067fd06e548 tags:
Rien de bien concluant à première vue, non ? Afin de déterminer visuellement s’il existe bien une relation linéaire entre la distance et la vitesse d’éloignement, affichez une droite de régression : Euh… rien de bien concluant à première vue, non ? Afin de déterminer visuellement s’il existe bien une relation linéaire entre la distance et la vitesse d’éloignement, affichez une droite de régression :
%% Cell type:code id:125c4241-faf9-4209-b8c6-cfc2c1b07105 tags: %% Cell type:code id:125c4241-faf9-4209-b8c6-cfc2c1b07105 tags:
``` python ``` python
# your code here # your code here
_ = sns.regplot(data=data, x="distance", y="velocity") _ = sns.regplot(data=data, x="distance", y="velocity")
``` ```
%% Cell type:markdown id:aa3c4eeb-5ce4-44f2-9403-9d50a9e425e9 tags: %% Cell type:markdown id:aa3c4eeb-5ce4-44f2-9403-9d50a9e425e9 tags:
Bon, appelez BFM TV, Wendy s’est trompée : 2/3 des points sont en dehors de l’intervalle de confiance à 95 %. Ou alors, peut-être avons-nous fait une erreur de méthodologie ? Bon, appelez BFM TV, Wendy s’est trompée : 2/3 des points sont en dehors de l’intervalle de confiance à 95 %. Ou alors, peut-être avons-nous fait une erreur de méthodologie ?
......
"","sex","weight","height","repwt","repht" sex weight height repwt repht
"1","M",77,182,77,180 1 F 166 57 56 163
"2","F",58,161,51,159 2 F 50 148 47 148
"3","F",53,161,54,158 3 F 47 150 45 152
"4","M",68,177,70,175 4 F 52 152 51 150
"5","F",59,157,59,155 5 F 47 153 NA 154
"6","M",76,170,76,165 6 F 43 154 NA NA
"7","M",76,167,77,165 7 F 55 155 NA 154
"8","M",69,186,73,180 8 F 51 156 51 158
"9","M",71,178,71,175 9 F 59 157 59 155
"10","M",65,171,64,170 10 F 39 157 41 153
"11","M",70,175,75,174 11 F 45 157 45 153
"12","F",166,57,56,163 12 F 44 157 44 155
"13","F",51,161,52,158 13 F 59 157 55 158
"14","F",64,168,64,165 14 F 52 158 51 155
"15","F",52,163,57,160 15 F 53 158 50 155
"16","F",65,166,66,165 16 F 50 158 49 155
"17","M",92,187,101,185 17 F 52 159 52 153
"18","F",62,168,62,165 18 F 59 159 59 155
"19","M",76,197,75,200 19 F 54 160 55 158
"20","F",61,175,61,171 20 F 50 160 55 150
"21","M",119,180,124,178 21 F 63 160 64 158
"22","F",61,170,61,170 22 F 56 160 53 158
"23","M",65,175,66,173 23 F 55 160 55 155
"24","M",66,173,70,170 24 F 58 161 51 159
"25","F",54,171,59,168 25 F 53 161 54 158
"26","F",50,166,50,165 26 F 51 161 52 158
"27","F",63,169,61,168 27 F 49 161 NA NA
"28","F",58,166,60,160 28 F 56 161 56 161
"29","F",39,157,41,153 29 F 54 161 54 160
"30","M",101,183,100,180 30 F 75 162 75 158
"31","F",71,166,71,165 31 F 60 162 59 160
"32","M",75,178,73,175 32 F 57 162 56 160
"33","M",79,173,76,173 33 F 53 162 53 160
"34","F",52,164,52,161 34 F 47 162 47 160
"35","F",68,169,63,170 35 F 55 162 NA NA
"36","M",64,176,65,175 36 F 53 162 52 158
"37","F",56,166,54,165 37 F 56 162 56 160
"38","M",69,174,69,171 38 F 52 163 57 160
"39","M",88,178,86,175 39 F 63 163 59 159
"40","M",65,187,67,188 40 F 56 163 57 159
"41","F",54,164,53,160 41 F 57 163 59 160
"42","M",80,178,80,178 42 F 47 163 47 160
"43","F",63,163,59,159 43 F 45 163 45 160
"44","M",78,183,80,180 44 F 52 163 53 160
"45","M",85,179,82,175 45 F 48 163 44 160
"46","F",54,160,55,158 46 F 51 163 50 160
"47","M",73,180,NA,NA 47 F 54 163 NA NA
"48","F",49,161,NA,NA 48 M 56 163 58 161
"49","F",54,174,56,173 49 F 52 164 52 161
"50","F",75,162,75,158 50 F 54 164 53 160
"51","M",82,182,85,183 51 F 64 164 62 161
"52","F",56,165,57,163 52 F 55 164 55 163
"53","M",74,169,73,170 53 F 53 164 51 160
"54","M",102,185,107,185 54 F 59 164 59 165
"55","M",64,177,NA,NA 55 F 62 164 61 161
"56","M",65,176,64,172 56 F 56 165 57 163
"57","F",66,170,65,NA 57 F 64 165 63 163
"58","M",73,183,74,180 58 F 55 165 54 163
"59","M",75,172,70,169 59 F 61 165 60 163
"60","M",57,173,58,170 60 F 53 165 53 165
"61","M",68,165,69,165 61 F 56 165 57 160
"62","M",71,177,71,170 62 F 55 165 55 163
"63","M",71,180,76,175 63 F 53 165 55 163
"64","F",78,173,75,169 64 F 55 165 55 165
"65","M",97,189,98,185 65 F 63 165 59 160
"66","F",60,162,59,160 66 M 68 165 69 165
"67","F",64,165,63,163 67 F 65 166 66 165
"68","F",64,164,62,161 68 F 50 166 50 165
"69","F",52,158,51,155 69 F 58 166 60 160
"70","M",80,178,76,175 70 F 71 166 71 165
"71","F",62,175,61,171 71 F 56 166 54 165
"72","M",66,173,66,175 72 F 50 166 50 161
"73","F",55,165,54,163 73 F 64 166 64 165
"74","F",56,163,57,159 74 F 59 166 55 163
"75","F",50,166,50,161 75 F 62 166 61 163
"76","F",50,171,NA,NA 76 F 66 166 66 165
"77","F",50,160,55,150 77 F 60 167 55 163
"78","F",63,160,64,158 78 F 57 167 55 164
"79","M",69,182,70,180 79 F 76 167 77 165
"80","M",69,183,70,183 80 F 62 167 NA NA
"81","F",61,165,60,163 81 F 57 167 56 165
"82","M",55,168,56,170 82 M 76 167 77 165
"83","F",53,169,52,175 83 M 69 167 73 165
"84","F",60,167,55,163 84 F 64 168 64 165
"85","F",56,170,56,170 85 F 62 168 62 165
"86","M",59,182,61,183 86 F 62 168 62 163
"87","M",62,178,66,175 87 F 57 168 58 165
"88","F",53,165,53,165 88 M 55 168 56 170
"89","F",57,163,59,160 89 M 62 168 64 168
"90","F",57,162,56,160 90 F 63 169 61 168
"91","M",70,173,68,170 91 F 68 169 63 170
"92","F",56,161,56,161 92 F 53 169 52 175
"93","M",84,184,86,183 93 F 50 169 50 165
"94","M",69,180,71,180 94 F 52 169 56 NA
"95","M",88,189,87,185 95 F 58 169 NA NA
"96","F",56,165,57,160 96 F 58 169 54 166
"97","M",103,185,101,182 97 M 74 169 73 170
"98","F",50,169,50,165 98 M 75 169 76 165
"99","F",52,159,52,153 99 M 54 169 58 165
"100","F",55,155,NA,154 100 M 76 169 75 165
"101","F",55,164,55,163 101 F 61 170 61 170
"102","M",63,178,63,175 102 F 66 170 65 NA
"103","F",47,163,47,160 103 F 56 170 56 170
"104","F",45,163,45,160 104 F 59 170 NA NA
"105","F",62,175,63,173 105 F 63 170 62 168
"106","F",53,164,51,160 106 M 76 170 76 165
"107","F",52,152,51,150 107 M 61 170 61 170
"108","F",57,167,55,164 108 M 66 170 67 165
"109","F",64,166,64,165 109 F 54 171 59 168
"110","F",59,166,55,163 110 F 50 171 NA NA
"111","M",84,183,90,183 111 F 68 171 68 169
"112","M",79,179,79,171 112 F 64 171 66 171
"113","F",55,174,57,171 113 M 65 171 64 170
"114","M",67,179,67,179 114 F 64 172 62 168
"115","F",76,167,77,165 115 F 59 172 58 171
"116","F",62,168,62,163 116 F 60 172 55 168
"117","M",83,184,83,181 117 M 75 172 70 169
"118","M",96,184,94,183 118 M 69 172 68 174
"119","M",75,169,76,165 119 F 78 173 75 169
"120","M",65,178,66,178 120 F 70 173 67 170
"121","M",78,178,77,175 121 M 66 173 70 170
"122","M",69,167,73,165 122 M 79 173 76 173
"123","F",68,178,68,175 123 M 57 173 58 170
"124","F",55,165,55,163 124 M 66 173 66 175
"125","M",67,179,NA,NA 125 M 70 173 68 170
"126","F",52,169,56,NA 126 M 70 173 70 173
"127","F",47,153,NA,154 127 M 89 173 86 173
"128","F",45,157,45,153 128 F 54 174 56 173
"129","F",68,171,68,169 129 F 55 174 57 171
"130","F",44,157,44,155 130 F 60 174 NA NA
"131","F",62,166,61,163 131 M 69 174 69 171
"132","M",87,185,89,185 132 M 68 174 68 173
"133","F",56,160,53,158 133 F 61 175 61 171
"134","F",50,148,47,148 134 F 62 175 61 171
"135","M",83,177,84,175 135 F 62 175 63 173
"136","F",53,162,53,160 136 M 70 175 75 174
"137","F",64,172,62,168 137 M 65 175 66 173
"138","F",62,167,NA,NA 138 M 66 175 68 175
"139","M",90,188,91,185 139 M 74 175 71 175
"140","M",85,191,83,188 140 M 81 175 NA NA
"141","M",66,175,68,175 141 F 54 176 55 176
"142","F",52,163,53,160 142 M 64 176 65 175
"143","F",53,165,55,163 143 M 65 176 64 172
"144","F",54,176,55,176 144 M 82 176 NA NA
"145","F",64,171,66,171 145 M 80 176 78 175
"146","F",55,160,55,155 146 M 68 177 70 175
"147","F",55,165,55,165 147 M 64 177 NA NA
"148","F",59,157,55,158 148 M 71 177 71 170
"149","F",70,173,67,170 149 M 83 177 84 175
"150","M",88,184,86,183 150 M 79 177 81 178
"151","F",57,168,58,165 151 F 68 178 68 175
"152","F",47,162,47,160 152 M 71 178 71 175
"153","F",47,150,45,152 153 M 75 178 73 175
"154","F",55,162,NA,NA 154 M 88 178 86 175
"155","F",48,163,44,160 155 M 80 178 80 178
"156","M",54,169,58,165 156 M 80 178 76 175
"157","M",69,172,68,174 157 M 62 178 66 175
"158","F",59,170,NA,NA 158 M 63 178 63 175
"159","F",58,169,NA,NA 159 M 65 178 66 178
"160","F",57,167,56,165 160 M 78 178 77 175
"161","F",51,163,50,160 161 M 71 178 68 178
"162","F",54,161,54,160 162 M 81 178 82 175
"163","F",53,162,52,158 163 M 85 179 82 175
"164","F",59,172,58,171 164 M 79 179 79 171
"165","M",56,163,58,161 165 M 67 179 67 179
"166","F",59,159,59,155 166 M 67 179 NA NA
"167","F",63,170,62,168 167 M 119 180 124 178
"168","F",66,166,66,165 168 M 73 180 NA NA
"169","M",96,191,95,188 169 M 71 180 76 175
"170","F",53,158,50,155 170 M 69 180 71 180
"171","M",76,169,75,165 171 M 83 180 80 180
"172","F",54,163,NA,NA 172 M 82 181 NA NA
"173","M",61,170,61,170 173 M 90 181 91 178
"174","M",82,176,NA,NA 174 M 77 182 77 180
"175","M",62,168,64,168 175 M 82 182 85 183
"176","M",71,178,68,178 176 M 69 182 70 180
"177","F",60,174,NA,NA 177 M 59 182 61 183
"178","M",66,170,67,165 178 M 101 183 100 180
"179","M",81,178,82,175 179 M 78 183 80 180
"180","M",68,174,68,173 180 M 73 183 74 180
"181","M",80,176,78,175 181 M 69 183 70 183
"182","F",43,154,NA,NA 182 M 84 183 90 183
"183","M",82,181,NA,NA 183 M 76 183 75 180
"184","F",63,165,59,160 184 M 84 184 86 183
"185","M",70,173,70,173 185 M 83 184 83 181
"186","F",56,162,56,160 186 M 96 184 94 183
"187","F",60,172,55,168 187 M 88 184 86 183
"188","F",58,169,54,166 188 M 102 185 107 185
"189","M",76,183,75,180 189 M 103 185 101 182
"190","F",50,158,49,155 190 M 87 185 89 185
"191","M",88,185,93,188 191 M 88 185 93 188
"192","M",89,173,86,173 192 M 69 186 73 180
"193","F",59,164,59,165 193 M 92 187 101 185
"194","F",51,156,51,158 194 M 65 187 67 188
"195","F",62,164,61,161 195 M 90 188 91 185
"196","M",74,175,71,175 196 M 97 189 98 185
"197","M",83,180,80,180 197 M 88 189 87 185
"198","M",81,175,NA,NA 198 M 85 191 83 188
"199","M",90,181,91,178 199 M 96 191 95 188
"200","M",79,177,81,178 200 M 76 197 75 200
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment