Ahmad Anis
Ahmad Anis

Reputation: 2704

AttributeError: 'RandomForestRegressor' object has no attribute 'coef_'

I have used Random Forest Regressor to solve a Regression Problem, Now I want to plot regression Line, According to this answer, I am trying this.

w =  model1.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (model1.intercept_[0]) / w[1]

plt.plot(xx, yy, 'k-')

Where model1 is sklearn.ensemble.RandomForestRegressor which is already fit on data set. What are some alternatives.

The Error Message is

AttributeError: 'RandomForestRegressor' object has no attribute 'coef_'

Upvotes: 0

Views: 4657

Answers (1)

mujjiga
mujjiga

Reputation: 16856

You will have coef_ and intercept_ when the model fits a hyperplane. Linear regression is one such model which fits a hyperplane along the train data such that the deviation/error is minimal. These coef_ and intercept_ represents the hyperplane.

However, models like Random Forest do not fit a hyperplane but instead identify a set of decisions based on the input which finally lead to the prediction. You can think of them as a set of nested if else conditions. So, if your model is a Random forest based then there is no concept of coef_ and intercept_ but what you can rather do is to print the decision tree.

Upvotes: 1

Related Questions