交叉驗證測試的限制
如果你想做非常大量的交叉驗證,可以把 nfold 和 num_boost_round 都設成很大的數字。資料框 cv_results_big 已經載入到工作空間,是用以下程式碼建立的:
cv = xgb.cv(params, DTrain, num_boost_round = 600, nfold=10,
shuffle = True)
這裡,cv() 一共執行了 600 次交叉驗證迭代!參數 shuffle 會讓函式在每次迭代時隨機打散紀錄。
請查看這份資料,看看 AUC 的表現,並檢查在交叉驗證下是否能達到 1.0。你也應該把測試集的 AUC 分數畫出來,觀察它的變化過程。
資料框 cv_results_big 已經載入到工作空間。
本練習屬於課程
以 Python 進行信用風險建模
練習說明
- 列印 CV 結果資料框的前 5 列。
- 列印 CV 結果資料框中測試集 AUC 的平均值,並四捨五入到小數點後兩位。
- 繪製折線圖,顯示各次迭代中的測試集 AUC 走勢。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Print the first five rows of the CV results data frame
print(____.____())
# Calculate the mean of the test AUC scores
print(np.____(____[____]).round(2))
# Plot the test AUC scores for each iteration
plt.____(____[____])
plt.title('Test AUC Score Over 600 Iterations')
plt.xlabel('Iteration Number')
plt.ylabel('Test AUC Score')
plt.____()