用 ML 分類來抓出詐欺
在這個練習中,你會看看若改用一個簡單的機器學習模型套用到信用卡資料上,結果會如何。
你覺得能超越之前的結果嗎?記得,你曾預測出 50 個詐欺案例中的 22 個,而且有 16 個誤判為詐欺。
基於這個背景,現在來實作一個 Logistic Regression 模型。若你上過 Python 的監督式學習課程,應該對這個模型不陌生;若沒有,也可以先複習一下。不過別擔心,等一下會引導你了解這個機器學習模型的結構。
X 與 y 變數已經在你的工作區中可用。
本練習屬於課程
Fraud Detection in Python
練習說明
- 將
X與y切分為訓練資料與測試資料,並保留 30% 作為測試。 - 將模型擬合到你的訓練資料上。
- 透過在
X_test上執行model.predict取得模型預測標籤。 - 比較
y_test與predicted產生分類結果,並使用提供的混淆矩陣檢查你的結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the training and testing sets
X_train, X_test, y_train, y_test = train_test_split(____, ____, test_size=____, random_state=0)
# Fit a logistic regression model to our data
model = LogisticRegression()
model.fit(____, ____)
# Obtain model predictions
predicted = model.predict(____)
# Print the classifcation report and confusion matrix
print('Classification report:\n', classification_report(____, ____))
conf_mat = confusion_matrix(y_true=y_test, y_pred=predicted)
print('Confusion matrix:\n', conf_mat)