建立學習曲線
當我們想針對單一超參數測試很多不同的數值時,用 DataFrame 呈現會不太直觀。你先前學過一個不錯的分析小撇步:「學習曲線(learning curve)」可以清楚展示調高或調低特定超參數對最終結果的影響。
這裡不只測試少數幾個學習率,而是測試很多個,讓你能清楚看到此超參數在廣泛數值範圍內的效果。NumPy 中有個實用函式 np.linspace(start, end, num),可在你指定的區間(start、end)內,產生等距分布的 num 個數值。
你會取得 X_train、X_test、y_train 和 y_test 資料集可供使用。
本練習屬於課程
Python 超參數調校
練習說明
- 建立一個包含 30 個學習率的清單,範圍在 0.01 到 2 之間且等距分布。
- 參考上一題的迴圈結構,但這次只需將準確率分數儲存到一個清單中。
- 繪製學習率對準確率分數的圖表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Set the learning rates & accuracies list
learn_rates = np.linspace(____, ____, num=____)
accuracies = []
# Create the for loop
for learn_rate in learn_rates:
# Create the model, predictions & save the accuracies as before
model = GradientBoostingClassifier(learning_rate=____)
predictions = model.fit(____, ____).predict(____)
accuracies.append(accuracy_score(y_test, ____))
# Plot results
plt.plot(____, ____)
plt.gca().set(xlabel='learning_rate', ylabel='Accuracy', title='Accuracy for different learning_rates')
plt.____