Nous allons travailler sur un jeu de données Adult
, dont toutes les informations sont sur cette page. Celui-ci provient d'une enquête auprès d'états-uniens, sur leur salaire (binaire - plus ou moins de 50K$) et un certain nombres de critères (âge, sexe, CSP, niveau d'études, ...)
Pour l'importer, il vous faire les 2 opérations suivantes :
ucimlrepo
from ucimlrepo import fetch_ucirepo
# fetch dataset
adult = fetch_ucirepo(id = 2)
# data (as pandas dataframes)
X = adult.data.features
y = adult.data.targets
X.head()
age | workclass | fnlwgt | education | education-num | marital-status | occupation | relationship | race | sex | capital-gain | capital-loss | hours-per-week | native-country | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 39 | State-gov | 77516 | Bachelors | 13 | Never-married | Adm-clerical | Not-in-family | White | Male | 2174 | 0 | 40 | United-States |
1 | 50 | Self-emp-not-inc | 83311 | Bachelors | 13 | Married-civ-spouse | Exec-managerial | Husband | White | Male | 0 | 0 | 13 | United-States |
2 | 38 | Private | 215646 | HS-grad | 9 | Divorced | Handlers-cleaners | Not-in-family | White | Male | 0 | 0 | 40 | United-States |
3 | 53 | Private | 234721 | 11th | 7 | Married-civ-spouse | Handlers-cleaners | Husband | Black | Male | 0 | 0 | 40 | United-States |
4 | 28 | Private | 338409 | Bachelors | 13 | Married-civ-spouse | Prof-specialty | Wife | Black | Female | 0 | 0 | 40 | Cuba |
y.head()
income | |
---|---|
0 | <=50K |
1 | <=50K |
2 | <=50K |
3 | <=50K |
4 | <=50K |
On remarque qu'il semble y avoir un problème d'importation... Un "." étant présent sur certaines modalités.
import pandas
pandas.crosstab(y.income, "Nb")
col_0 | Nb |
---|---|
income | |
<=50K | 24720 |
<=50K. | 12435 |
>50K | 7841 |
>50K. | 3846 |
y = y.assign(income = y.income.str.replace(".", ""))
pandas.crosstab(y.income, "Nb")
col_0 | Nb |
---|---|
income | |
<=50K | 37155 |
>50K | 11687 |
Il est important, dans un cadre supervisé, de créer 2 jeux de données (un d'apprentissage et un de test) pour paramètrer les méthodes, dans un rapport 2 tiers / 1 tiers (voire jusque 80% - 20%). Cela permet d'éviter le sur-apprentissage particulièrement. On peut même envisager de découper en 3 jeux de données, le dernier étant de validation pour avir une estimation de la performance des modèles sur des données nouvelles.
On va donc créer un jeu d'apprentissage avec 75% des réponses, et un jeu de données test avec 25%.
X_train = X.sample(frac = .75)
X_test = X.drop(X_train.index)
y_train = y.iloc[X_train.index]
y_test = y.iloc[X_test.index]
X_train.head()
age | workclass | fnlwgt | education | education-num | marital-status | occupation | relationship | race | sex | capital-gain | capital-loss | hours-per-week | native-country | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
29451 | 32 | Self-emp-not-inc | 62272 | HS-grad | 9 | Divorced | Exec-managerial | Unmarried | White | Female | 0 | 0 | 40 | United-States |
43497 | 39 | Self-emp-inc | 172927 | Assoc-acdm | 12 | Married-civ-spouse | Prof-specialty | Husband | White | Male | 15024 | 0 | 50 | United-States |
8220 | 48 | Private | 125120 | Some-college | 10 | Married-civ-spouse | Adm-clerical | Wife | White | Female | 0 | 0 | 37 | United-States |
11381 | 56 | State-gov | 175057 | Bachelors | 13 | Married-civ-spouse | Exec-managerial | Husband | White | Male | 0 | 0 | 40 | United-States |
38963 | 81 | Self-emp-inc | 104443 | HS-grad | 9 | Widowed | Sales | Not-in-family | White | Female | 0 | 0 | 40 | NaN |
y_train.head()
income | |
---|---|
29451 | <=50K |
43497 | >50K |
8220 | <=50K |
11381 | <=50K |
38963 | <=50K |
X_test.head()
age | workclass | fnlwgt | education | education-num | marital-status | occupation | relationship | race | sex | capital-gain | capital-loss | hours-per-week | native-country | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2 | 38 | Private | 215646 | HS-grad | 9 | Divorced | Handlers-cleaners | Not-in-family | White | Male | 0 | 0 | 40 | United-States |
4 | 28 | Private | 338409 | Bachelors | 13 | Married-civ-spouse | Prof-specialty | Wife | Black | Female | 0 | 0 | 40 | Cuba |
8 | 31 | Private | 45781 | Masters | 14 | Never-married | Prof-specialty | Not-in-family | White | Female | 14084 | 0 | 50 | United-States |
9 | 42 | Private | 159449 | Bachelors | 13 | Married-civ-spouse | Exec-managerial | Husband | White | Male | 5178 | 0 | 40 | United-States |
16 | 25 | Self-emp-not-inc | 176756 | HS-grad | 9 | Never-married | Farming-fishing | Own-child | White | Male | 0 | 0 | 35 | United-States |
y_test.head()
income | |
---|---|
2 | <=50K |
4 | <=50K |
8 | >50K |
9 | >50K |
16 | <=50K |
A partir de maintenant, nous allons donc créer nos modèles sur le jeu train
et chercher nos paramètres avec le jeu test
.
test
à utiliser avec les fonctions predict()
et predict_proba()
test
à utiliser pour l'élagage (fonction cost_complexity_pruning_path()
à appliquer sur les 2 - train
et test
)test
à utiliser avec les fonctions predict()
et predict_proba()