開始使用免費開始

用 Random Forest 進行隨機搜尋

為了鞏固你對隨機取樣的理解,讓我們用不同的超參數與不同的演算法來做一個類似的練習。

和之前一樣,先建立一些超參數的清單,之後可以壓成一個「清單的清單」。你將使用隨機森林演算法的超參數 criterionmax_depthmax_features。接著,為即將執行的隨機搜尋,隨機抽取多組超參數組合。

在這個任務中,你會用一個稍微不同的取樣方式:random.sample()

本練習屬於課程

Python 超參數調校

檢視課程

練習說明

  • criterion 建立包含值 'gini''entropy' 的清單,為 max_features 建立包含 "auto", "sqrt", "log2", None 的清單。
  • 為超參數 max_depth 建立一個介於 3 到 55(含)之間的數值清單,並指定給清單 max_depth_list。記住,range(N,M) 會建立從 NM-1 的清單。
  • 使用 product() 將這些清單合併成一個可供取樣的「清單的清單」。
  • 從合併後的清單中隨機取樣 150 個模型,並印出結果。

動手互動練習

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

# Create lists for criterion and max_features
criterion_list = ____
max_feature_list = ____

# Create a list of values for the max_depth hyperparameter
max_depth_list = list(range(____,____))

# Combination list
combinations_list = [list(x) for x in product(____, ____, ____)]

# Sample hyperparameter combinations for a random search
combinations_random_chosen = random.sample(____, ____)

# Print the result
print(____)
編輯並執行程式碼