Home > database >  Class raises "name 'smth' is not defined" error, though I've already define
Class raises "name 'smth' is not defined" error, though I've already define

Time:10-07

I am currently confronting "name 'imputer' is not defined" error.

the thing is in the init(self) part, have already defined 'imputer' by declaring 'self.imputer = IterativeImputer(max_iter=10)'.

Can anyone explain why this happens?

the whole code is as follows:

away_defencePressure_idx = 15

class IterImputer(BaseEstimator, TransformerMixin):
    def __init__(self):
        self.imputer = IterativeImputer(max_iter=10)
        
    def fit(self, X, y=None):

        imputer.fit(X)
        return self
    
    def transform(self, X, y=None):
        imputed = imputer.transform(X)
        X.T[away_defencePressure_idx] = imputed.T[away_defencePressure_idx]
        return X
        
p = Pipeline([
              ('imputerA', IterImputer())
              ])

p.fit(X)

CodePudding user response:

You defined self.imputer but not imputer. Just add self. in front of it:

    def fit(self, X, y=None):
        self.imputer.fit(X)
        return self

    def transform(self, X, y=None):
        imputed = self.imputer.transform(X)
        X.T[away_defencePressure_idx] = imputed.T[away_defencePressure_idx]
        return X
  • Related