How to do it...

We will now look at how to use a random forest to train our model:

  1. We start by splitting our target and feature variables:
predictor= df_creditcarddata.iloc[:, df_creditcarddata.columns != 'default.payment.next.month']target= df_creditcarddata.iloc[:, df_creditcarddata.columns == 'default.payment.next.month']
  1. We separate the numerical and non-numerical variables in our feature set:
# save all categorical columns in listcategorical_columns = [col for col in predictor.columns.values if predictor[col].dtype == 'object']# dataframe with categorical featuresdf_categorical = predictor[categorical_columns]# dataframe with numerical featuresdf_numeric = predictor.drop(categorical_columns, axis=1)
  1. We dummy code the categorical ...

Get Ensemble Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.