Leave-one-out cross-validation
假設你最喜歡的糖果並不在這個糖果資料集中,而你想知道它的受歡迎程度。使用 5 折交叉驗證時,每次只會用到 80% 的資料來訓練。不過這個糖果資料集只有 85 列,若每次留掉 20% 的資料,可能會影響模型表現。相對地,使用留一法交叉驗證(leave-one-out cross-validation,LOOCV)能把有限的資料用到極致,並為你最喜歡的糖果受歡迎程度提供更好的估計!
在本練習中,你將使用 cross_val_score() 來執行 LOOCV。
本練習屬於課程
Python 的模型驗證
練習說明
- 使用
mean_absolute_error建立供cross_val_score()使用的評分器(scorer)。 - 完成
cross_val_score(),讓模型rfr、新建立的mae_scorer,以及 LOOCV 都被使用。 - 使用
numpy(已以np載入)列印scores的平均值與標準差。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from sklearn.metrics import mean_absolute_error, make_scorer
# Create scorer
mae_scorer = ____(____)
rfr = RandomForestRegressor(n_estimators=15, random_state=1111)
# Implement LOOCV
scores = cross_val_score(____, X=X, y=y, cv=____, scoring=____)
# Print the mean and standard deviation
print("The mean of the errors is: %s." % np.____(____))
print("The standard deviation of the errors is: %s." % np.____(____))