0

I am having issues creating a custom transform and pipeline. I keep getting the error after running my pipeline.

This SimpleImputer instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

I know this has something to do with my custom transformer, but I can’t seem to fix the issue. I am attempting to create a custom transformer that performs two computations. First add an attribute that is equal to column 1 (x1) cubed, divided by column 5 (x5). Then drop the entire x4 feature column if the passed function is true.

I then applied a pipeline. In my pipeline I attempted to apply a SimpleImputer class with the strategy mean, the custom transformer and the StandardScaler class. I keep getting the error above.

Below is my script:

from sklearn.base import BaseEstimator, TransformerMixin

data_num['x6'] = (data_num['x1']**3) / data_num['x5']

class Assignment4Transformer(BaseEstimator, TransformerMixin):
    def __init__(self,x4 = True):
        self.x4 = x4
        
    def fit(self, X, y=None):
        return self 
    
    def transform(self, X):
        if self.x4 == True:
            data_num.drop(columns=['x4'],inplace=True)
            return np.c_[X, x1, x2, x5, x6]
        else: 
            return np.c_[X, x1,x2, x4, x5, x6]

pipeline = Pipeline([('imputer', SimpleImputer(strategy='mean')),
                         ('std_scaler', StandardScaler),
                         ('custom_trans', Assignment4Transformer())])
num_pipeline = pipeline.transform(data_num)

Any help or suggestions would be appreciated, thank you

FYI this is the data I am working with enter image description here

Alexander L. Hayes
  • 3,892
  • 4
  • 13
  • 34
Julie
  • 1
  • 1
  • There are other issues with the code, but the error seems correct that you haven't defined a method `transform`, but instead `transfrom`. – Ben Reiniger Oct 10 '22 at 14:23

0 Answers0