训练与测试集的变换(I)
到目前为止,您已经基于某一列创建了缩放器,然后将该缩放器应用到与其训练时相同的数据上。在构建机器学习模型时,通常会在历史数据(训练集)上训练模型,并将模型应用到新的、未见过的数据(测试集)。在这种情况下,您需要确保对训练集和测试集应用相同的缩放方式。 在实际操作中,您需要在训练集上训练缩放器,并保留这个已训练的缩放器,用于对测试集进行变换。切勿在测试集上重新训练缩放器。
在本练习及下一个练习中,我们已将 so_numeric_df DataFrame 划分为训练集(so_train_numeric)和测试集(so_test_numeric)。
本练习是课程的一部分
Python 中的机器学习特征工程
练习说明
- 将
StandardScaler()实例化为SS_scaler。 - 在
Age列上拟合StandardScaler。 - 在测试集(
so_test_numeric)中对Age列进行变换。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import StandardScaler
from sklearn.preprocessing import StandardScaler
# Apply a standard scaler to the data
SS_scaler = ____
# Fit the standard scaler to the data
____
# Transform the test data using the fitted scaler
so_test_numeric['Age_ss'] = ____
print(so_test_numeric[['Age', 'Age_ss']].head())