I am trying to figure out when and how to use scikit-learn's StandardScaler transformer, and how I can apply it to the target variable as well.

I've read this post and, while the accepted answer maintains that it is not necessary to standardize the target vector, other answers suggested that it might still be beneficial.

So let's assume that I want to go ahead and standardize the target vector. According to the syntax, the fit_transform method of a StandardScaler instance can take both a feature matrix X, and a target vector y for supervised learning problems.

However, when I apply it, the method returns only a single array. If I try to unpack two values, like in the code below, I get a "ValueError: too many values to unpack (expected 2)" error:

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_scaled, y_scaled = scaler.fit_transform(X,y) # X is some feature array, y is the target vector

# This code will produce an error message

This is consistent with the documentation, which states that the return value is a single output array X_new.

Then my question is: why is there an option to add y to the parameters of the method? Does it change the way in which X is standardized?

If not, should I use something like the code below?:

from sklearn.preprocessing import StandardScaler

scaler_X = StandardScaler()
scaler_y = StandardScaler()

X_scaled = scaler_X.fit_transform(X)
y_scaler = scaler_y.fit_transform(y)

  • 2
    $\begingroup$ StandardScaler.fit(x, [y]...) y is optional and present for the algorithm who need it. Some algorithm have a .fit(X, y) so the StandardScaler method needs to be compatible, in case you use a pipeline. $\endgroup$
    – Malo
    Jul 5, 2021 at 22:02

1 Answer 1


The correct way of scaling both the features and the target in Python with Scikit-Learn for a regression problem would be wit pipelines as follow:

from sklearn.linear_model import LinearRegression
from sklearn.compose import TransformedTargetRegressor
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

tt = TransformedTargetRegressor(regressor=LinearRegression(),
                                transformer = StandardScaler)

model = Pipeline([("scaler",StandardScaler()),("regressor",tt)])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.