Evaluar un modelo en test y train
La función auc_train_test calcula el AUC de un modelo que se construye con un conjunto train y se evalúa con un conjunto test:
auc_train, auc_test = auc_train_test(variables, target, train, test)
donde variables es una lista con los nombres de las variables utilizadas en el modelo.
En este ejercicio vas a aplicar esta función y comprobar si el AUC de train y de test son similares.
Este ejercicio forma parte del curso
Introducción al análisis predictivo en Python
Instrucciones del ejercicio
- El
basetableestá cargado. Divide la tabla para que el conjunto de entrenamiento contenga el 70% de los datos y asegúrate de que los conjuntos de train y test tengan la misma incidencia del target. - Calcula el AUC de train y de test del modelo usando
"age"y"gender_F"como predictores con la funciónauc_train_test.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# 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))