Get startedGet started for free

Extracting a Random Forest parameter

You will now translate the work previously undertaken on the logistic regression model to a random forest model. A parameter of this model is, for a given tree, how it decided to split at each level.

This analysis is not as useful as the coefficients of logistic regression as you will be unlikely to ever explore every split and every tree in a random forest model. However, it is a very useful exercise to peek under the hood at what the model is doing.

In this exercise we will extract a single tree from our random forest model, visualize it and programmatically extract one of the splits.

You have available:

  • A random forest model object, rf_clf
  • An image of the top of the chosen decision tree, tree_viz_image
  • The X_train DataFrame & the original_variables list

This exercise is part of the course

Hyperparameter Tuning in Python

View Course

Exercise instructions

  • Extract the 7th tree (6th index) from the random forest model.
  • Visualize this tree (tree_viz_image) to see the split decisions.
  • Extract the feature & level of the top split.
  • Print out the feature and level together.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Extract the 7th (index 6) tree from the random forest
chosen_tree = rf_clf.estimators_[____]

# Visualize the graph using the provided image
imgplot = plt.imshow(____)
plt.show()

# Extract the parameters and level of the top (index 0) node
split_column = chosen_tree.tree_.feature[____]
split_column_name = X_train.columns[split_column]
split_value = chosen_tree.tree_.threshold[____]

# Print out the feature and level
print("This node split on feature {}, at a value of {}".format(split_column_name, ____))
Edit and Run Code