galoosh33
galoosh33

Reputation: 326

Scikit-Learn: How to use Transformation of Matrix X to transform Target Variables Y

I'm working on a regression problem, with my features in a matrix X and target values in Y.

I want to scale the input. I'm doing it using sklearn's MinMaxScaler:

# scale data to 0-1
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)

My understanding is that the true labels vector Y should now be scaled accordingly. How do I do this using the scaler object? From the docs, I can pass the true labels to the fit_transform method, but it seems like this is only for "pipeline compatibility" - i.e, the method disregards Y and only returns the transformed X.

Thanks!

Upvotes: 1

Views: 703

Answers (2)

Pedro Ilidio
Pedro Ilidio

Reputation: 53

Nowadays an option is to use the TransformedTargetRegressor meta-estimator to apply a transformation to the target variable.

Upvotes: 0

akuiper
akuiper

Reputation: 215117

You can't scale target variable Y with that scaler.

Because MinMaxScaler:

Transforms features by scaling each feature to a given range.

It doesn't transform target variable. Or more precisely since you've fitted it on features, then you can only apply it on features.

The case when you will need the scaler again is when you try to apply the model to your testing data to make predictions, you need to use the same scaler to transform the features of your testing data as well so the result will be consistent.

Upvotes: 5

Related Questions