使用fit_transform()和transform()

发布于 2025-02-08 09:09:32 字数 451 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2025-02-15 09:09:32

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 do fit_transform because we need to compute mean and stdev, and then use it to scale the X_train dataset. For X_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 from X_train. The reason why we apply the derived mean and stdev (from X_train) to transform X_test as well, is to have the same "apple-to-apple" comparison for y_test and y_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文