訓練與測試時的轉換(II)
和在訓練集與測試集上使用同一個縮放器的做法相同,如果你在訓練集中移除了離群值,通常也會希望在測試集中做同樣的事。再次強調,你應該只使用「從訓練集計算出的門檻值」,來從測試集中移除離群值。
和上一個練習類似,我們將 so_numeric_df DataFrame 拆分為訓練集(so_train_numeric)與測試集(so_test_numeric)。
本練習屬於課程
Feature Engineering for Machine Learning in Python
練習說明
- 計算
ConvertedSalary欄位的標準差與平均數。 - 在兩個方向上,各以距平均數 3 個標準差計算上下界。
- 修剪
so_test_numericDataFrame,只保留ConvertedSalary介於下界與上界之間的所有列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
train_std = so_train_numeric['ConvertedSalary'].____
train_mean = so_train_numeric['ConvertedSalary'].____
cut_off = train_std * 3
train_lower, train_upper = ____, train_mean + cut_off
# Trim the test DataFrame
trimmed_df = so_test_numeric[(so_test_numeric['ConvertedSalary'] < ____) \
& (so_test_numeric['ConvertedSalary'] > ____)]