stone rock
stone rock

Reputation: 1953

I am getting Not Fitted error in random forest classifier?

I have 4 features and one target variable. I am using RandomForestRegressor instead of RandomForestClassifer as my target variable is float. When I am trying to fit my model and then output them in sorted order to get the important features I am getting Not fitted error how to fix it?

Code:

import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn import datasets
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectFromModel
from sklearn.metrics import accuracy_score

# Split the data into 30% test and 70% training
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
feat_labels = data.columns[:4]

regr = RandomForestRegressor(max_depth=2, random_state=0)
#clf = RandomForestClassifier(n_estimators=100, random_state=0)

# Train the classifier
#clf.fit(X_train, y_train)
regr.fit(X, y)

importances = clf.feature_importances_
indices = np.argsort(importances)[::-1]

for f in range(X_train.shape[1]):
    print("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[f]], importances[indices[f]]))

enter image description here

Upvotes: 0

Views: 1217

Answers (2)

camstu
camstu

Reputation: 51

I noticed that previously your classifier was being fit with the training data you setup, but the regressor is now being fit with X and y.

However, I don't see here where you're setting X and y in the first place or even more where you actually load in a dataset. Could it be you forgot this step as well as what Harpal mentioned in another answer?

Upvotes: 1

Harpal
Harpal

Reputation: 12587

You are fitting to regr but calling the feature importances on clf. Try calling this instead:

importances = regr.feature_importances_

Upvotes: 4

Related Questions