訓練與測試的轉換(I)
到目前為止,你已經根據單一欄位建立縮放器,然後將同一個縮放器套用到它所訓練的那份資料上。在建立機器學習模型時,你通常會用歷史資料(train set)來建立模型,並將模型套用到新的、未看過的資料(test set)。在這些情況下,你需要確保對訓練資料與測試資料使用相同的縮放方式。 在實務上,做法是先在訓練集上訓練縮放器,並保留已訓練好的縮放器,接著再把它用在測試集上。你不應該在測試集上重新訓練縮放器。
在這題與下一題中,我們已將 so_numeric_df DataFrame 切分為訓練集(so_train_numeric)與測試集(so_test_numeric)。
本練習屬於課程
Feature Engineering for Machine Learning in Python
練習說明
- 將
StandardScaler()具現化為SS_scaler。 - 在
Age欄位上對StandardScaler進行擬合(fit)。 - 將測試集(
so_test_numeric)中的Age欄位進行轉換(transform)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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())