baxx
baxx

Reputation: 4755

sklearn pipeline error - fit() takes 1 positional argument but 3 were given

Trying to run the following:

class N1:
    def __init__(self):
        pass

    def fit(self):
        return self

    def transform(self, X):
        return X.assign(num_1="n1")

X = pd.DataFrame(
    {
        "n1": [1, 2, 3],
        "n2": [3, 4, 4],
        "c1": ["a", "b", "c"],
        "c2": ["x", "y", "z"],
    }
)

num_pipeline = Pipeline(
    [
        ("num_1", N1()),
    ]
)

num_pipeline.fit(X)
# same error with: 
# num_pipeline.fit_transform(X)

Gives the error:

TypeError: fit() takes 1 positional argument but 3 were given

I don't really see how this is happening though, or how to fix.

Full traceback:

    387                 return last_step.fit_transform(Xt, y, **fit_params_last_step)
    388             else:
--> 389                 return last_step.fit(Xt, y,
    390                                      **fit_params_last_step).transform(Xt)
    391 

TypeError: fit() takes 1 positional argument but 3 were given

I'm expecting to have the dataframe X returned with the added column num_1

Upvotes: 1

Views: 2357

Answers (1)

nocibambi
nocibambi

Reputation: 2431

Usually, fit requires also an X and an optional y parameter so probably Pipeline tries to pass these.

Maybe try to define it like so:

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

You might also want to look at FunctionTransformer.

Upvotes: 3

Related Questions