everyone. I'm a noob in data science and I'm building my first ML model. But I keep getting this error,

'DecisionTreeRegressor' object has no attribute 'tree_'

This is my code below.

df = pd.read_csv('/Users/mog/Downloads/train_data.csv')

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 1)

df.model = DecisionTreeRegressor()
df.model.fit(x_train, y_train)

from sklearn.metrics import accuracy_score

print('Training score...', accuracy_score(y_train, df_model.predict(x_train)))
print('Test score...', accuracy_score(y_test, df_model.predict(x_test)))

Please help.


1 Answer 1


The problem is that you are trying to estimate a classification metric for a regression model.

So, depending on your target you have two options:

  1. If it is categorical you need to use DecisionTreeClassifier instead of DecisionTreeRegressor

  2. If it is continuous, you need to change the metric accuracy_score for example for r2_score

I also noticed you named your model df.model but when predicting this is named df_model. So I recommend to change both to df_model

Hope it helps!

  • $\begingroup$ This helped a lot. Thank you!!! $\endgroup$ Apr 1, 2022 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.