開始使用免費開始

使用 GBM 進行情緒分析

現在,讓我們在 reviews 資料集上使用 scikit-learnGradientBoostingClassifier,根據評論文字來預測其情緒。

我們不會把原始文字直接輸入模型。以下前處理步驟已替你完成:

  1. 移除含有遺漏值的評論。
  2. 篩選出前 5 個應用程式的資料。
  3. 隨機抽樣 500 筆評論。
  4. 從評論中移除「停用詞」。
  5. 將評論轉換為矩陣,其中每個特徵代表某個單字在該評論中的出現頻率。

想更深入了解文字探勘嗎?可以參考這門課程:Introduction to Natural Language Processing in Python

本練習屬於課程

Python 的 Ensemble 方法

檢視課程

練習說明

  • 建立一個 GradientBoostingClassifier,設定 100 個 estimators,學習率為 0.1
  • 在測試集上計算預測結果。
  • 計算正確率以評估模型。
  • 計算並印出混淆矩陣。

動手互動練習

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

# Build and fit a Gradient Boosting classifier
clf_gbm = ____(____, ____, random_state=500)
clf_gbm.fit(X_train, y_train)

# Calculate the predictions on the test set
pred = ____

# Evaluate the performance based on the accuracy
acc = ____
print('Accuracy: {:.3f}'.format(acc))

# Get and show the Confusion Matrix
cm = ____
print(cm)
編輯並執行程式碼