Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saved Model Cannot Be Used for Prediction When Saved with Model_Only=True #1

Open
maverick-scientist opened this issue May 8, 2022 · 0 comments

Comments

@maverick-scientist
Copy link

Hi Team,
I saved a lot of models with model_only set to true. I reopened my notebook, cleansed the data and executed the "setup" method. Then loaded all the saved models and tried to run predict method only to receive the below error. I was under the impression that if I saved a model with model_only=True, I need to run the setup and the model can be used for everything without any hassle, Which doesn't seem to be the case.

ERROR


ValueError Traceback (most recent call last)
Input In [71], in <cell line: 1>()
----> 1 unseen_predictions_tuned_model = predict_model(tuned_model, data=data_unseen)
2 unseen_predictions_tuned_model.head()

File ~\AppData\Roaming\Python\Python38\site-packages\pycaret\classification.py:2126, in predict_model(estimator, data, probability_threshold, encoded_labels, raw_score, drift_report, round, verbose, drift_kwargs)
2047 def predict_model(
2048 estimator,
2049 data: Optional[pd.DataFrame] = None,
(...)
2056 drift_kwargs: Optional[dict] = None,
2057 ) -> pd.DataFrame:
2059 """
2060 This function predicts Label and Score (probability of predicted
2061 class) using a trained model. When data is None, it predicts label and
(...)
2123
2124 """
-> 2126 return pycaret.internal.tabular.predict_model(
2127 estimator=estimator,
2128 data=data,
2129 probability_threshold=probability_threshold,
2130 encoded_labels=encoded_labels,
2131 raw_score=raw_score,
2132 drift_report=drift_report,
2133 round=round,
2134 verbose=verbose,
2135 ml_usecase=MLUsecase.CLASSIFICATION,
2136 drift_kwargs=drift_kwargs,
2137 )

File ~\AppData\Roaming\Python\Python38\site-packages\pycaret\internal\tabular.py:9116, in predict_model(estimator, data, probability_threshold, encoded_labels, drift_report, raw_score, round, verbose, ml_usecase, display, drift_kwargs)
9113 probability_threshold = estimator.probability_threshold
9114 estimator = get_estimator_from_meta_estimator(estimator)
-> 9116 pred = np.nan_to_num(estimator.predict(X_test_))
9118 try:
9119 score = estimator.predict_proba(X_test_)

File ~\AppData\Roaming\Python\Python38\site-packages\sklearn\utils\metaestimators.py:119, in _IffHasAttrDescriptor.get..(*args, **kwargs)
116 attrgetter(self.delegate_names[-1])(obj)
118 # lambda, but not partial, allows help() to work with update_wrapper
--> 119 out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs)
120 # update the docstring of the returned function
121 update_wrapper(out, self.fn)

File ~\AppData\Roaming\Python\Python38\site-packages\sklearn\pipeline.py:408, in Pipeline.predict(self, X, **predict_params)
406 for _, name, transform in self._iter(with_final=False):
407 Xt = transform.transform(Xt)
--> 408 return self.steps[-1][-1].predict(Xt, **predict_params)

File ~\AppData\Roaming\Python\Python38\site-packages\pycaret\internal\meta_estimators.py:151, in CustomProbabilityThresholdClassifier.predict(self, X, **predict_params)
149 if not hasattr(self.classifier_, "predict_proba"):
150 return self.classifier_.predict(X, **predict_params)
--> 151 pred = self.classifier_.predict_proba(X, **predict_params)
152 if pred.shape[1] > 2:
153 raise ValueError(
154 f"{self.class.name} can only be used for binary classification."
155 )

File ~\AppData\Roaming\Python\Python38\site-packages\sklearn\ensemble_forest.py:673, in ForestClassifier.predict_proba(self, X)
671 check_is_fitted(self)
672 # Check data
--> 673 X = self._validate_X_predict(X)
675 # Assign chunk of trees to jobs
676 n_jobs, _, _ = _partition_estimators(self.n_estimators, self.n_jobs)

File ~\AppData\Roaming\Python\Python38\site-packages\sklearn\ensemble_forest.py:421, in BaseForest.validate_X_predict(self, X)
417 """
418 Validate X whenever one tries to predict, apply, predict_proba."""
419 check_is_fitted(self)
--> 421 return self.estimators
[0]._validate_X_predict(X, check_input=True)

File ~\AppData\Roaming\Python\Python38\site-packages\sklearn\tree_classes.py:396, in BaseDecisionTree.validate_X_predict(self, X, check_input)
394 n_features = X.shape[1]
395 if self.n_features
!= n_features:
--> 396 raise ValueError("Number of features of the model must "
397 "match the input. Model n_features is %s and "
398 "input n_features is %s "
399 % (self.n_features_, n_features))
401 return X

ValueError: Number of features of the model must match the input. Model n_features is 233 and input n_features is 235.

There has been no change in the source data and all the steps executed were same.

Please advise.

Thanks in advance,
Abhinav

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant