建立並評估模型:產品評論資料
在這個練習中,你會使用 reviews 資料集來建立邏輯斯迴歸。這個資料集包含使用者對 Amazon 產品的評論。陣列 y 包含情緒標籤:正向為 1,否則為 0。陣列 X 則包含以 BOW 方法建立的所有數值特徵。你可以在 IPython Shell 中自由探索。
你的任務是建立一個邏輯斯迴歸模型,並使用測試資料集計算準確率與混淆矩陣。
邏輯斯迴歸與訓練/測試切分的函式都已為你匯入。
本練習屬於課程
Python 情感分析
練習說明
- 匯入 accuracy score 與 confusion matrix 的函式。
- 將資料切分為訓練與測試集,將其中 30% 作為測試集,並將隨機種子設為
42。 - 訓練一個邏輯斯迴歸模型。
- 使用測試資料列印準確率與混淆矩陣。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the accuracy and confusion matrix
____
# Split the data into training and testing
X_train, X_test, y_train, y_test = ____(____, ____, ____=0.3, ____=42)
# Build a logistic regression
log_reg = ____._____
# Predict the labels
y_predict = log_reg.predict(X_test)
# Print the performance metrics
print('Accuracy score of test data: ', ____(____, ____))
print('Confusion matrix of test data: \n', ____(____, ____)/len(y_test))