Search code examples
pythonpandasmachine-learningscikit-learnfeature-engineering

How can i solve the TypeError gotten while working with feature_engine


I am working with feature_engine to fill missing values

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# from feature-engine
from feature_engine import missing_data_imputers as mdi

#Working with House Data and Feature Engine__Practice
cols_to_use = [
    'BsmtQual', 'FireplaceQu', 'LotFrontage', 'MasVnrArea', 'GarageYrBlt',
]
data = pd.read_csv(r'C:\Users\HP\Desktop\Hash\kaggle\Housing Project/train.csv', usecols=cols_to_use)

I created an instance of mdi to fit my data

imputer = mdi.MeanMedianImputer(imputation_method='median')
imputer.fit(data)

But on calling the transform method it returns a TypeError of which i cant find a reason why it happened.

tmp = imputer.transform(data)

Here is the error returned

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-39-2f486acb96bd> in <module>
----> 1 tmp = imputer.transform(data)

~\Anaconda3\lib\site-packages\feature_engine\missing_data_imputers.py in transform(self, X)
    103     # Ugly work around to import the docstring for Sphinx, otherwise none of this is necessary
    104     def transform(self, X):
--> 105         X = super().transform(X)
    106         return X
    107 

~\Anaconda3\lib\site-packages\feature_engine\base_transformers.py in transform(self, X)
     35 
     36         # Check method fit has been called
---> 37         check_is_fitted(self)
     38 
     39         # check that input is a dataframe

TypeError: check_is_fitted() missing 1 required positional argument: 'attributes'

Solution

  • By looking at the stack trace you provided, this seems to me like an incompatibility between feature_engine and an old version of scikit-learn. In older versions (e.g. 0.21), attributes was a mandatory parameter for check_is_fitted, but in newer versions (e.g. 0.23) it is optional:

    If None, estimator is considered fitted if there exist an attribute that ends with a underscore and does not start with double underscore.