使用fit_transform()和transform()
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
我所知道的是fit()
方法计算功能的均值和标准偏差,然后transform()
方法使用它们将功能转换为新的缩放功能。 fit_transform()
无非是调用fit()
& transform()
单行中的方法。
但是这里为什么我们只调用fit()
用于培训数据而不是用于测试数据?
这是否意味着我们正在使用Mean&培训数据的标准偏差以转换我们的测试数据?
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
What I know is fit()
method calculates mean and standard deviation of the feature and then transform()
method uses them to transform the feature into a new scaled feature. fit_transform()
is nothing but calling fit()
& transform()
method in a single line.
But here why are we only calling fit()
for training data and not for testing data??
Does that means we are using mean & standard deviation of training data to transform our testing data ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fit
计算用于以后缩放的平均值和STDEV,请注意,这只是一个未完成缩放的计算。转换
使用先前计算的平均值和STDEV来扩展数据(从所有值中减去平均值,然后将其除以STDEV)。fit_transform
同时进行。因此,您只需1行代码即可完成。对于
x_train
数据集,我们执行fit_transform
,因为我们需要计算均值和stdev,然后使用它来扩展x_train
数据集。对于x_test
数据集,由于我们已经具有均值和stdev,因此我们只执行转换部分。edit :
x_test
数据应完全看不见和未知(即,从它们中提取任何信息),所以我们只能从x_train
中得出信息。我们之所> y_test 和y_pred
。顺便说一句,如果火车/测试数据在没有偏差的情况下正确拆分,并且数据足够大,则两个数据集将与总体平均值和STDEV具有相同的近似值。
fit
computes the mean and stdev to be used for later scaling, note it's just a computation with no scaling done.transform
uses the previously computed mean and stdev to scale the data (subtract mean from all values and then divide it by stdev).fit_transform
does both at the same time. So you can do it with just 1 line of code.For
X_train
dataset, we dofit_transform
because we need to compute mean and stdev, and then use it to scale theX_train
dataset. ForX_test
dataset, since we already have the mean and stdev, we only do the transformation part.Edit:
X_test
data should be totally unseen and unknown (ie, no info is extracted from them), so we can only derive info fromX_train
. The reason why we apply the derived mean and stdev (fromX_train
) to transformX_test
as well, is to have the same "apple-to-apple" comparison fory_test
andy_pred
.By the way, if the train/test data is split properly without bias, and that the data is sufficiently large, both datasets would have the same approximation to the population mean and stdev.