M Waz
M Waz

Reputation: 775

RuntimeError: The reset parameter is False but there is no n_features_in_ attribute. Is this estimator fitted?

I am saving a sklearn.impute.SimpleImputer object using pickle. The imputer is fit with

imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean')
imputer = imputer.fit(train)

I then save the imputer with

pickle.dump(imputer,open('imputer.pkl','wb'))

and load the model in a flask app using

imputer = pickle.load(open('imputer.pkl','rb'))

I can successfully see the values that should be imputed if I check imputer.statistics_ . However, when I run

imputer.transform(test)

in my flask app using the imputer loaded with pickle, I get the following error:

"The reset parameter is False but there is no " RuntimeError: The reset parameter is False but there is no n_features_in_ attribute. Is this estimator fitted?

I am using sklearn version 0.23.1 to fit the imputer and transform the data. Does anyone have any insight on this? Please let me know what other information I can provide.

Upvotes: 6

Views: 3405

Answers (2)

James Brusey
James Brusey

Reputation: 365

This seems to have been introduced with a new public attribute n_features_in_ for estimators SLEP010 that facilitates a check on the length of the input vector.

This is going to be an issue for anyone who has saved a learned model under 0.22 and is then trying to use it with 0.23 or later. Assuming that you cannot go back and re-learn the model under 0.23, a possible solution is to load the model and then set the n_features_in_ attribute manually (to the length of X vector for predict() or transform()) after loading.

If the model is a pipeline, set n_features_in_ for each step.

Upvotes: 2

Sairam Reddy
Sairam Reddy

Reputation: 81

Reverting sklearn version to 0.22.2.post1 could fix your issue:

pip install scikit-learn==0.22.2.post1

It worked for me.

Upvotes: 7

Related Questions