Trees for defaults
You will now train a gradient boosted tree model on the credit data, and see a sample of some of the predictions. Do you remember when you first looked at the predictions of the logistic regression model? They didn't look good. Do you think this model be different?
The credit data cr_loan_prep
, the training sets X_train
and y_train
, and the test data X_test
is available in the workspace. The XGBoost package is loaded as xgb
.
This exercise is part of the course
Credit Risk Modeling in Python
Exercise instructions
- Create and train a gradient boosted tree using
XGBClassifier()
and name itclf_gbt
. - Predict probabilities of default on the test data and store the results in
gbt_preds
. - Create two data frames,
preds_df
andtrue_df
, to store the first five predictions and trueloan_status
values. - Concatenate and print the data frames
true_df
andpreds_df
in order, and check the model's results.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Train a model
import xgboost as xgb
____ = xgb.____().fit(____, np.ravel(____))
# Predict with a model
____ = clf_gbt.____(____)
# Create dataframes of first five predictions, and first five true labels
____ = pd.DataFrame(____[:,1][0:5], columns = ['prob_default'])
____ = y_test.____()
# Concatenate and print the two data frames for comparison
print(pd.____([____.reset_index(drop = True), ____], axis = 1))