sklearn.exceptions.NotFittedError: Estimator not fitted, call `fit` before exploiting the model

Viewed 2675

I tried Random Forests regression.

The code is given below.

import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold, cross_val_predict
from sklearn.feature_selection import SelectKBest, f_regression 
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.feature_selection import RFECV
from sklearn.model_selection import GridSearchCV
np.random.seed(0)


d1 = np.random.randint(2, size=(50, 10))
d2 = np.random.randint(3, size=(50, 10))
d3 = np.random.randint(4, size=(50, 10))
Y = np.random.randint(7, size=(50,))


X = np.column_stack([d1, d2, d3])


n_smples, n_feats = X.shape
print (n_smples, n_feats)


kf = KFold(n_splits=5, shuffle=True, random_state=0)

regr = RandomForestRegressor(max_features=None,random_state=0)                
pipe = make_pipeline(RFECV(estimator=regr, step=3, cv=kf, scoring = 
'neg_mean_squared_error', n_jobs=-1),
             GridSearchCV(regr, param_grid={'n_estimators': [100, 300]},
                          cv=kf, scoring = 'neg_mean_squared_error', 
n_jobs=-1))

ypredicts = cross_val_predict(pipe, X, Y, cv=kf, n_jobs=-1)

rmse = mean_squared_error(Y, ypredicts)
print (rmse)

However, I got the following error: sklearn.exceptions.NotFittedError: Estimator not fitted, call fit before exploiting the model.

I also tried:

model = pipe.fit(X,Y)

ypredicts = cross_val_predict(model, X, Y, cv=kf, n_jobs=-1)

But got the same error.

Edit 1: I also tried:

pipe.fit(X,Y)

But got the same error.

In Python 2.7 (Sklearn 0.20), for the same code I got different error:

TerminatedWorkerError: A worker process managed by the executor was unexpectedly terminated. This could be caused by a segmentation fault while calling the function or by an excessive memory usage causing the Operating System to kill the worker.

In Python 2.7 (Sklearn 0.20.3): NotFittedError: Estimator not fitted, callfitbefore exploiting the model.

2 Answers

Instead of

model = pipe.fit(X,Y)

have you tried

pipe.fit(X,Y)

instead?

so that would be

pipe.fit(X,Y)
# change model to pipe
ypredicts = cross_val_predict(pipe, X, Y, cv=kf, n_jobs=-1)

It seems that you are trying to select best parameters for your classifier by using grid search their is a another to do so. You are using pipelines but in this method I am not using pipeline but I am getting best parameters through random search.

import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold, cross_val_predict
from sklearn.feature_selection import SelectKBest, f_regression 
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.feature_selection import RFECV
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier

np.random.seed(0)


d1 = np.random.randint(2, size=(50, 10))
d2 = np.random.randint(3, size=(50, 10))
d3 = np.random.randint(4, size=(50, 10))
Y = np.random.randint(7, size=(50,))


X = np.column_stack([d1, d2, d3])


n_smples, n_feats = X.shape
print (n_smples, n_feats)


kf = KFold(n_splits=5, shuffle=True, random_state=0)

regr = RandomForestRegressor(max_features=None,random_state=0)                

n_iter_search = 20
random_search = RandomizedSearchCV(regr, param_distributions={'n_estimators': [100, 300]},
                                   n_iter=20, cv=kf,verbose=1,return_train_score=True)
random_search.fit(X, Y)

ypredicts=random_search.predict(X)
rmse = mean_squared_error(Y, ypredicts)
print(rmse)
print(random_search.best_params_)
random_search.cv_results_

Try this piece of code. I hope this code is fullfilling your problem.

Related