開始使用免費開始

計算準確率

在你把資料分成訓練集與測試集之後,就可以先把模型擬合在「訓練」資料上,接著再去預測「測試」資料的標籤。這個練習會帶你實作這些步驟。

到目前為止,你已經用過 Logistic Regression 與 Decision Trees。這裡你會使用 RandomForestClassifier,它可以被視為多棵 Decision Trees 的集成,一般來說會比單一的 Decision Tree 表現更好。

你在前面練習的成果已經延續過來,訓練集與測試集分別存放在變數 X_trainX_testy_trainy_test 中。

本練習屬於課程

行銷分析:用 Python 預測客戶流失

檢視課程

練習說明

  • sklearn.ensemble 匯入 RandomForestClassifier
  • RandomForestClassifier 例項化為 clf
  • 使用訓練資料 X_trainy_train 擬合 clf
  • 使用 .score() 方法計算 clf 在測試資料上的準確率。

動手互動練習

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

# Import RandomForestClassifier


# Instantiate the classifier
clf = ____

# Fit to the training data


# Compute accuracy
print(____.____(____, ____))
編輯並執行程式碼