sklearn.compose.ColumnTransformer fit_transform takes 2 positional arguments but 3 were given
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error fit_transform() takes 2 positional arguments but 3 were given occurs when you pass both X and y to a ColumnTransformer's fit_transform() method. Unlike estimators like RandomForestClassifier that expect fit(X, y), transformers in scikit-learn expect fit(X) or fit_transform(X) — they transform features without needing the target variable. The fix is to remove the y argument from the transformer call or restructure your pipeline so the transformer and estimator are properly chained.
The Error
The Fix
ColumnTransformer.fit_transform() only accepts X. It transforms feature columns — the target variable y is not used.
Why Transformers Don't Take y
Transformers preprocess input features independently of the target. StandardScaler computes mean and standard deviation from X alone. OneHotEncoder maps categories from X alone. The target y is only needed by supervised estimators (classifiers and regressors) during fit().
Using Pipeline to Chain Transformer + Estimator
The correct way to combine preprocessing and prediction is with a Pipeline:
The Pipeline passes X through the transformer, then passes the transformed X and y to the estimator. You only call fit(X, y) on the Pipeline — it handles the argument routing internally.
Common ColumnTransformer Patterns
TransformedTargetRegressor for y Transformation
If you need to transform the target y, use TransformedTargetRegressor:
Common Pitfalls
- Passing
ytoColumnTransformer.fit_transform(): Transformers preprocess features only — they do not use the target variable. Pass onlyXtofit_transform(). Use aPipelineto connect the transformer to an estimator that needsy. - Confusing
fit_transform(X)withfit(X, y): Transformers usefit(X)andtransform(X)(orfit_transform(X)). Estimators usefit(X, y)andpredict(X). Pipelines unify both interfaces so you can callpipeline.fit(X, y)and it routes arguments correctly. - Not using
make_column_selectorfor dynamic column selection: Hard-coding column names breaks when columns change.make_column_selector(dtype_include=np.number)automatically selects numeric columns regardless of their names. - Forgetting
handle_unknown="ignore"on OneHotEncoder: If test data contains categories not seen during training,OneHotEncoderraises an error by default. Sethandle_unknown="ignore"to encode unknown categories as all-zeros. - Calling
transform()beforefit():ColumnTransformer.transform(X)requires thatfit(X)has been called first to learn the transformations. Callingtransform()on an unfitted transformer raisesNotFittedError.
Summary
ColumnTransformer.fit_transform()accepts onlyX— do not passy- Transformers preprocess features independently of the target variable
- Use
Pipelineto chainColumnTransformerwith an estimator that needsy - Use
make_column_selectorfor automatic column type detection - Use
TransformedTargetRegressorif you need to transform the target variable - Always call
fit()orfit_transform()beforetransform()on any transformer

