Selecting the best model

In this unit we compare the three model families we trained in this module - decision tree, random forest and XGBoost - pick the winner, and train the final model on the full training data to check it on the test set.

Comparing the three models

Each family was tuned to its best parameters in the previous units, so we train all three on the training data and compare their AUC on the same validation set.

Comparing the three tuned models on the same validation set

The tuned decision tree - max_depth=6 and min_samples_leaf=15 - gives:

dt = DecisionTreeClassifier(max_depth=6, min_samples_leaf=15)
dt.fit(X_train, y_train)
y_pred = dt.predict_proba(X_val)[:, 1]
roc_auc_score(y_val, y_pred)
0.7850802838390931

The tuned random forest - 200 trees with max_depth=10 and min_samples_leaf=3 - gives:

rf = RandomForestClassifier(n_estimators=200,
                            max_depth=10,
                            min_samples_leaf=3,
                            random_state=1)
rf.fit(X_train, y_train)
y_pred = rf.predict_proba(X_val)[:, 1]
roc_auc_score(y_val, y_pred)
0.8249709379767989

And the tuned XGBoost model - eta=0.1, max_depth=3, min_child_weight=1, 175 rounds - gives:

xgb_params = {
    'eta': 0.1,
    'max_depth': 3,
    'min_child_weight': 1,

    'objective': 'binary:logistic',
    'eval_metric': 'auc',

    'nthread': 8,
    'seed': 1,
    'verbosity': 1,
}

model = xgb.train(xgb_params, dtrain, num_boost_round=175)
y_pred = model.predict(dval)
roc_auc_score(y_val, y_pred)
0.8360387251459157

XGBoost wins with an AUC of about 0.836, ahead of random forest at about 0.825 and the single decision tree at about 0.785. This is a typical outcome: boosted trees are very strong on tabular data. The price is that XGBoost has many hyperparameters and can overfit quickly, so it needs more attention during tuning than random forest, which works well almost out of the box.

Training the final model

Now that we have selected the best model and the best parameters, we use the standard final-step recipe: combine the training and validation parts into df_full_train, retrain with the same parameters, and evaluate once on the test set - the data we have kept aside until now:

df_full_train = df_full_train.reset_index(drop=True)

y_full_train = (df_full_train.status == 'default').astype(int).values
del df_full_train['status']

We turn both df_full_train and the test set into feature matrices with a newly fitted DictVectorizer, and wrap them in DMatrix objects - note that the test DMatrix needs no labels:

dicts_full_train = df_full_train.to_dict(orient='records')

dv = DictVectorizer(sparse=False)
X_full_train = dv.fit_transform(dicts_full_train)

dicts_test = df_test.to_dict(orient='records')
X_test = dv.transform(dicts_test)
dfulltrain = xgb.DMatrix(X_full_train, label=y_full_train,
                    feature_names=dv.get_feature_names_out())

dtest = xgb.DMatrix(X_test, feature_names=dv.get_feature_names_out())

Then we train the final model and look at its AUC on the test data:

xgb_params = {
    'eta': 0.1,
    'max_depth': 3,
    'min_child_weight': 1,

    'objective': 'binary:logistic',
    'eval_metric': 'auc',

    'nthread': 8,
    'seed': 1,
    'verbosity': 1,
}

model = xgb.train(xgb_params, dfulltrain, num_boost_round=175)
y_pred = model.predict(dtest)
roc_auc_score(y_test, y_pred)
0.8322662626460096

The test AUC is about 0.832, close to the 0.836 we saw on validation. There is no big gap between the two, which means the model generalized well: it learned patterns that hold on data it has never seen.

Notes

We select the final model from decision tree, random forest, or xgboost based on the best auc scores. After that we prepare the df_full_train and df_test to train and evaluate the final model. If there is not much difference between model auc scores on the train as well as test data then the model has generalized the patterns well enough.

Generally, XGBoost models perform better on tabular data than other machine learning models but the downside is that these model are easy to overfit cause of the high number of hyperparameter. Therefore, XGBoost models require a lot more attention for parameters tuning to optimize them.

← Previous: XGBoost parameter tuning · Next: Summary →

Optional analytics

Optional analytics are off unless you allow them. Your choice does not affect access to DataTalks.Club, Slack, events, or courses.

Current choice: not chosen.

Privacy Policy

If you allow analytics, self-hosted Umami counts anonymous public pageviews without analytics cookies. Private account and learning pages are excluded. Read the Privacy Policy for the complete data boundary.