ML Models results in `AttributeError: 'OneHotEncoder' object has no attribute '_infrequent_enabled'`

Viewed 2473

I am trying to run the ServingMLFastCelery, which is also available and explained on the Towards Data Science website.

The machine learning model is working perfectly, but when I test the complete project the error appears:

[2022-05-18 11:37:45,306: ERROR/MainProcess] Task celery_task_app.tasks.Churn raised unexpected: AttributeError("'OneHotEncoder' object has no attribute '_infrequent_enabled'")
Traceback (most recent call last):
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\celery\app\trace.py", line 405, in trace_task
    R = retval = fun(*args, **kwargs)
  File "C:\Users\diego\codes\ServingMLFastCelery\celery_task_app\tasks.py", line 30, in __call__
    return self.run(*args, **kwargs)
  File "C:\Users\diego\codes\ServingMLFastCelery\celery_task_app\tasks.py", line 42, in predict_churn_single
    pred_array = self.model.predict([data])
  File "C:\Users\diego\codes\ServingMLFastCelery\celery_task_app\ml\model.py", line 27, in predict
    predictions = self.model.predict_proba(df)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\sklearn\pipeline.py", line 523, in predict_proba
    Xt = transform.transform(Xt)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\sklearn\compose\_column_transformer.py", line 746, in transform
    Xs = self._fit_transform(
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\sklearn\compose\_column_transformer.py", line 604, in _fit_transform
    return Parallel(n_jobs=self.n_jobs)(
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\joblib\parallel.py", line 1044, in __call__
    while self.dispatch_one_batch(iterator):
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\joblib\parallel.py", line 859, in dispatch_one_batch
    self._dispatch(tasks)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\joblib\parallel.py", line 777, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\joblib\_parallel_backends.py", line 208, in apply_async
    result = ImmediateResult(func)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\joblib\_parallel_backends.py", line 572, in __init__
    self.results = batch()
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\joblib\parallel.py", line 262, in __call__
    return [func(*args, **kwargs)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\joblib\parallel.py", line 262, in <listcomp>
    return [func(*args, **kwargs)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\sklearn\utils\fixes.py", line 117, in __call__
    return self.function(*args, **kwargs)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\sklearn\pipeline.py", line 853, in _transform_one
    res = transformer.transform(X)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\sklearn\preprocessing\_encoders.py", line 888, in transform
    self._map_infrequent_categories(X_int, X_mask)
  File "c:\users\diego\anaconda3\envs\k38\lib\site-packages\sklearn\preprocessing\_encoders.py", line 726, in _map_infrequent_categories
    if not self._infrequent_enabled:
AttributeError: 'OneHotEncoder' object has no attribute '_infrequent_enabled'

The part of the prediction model that uses OneHotEnconder is:

preprocessing_pipeline = ColumnTransformer(transformers=[
    ('num', StandardScaler(), NUMERICAL_FEATURES),
    ('cat', OneHotEncoder(sparse=False), CATEGORICAL_FEATURES)
])

df_new = pd.DataFrame(preprocessing_pipeline.fit_transform(df))

I tried some solutions available on the internet, but none worked for this case.

0 Answers
Related