Valutare un modello su train e test
La funzione auc_train_test calcola l’AUC di un modello costruito su un set di train e valutato su un set di test:
auc_train, auc_test = auc_train_test(variables, target, train, test)
con variables una lista dei nomi delle variabili utilizzate nel modello.
In questo esercizio userai questa funzione e verificherai se l’AUC su train e test sono simili.
Questo esercizio fa parte del corso
Introduzione alla Predictive Analytics in Python
Istruzioni dell'esercizio
- Il
basetableè già caricato. Suddividi il basetable in modo che il train set contenga il 70% dei dati e assicurati che train e test abbiano la stessa incidenza del target. - Calcola l’AUC su train e test del modello usando
"age"e"gender_F"come predittori con la funzioneauc_train_test.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Load the partitioning module
from sklearn.model_selection import train_test_split
# Create DataFrames with variables and target
X = basetable.drop('target', 1)
y = basetable["target"]
# Carry out 70-30 partititioning with stratification
X_train, X_test, y_train, y_test = ____(X, y, test_size = ____, stratify = ____)
# Create the final train and test basetables
train = pd.concat([X_train, y_train], axis=1)
test = pd.concat([X_test, y_test], axis=1)
# Apply the auc_train_test function
auc_train, auc_test = ____([____, ____], ["target"], ____, ____)
print(round(auc_train,2))
print(round(auc_test,2))