scikit_learn 中的 fit() 、fit_transform() 和 transform() 有什么区别?
what is the difference between fit() ,fit_transform() and transform() in scikit_learn?
这是我使用 fit_transform()
和 transform()
:
的特征缩放代码
##Feature scaling
from sklearn.preprocessing import StandardScaler
sc_x=StandardScaler()
X_train=sc_x.fit_transform(X_train)
X_test=sc_x.transform(X_test)
fit
表示使 pre-processor 适合所提供的数据。这是 pre-processor 从数据中“学习”的地方。
transform
表示根据拟合pre-processor对数据进行变换(产生输出);它通常用于 test 数据和一般看不见的数据(例如,在部署模型后出现的新数据中)。
fit_transform
意味着两者兼而有之 - 将 pre-processor 拟合到数据,然后根据拟合的 pre-processor 转换数据。调用 fit_transform
是为了避免需要在同一输入上顺序调用 fit
和 transform
,当然这只适用于 training 数据(不幸的是,在测试中再次调用 fit_transform
或看不见的数据是菜鸟常见的错误)。
这是我使用 fit_transform()
和 transform()
:
##Feature scaling
from sklearn.preprocessing import StandardScaler
sc_x=StandardScaler()
X_train=sc_x.fit_transform(X_train)
X_test=sc_x.transform(X_test)
fit
表示使 pre-processor 适合所提供的数据。这是 pre-processor 从数据中“学习”的地方。
transform
表示根据拟合pre-processor对数据进行变换(产生输出);它通常用于 test 数据和一般看不见的数据(例如,在部署模型后出现的新数据中)。
fit_transform
意味着两者兼而有之 - 将 pre-processor 拟合到数据,然后根据拟合的 pre-processor 转换数据。调用 fit_transform
是为了避免需要在同一输入上顺序调用 fit
和 transform
,当然这只适用于 training 数据(不幸的是,在测试中再次调用 fit_transform
或看不见的数据是菜鸟常见的错误)。