AttributeError: 'RandomForestRegressor' object has no attribute 'tree_'. How do i resolve?

32 views Asked by At

I am trying to use the random forest model to predict social media ads effects based on age and estimated salary, this is my code but i keep getting Attribute error prompting up.

from sklearn.tree import export_graphviz
from IPython import display
from sklearn.ensemble import RandomForestRegressor

m = RandomForestRegressor(n_estimators=5, max_depth=3, bootstrap=False, n_jobs=-1)
m.fit(x_train, y_train)

# data = data.drop('Purchased', axis=1)

str_tree = export_graphviz(m, 
                          out_file=None,
                          feature_names=data.columns[2:4], 
                          filled=True,
                          special_characters=True,
                          rotate=True,
                          precision=1)

display.display(str_tree)

This is the error i keep getting. Despite the fact that i didn't call the tree_ parameter.

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\tree\_export.py:465, in _DOTTreeExporter.export(self, decision_tree)
    463     self.recurse(decision_tree, 0, criterion="impurity")
    464 else:
--> 465     self.recurse(decision_tree.tree_, 0, criterion=decision_tree.criterion)
    467 self.tail()

AttributeError: 'RandomForestRegressor' object has no attribute 'tree_'
1

There are 1 answers

1
Muhammed Yunus On

graphviz works with individual trees. You can access the fitted decision trees of the random forest via its .estimators_ attribute: fitted_trees = random_forest.estimators_.

For displaying, I think you'll also need to run sudo apt install graphviz and use graphviz.Source(dot_string). I've included a few options for displaying the graph. By default, the graph can be very wide, so I prefer to handle its size before rendering the plot.

enter image description here

Example with sample data:

from sklearn.tree import export_graphviz
from IPython import display
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor

#
#Create some test data
#
from sklearn.datasets import make_regression
import pandas as pd
import numpy as np

np.random.seed(0)
x_train, y_train = make_regression(n_samples=1000, noise=1)
data = pd.DataFrame(data=x_train, columns=[f'feature_{i}' for i in range(x_train.shape[1])])

#
# Fit and view
#
import graphviz #also install: sudo apt install graphviz

rf = RandomForestRegressor(n_estimators=5, max_depth=2, max_features='sqrt', n_jobs=-1)
rf.fit(x_train, y_train)

for fitted_tree in rf.estimators_:
    str_tree = export_graphviz(
        fitted_tree, 
        out_file=None,
        feature_names=data.columns, 
        filled=True,
        special_characters=True,
        rotate=True,
        precision=1
    )
    
    #Display as png - results in better figure size than default format
    # display.display_png(graphviz.Source(str_tree))
    
    #Alternatively, resize figure and display
    g = graphviz.Source(str_tree)
    g.render('./_temp_tree', format='png')
    fig = plt.figure(figsize=(6, 3), dpi=100)
    fig.add_subplot().imshow(plt.imread('./_temp_tree.png'))
    fig.axes[0].axis('off')
    
    #Or view full size in default graphics format
    # display( graphviz.Source(str_tree) )