開始使用免費開始

建立 Grid Search 函式

在資料科學中,嘗試從零開始打造演算法、模型與流程,是加深理解內部運作的好方法。當然,也有很棒的套件與函式庫能協助完成這些工作(我們很快就會用到!),但從零開始練習,會讓你的資料科學工作更具優勢。

在這個練習中,你會建立一個函式,接收 2 個超參數、訓練模型並回傳結果。你會在後續的練習中再次使用這個函式。

你可以使用 X_trainX_testy_trainy_test 這些資料集。

本練習屬於課程

Python 超參數調校

檢視課程

練習說明

  • 建立一個函式,接受兩個參數 learning_ratemax_depth,分別代表學習率與最大深度。
  • 在函式中加入功能:建立一個 GBM 模型,並以輸入的超參數對資料進行訓練(fit)。
  • 讓該函式回傳模型的結果,以及選用的超參數(learning_ratemax_depth)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create the function
def gbm_grid_search(____, ____):

	# Create the model
    model = GradientBoostingClassifier(____=___, ____=____)
    
    # Use the model to make predictions
    predictions = model.fit(____, ____).predict(____)
    
    # Return the hyperparameters and score
    return([____, ____, accuracy_score(____, ____)])
編輯並執行程式碼