使用逻辑回归对信用卡诈骗分析
生活随笔
收集整理的這篇文章主要介紹了
使用逻辑回归对信用卡诈骗分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
此次的數據集包括了 2013 年 9 月份兩天時間內的信用卡交易數據,284807 筆交易中,一共有 492 筆 是欺詐行為。輸入數據一共包括了 28 個特征 V1,V2,……V28 對應的取值,以及交易時間 Time 和交易金額 Amount。為了保護數據隱私,我們不知道 V1 到 V28 這些特征代表的具體含 義,只知道這 28 個特征值是通過 PCA 變換得到的結果。另外字段 Class 代表該筆交易的分類, Class=0 為正常(非欺詐),Class=1 代表欺詐。
因為284807筆交易中只有492筆是詐騙的,數據非常的不平衡,就算全部瞎猜是正常交易,準確率也可以達到98%,所以我們不能使用準確率來評價,這里我們使用統計 F1 值(綜合精確率和召回率)。
數據探索
import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt import itertools from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix, precision_recall_curve from sklearn.preprocessing import StandardScaler import warnings warnings.filterwarnings('ignore')# 數據加載 data = pd.read_csv('F:/BaiduNetdiskDownload/DC/credit_fraud/creditcard.csv', encoding='utf-8') # 查看數據 print(data.describe())運行結果如下
Time V1 V2 V3 V4 \ count 284807.000000 2.848070e+05 2.848070e+05 2.848070e+05 2.848070e+05 mean 94813.859575 3.919560e-15 5.688174e-16 -8.769071e-15 2.782312e-15 std 47488.145955 1.958696e+00 1.651309e+00 1.516255e+00 1.415869e+00 min 0.000000 -5.640751e+01 -7.271573e+01 -4.832559e+01 -5.683171e+00 25% 54201.500000 -9.203734e-01 -5.985499e-01 -8.903648e-01 -8.486401e-01 50% 84692.000000 1.810880e-02 6.548556e-02 1.798463e-01 -1.984653e-02 75% 139320.500000 1.315642e+00 8.037239e-01 1.027196e+00 7.433413e-01 max 172792.000000 2.454930e+00 2.205773e+01 9.382558e+00 1.687534e+01 V5 V6 V7 V8 V9 \ count 2.848070e+05 2.848070e+05 2.848070e+05 2.848070e+05 2.848070e+05 mean -1.552563e-15 2.010663e-15 -1.694249e-15 -1.927028e-16 -3.137024e-15 std 1.380247e+00 1.332271e+00 1.237094e+00 1.194353e+00 1.098632e+00 min -1.137433e+02 -2.616051e+01 -4.355724e+01 -7.321672e+01 -1.343407e+01 25% -6.915971e-01 -7.682956e-01 -5.540759e-01 -2.086297e-01 -6.430976e-01 50% -5.433583e-02 -2.741871e-01 4.010308e-02 2.235804e-02 -5.142873e-02 75% 6.119264e-01 3.985649e-01 5.704361e-01 3.273459e-01 5.971390e-01 max 3.480167e+01 7.330163e+01 1.205895e+02 2.000721e+01 1.559499e+01 ... V21 V22 V23 V24 \ count ... 2.848070e+05 2.848070e+05 2.848070e+05 2.848070e+05 mean ... 1.537294e-16 7.959909e-16 5.367590e-16 4.458112e-15 std ... 7.345240e-01 7.257016e-01 6.244603e-01 6.056471e-01 min ... -3.483038e+01 -1.093314e+01 -4.480774e+01 -2.836627e+00 25% ... -2.283949e-01 -5.423504e-01 -1.618463e-01 -3.545861e-01 50% ... -2.945017e-02 6.781943e-03 -1.119293e-02 4.097606e-02 75% ... 1.863772e-01 5.285536e-01 1.476421e-01 4.395266e-01 max ... 2.720284e+01 1.050309e+01 2.252841e+01 4.584549e+00 V25 V26 V27 V28 Amount \ count 2.848070e+05 2.848070e+05 2.848070e+05 2.848070e+05 284807.000000 mean 1.453003e-15 1.699104e-15 -3.660161e-16 -1.206049e-16 88.349619 std 5.212781e-01 4.822270e-01 4.036325e-01 3.300833e-01 250.120109 min -1.029540e+01 -2.604551e+00 -2.256568e+01 -1.543008e+01 0.000000 25% -3.171451e-01 -3.269839e-01 -7.083953e-02 -5.295979e-02 5.600000 50% 1.659350e-02 -5.213911e-02 1.342146e-03 1.124383e-02 22.000000 75% 3.507156e-01 2.409522e-01 9.104512e-02 7.827995e-02 77.165000 max 7.519589e+00 3.517346e+00 3.161220e+01 3.384781e+01 25691.160000 Class count 284807.000000 mean 0.001727 std 0.041527 min 0.000000 25% 0.000000 50% 0.000000 75% 0.000000 max 1.000000交易筆數,欺詐交易筆數可視化
# 設置plt正確顯示中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪制類別分布 plt.figure() ax = sns.countplot(x ='Class', data = data) plt.title('類別分布') plt.show() # 顯示交易筆數,欺詐交易筆數 num = len(data) num_fraud = len(data[data['Class']==1]) print('總交易筆數: ', num) print('詐騙交易筆數:', num_fraud) print('詐騙交易比例:{:.6f}'.format(num_fraud/num)) # 欺詐和正常交易可視化 f, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(15,8)) bins = 50 ax1.hist(data.Time[data.Class == 1], bins=bins, color= 'deeppink') ax1.set_title('詐騙交易') ax2.hist(data.Time[data.Class == 0], bins=bins, color= 'deepskyblue') ax2.set_title('正常交易') plt.xlabel('時間') plt.ylabel('交易次數') plt.show()運行結果如下
總交易筆數: 284807 詐騙交易筆數: 492 詐騙交易比例:0.001727
特征選擇
# 對Amount進行數據規范化 data['Amount_Norm'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1,1)) # 特征選擇 y = np.array(data.Class.tolist()) data = data.drop(['Time','Amount','Class'],axis=1) X = np.array(data.values) # 準備訓練集和測試集 train_x, test_x, train_y, test_y = train_test_split (X, y, test_size =0.1, random_state=33)邏輯回歸建模
# 邏輯回歸分類 clf = LogisticRegression() clf.fit(train_x, train_y) predict_y = clf.predict(test_x) # 預測樣本的置信分數 score_y = clf.decision_function(test_x) # 計算混淆矩陣 cm = confusion_matrix(test_y, predict_y) class_names = [0, 1] # 顯示混淆矩陣 plt.figure() plt.imshow(cm, interpolation = 'nearest', cmap=plt.cm.Blues) # 熱度圖 plt.title('Confusion matrix') plt.colorbar() # 熱度顯示儀 tick_marks = np.arange(len(class_names)) # 坐標順序 plt.xticks(tick_marks, class_names, rotation = 0) # 第一個是迭代對象,表示坐標的順序,第二個是坐標顯示的數值的數組 plt.yticks(tick_marks, class_names) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])) : # 顯示數字直觀些plt.text(j, i, cm[i, j],horizontalalignment = 'center',color = 'white' if cm[i, j] > thresh else 'black') plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.show()混淆矩陣顯示如下
顯示模型評估分數
tp = cm[1, 1] fn = cm[1, 0] fp = cm[0, 1] tn = cm[0, 0] print('精確率: {:.3f}'.format(tp / (tp + fp))) print('召回率: {:.3f}'.format(tp / (tp + fn))) print('F1值: {:.3f}'.format(2 * (((tp / (tp + fp)) * (tp / (tp + fn))) / ((tp / (tp + fp)) + (tp / (tp + fn))))))運行結果如下
精確率: 0.848 召回率: 0.650 F1值: 0.736可以看出邏輯回歸模型的F1值跟精確率,召回率的值都不錯,這三個值最好的時候都為1,最差時候為0
繪制精確率-召回率曲線
precision, recall, thresholds = precision_recall_curve(test_y, score_y) plt.step(recall, precision, color='b', alpha=0.2, where='post') plt.fill_between(recall, precision, step='post', alpha=0.2, color='b') plt.plot(recall, precision, linewidth=2) plt.xlim([0.0, 1]) plt.ylim([0.0, 1.05]) plt.xlabel('召回率') plt.ylabel('精確率') plt.title('精確率-召回率 曲線') plt.show()運行結果如下
總結
在我們發現數據正負比例嚴重不平衡時,不能使用準確率來判斷模型的好壞,而是使用F1 值(綜合精確率和召回率)來判斷模型的好壞
總結
以上是生活随笔為你收集整理的使用逻辑回归对信用卡诈骗分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ping32文档透明加密系统
- 下一篇: 不高兴的津津【NOIP2004普及组第1