PacktPublishing / Modern-Time-Series-Forecasting-with-Python

Modern Time Series Forecasting with Python, published by Packt
MIT License
454 stars 230 forks source link

Ch10/01 - Global Forecasting Models: AttributeError: 'CountEncoder' object has no attribute 'get_feature_names' #18

Open VondracekS opened 1 year ago

VondracekS commented 1 year ago

Hi, when running the notebook 01 for the Global Forecasting Models, I keep getting the following error: AttributeError: 'CountEncoder' object has no attribute 'get_feature_names'

I have noticed that in the requirements, category_encoders package is listed without a specific version. When searching through its documentation, I haven't found the ._get_featurenames method (I guess it might have been replaced by ._get_feature_namesin() and ._get_feature_namesout(), see here I've been trying to fix the _\src\forecasting\mlforecasting.py module, however didn't get the code to work. Maybe providing a specific version of the category_encoders package you are using should suffice. Thanks

Traceback here:

Cell In[11], line 15, in train_model(model_config, feature_config, missing_config, train_features, train_target, test_features, fit_kwargs)
      1 def train_model(
      2     model_config,
      3     feature_config,
   (...)
      8     fit_kwargs={}
      9 ):
     10     ml_model = MLForecast(
     11         model_config=model_config,
     12         feature_config=feature_config,
     13         missing_config=missing_config,
     14     )
---> 15     ml_model.fit(train_features, train_target, fit_kwargs=fit_kwargs)
     16     y_pred = ml_model.predict(test_features)
     17     feat_df = ml_model.feature_importance()

File ~\Documents\ModernTimeSeries\src\forecasting\ml_forecasting.py:287, in MLForecast.fit(self, X, y, is_transformed, fit_kwargs)
    282     assert (
    283         len(missing_cat_cols) == 0
    284     ), f"These categorical features are not handled by the categorical_encoder : {missing_cat_cols}"
    285     X = self._cat_encoder.fit_transform(X, y)
    286     self._encoded_categorical_features = difference_list(
--> 287         self.model_config.categorical_encoder.get_feature_names(),
    288         self.feature_config.continuous_features
    289         + self.feature_config.boolean_features,
    290     )
    291 else:
    292     self._encoded_categorical_features = []

AttributeError: 'CountEncoder' object has no attribute 'get_feature_names'
nonpa-gt commented 1 year ago

Hello,

In my case I had no problem but I just checked and I have 2 different packages (category-encoders, category_encoders) , both are version 2.6.0

I hope it helps you on something

murpher9 commented 1 year ago

I added this function at 242

        self._encoded_categorical_features = copy.deepcopy(
            self.feature_config.categorical_features
        )
**def get_feature_names(self, input_features=None):
    if input_features is None:
        return self.columns
    else:
        return input_features**

def fit(
    self,

Also I change 291 to the following:

            ), f"These categorical features are not handled by the categorical_encoder : {missing_cat_cols}"
        X = self._cat_encoder.fit_transform(X, y)
        X_encoded = self._cat_encoder.fit_transform(X, y)
        **self._encoded_categorical_features = [col for col in X_encoded.columns if col not in self.feature_config.continuous_features + self.feature_config.boolean_features]
    else:
        self._encoded_categorical_features = []
VondracekS commented 1 year ago

Hello,

In my case I had no problem but I just checked and I have 2 different packages (category-encoders, category_encoders) , both are version 2.6.0

I hope it helps you on something

Hi. Thanks for the comment, however I do not think these are 2 separate packages. The category-encoders is name of the package on pip server while category_encoders is the name you use when importing the package in python

manujosephv commented 1 year ago

21 Should have fixed this. It would be nice if someone can check and confirm

AhmedGillani commented 9 months ago

I can fix this error by simply running following commands, python -m pip install --upgrade scikit-learn & pip install -U scikit-learn python -m pip install --upgrade category_encoders & pip install -U category_encoders