Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - why is fit_transform() not applied to test sets?

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train[:, 3:] = sc.fit_transform(X_train[:, 3:])
X_test[:, 3:] = sc.transform(X_test[:, 3:])

in the above code why is fit_transform() not applied to test set and why is it applied only to training set?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The idea is that your scale the values take in consideration only the X_train because it's the only data you have to train the model. Test data is only used to simulated when you are applying your model to real live.

As you know, StandarScaler depend on the data, so the scalation would be different between train and test data. What you do fiting X_train is that all the test data will be transformed following the parameters you obtained on the train set.

Clear example:

Now imagine you have only 1 samples test. Do you fit and transform this simple test data? (difficult for only one sample). The answer is NO. You use the parameters of X_train to scale this unique value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...