用svm预测信用卡诈骗
數據集來源于Kaggle https://www.kaggle.com/mlg-ulb/creditcardfraud, 用于預測信用卡用戶是否會落入詐騙組,這里發一個中文版本的存稿
數據初探
首先導入數據
dat = pd.read_csv("E:/study/machine learning/credit card fraud/creditcard.csv") df = pd.DataFrame(dat) df.describe()Time V1 V2 ... Amount Class Amount_log 0 0.0 -1.359807 -0.072781 ... 149.62 0 5.008166 1 0.0 1.191857 0.266151 ... 2.69 0 0.993252 2 1.0 -1.358354 -1.340163 ... 378.66 0 5.936665 3 1.0 -0.966272 -0.185226 ... 123.50 0 4.816322 4 2.0 -1.158233 0.877737 ... 69.99 0 4.248495 5 2.0 -0.425966 0.960523 ... 3.67 0 1.302913 6 4.0 1.229658 0.141004 ... 4.99 0 1.609438 7 7.0 -0.644269 1.417964 ... 40.80 0 3.708927 8 7.0 -0.894286 0.286157 ... 93.20 0 4.534855 9 9.0 -0.338262 1.119593 ... 3.68 0 1.305626 10 10.0 1.449044 -1.176339 ... 7.80 0 2.055405 11 10.0 0.384978 0.616109 ... 9.99 0 2.302585 12 10.0 1.249999 -1.221637 ... 121.50 0 4.79...數據共31列,除去是否落入詐騙組的“Class”組外,另有時間和消費額,以及用于隱藏用戶信息、經過PCA處理過的V1至V28,我們首先來看數據在Class組中的分布
plt.figure(figsize=(7,5)) sns.countplot(df['Class']) plt.title("Fraud and not Fraud Class Count", fontsize=18) plt.xlabel("Fraud and not Fraud", fontsize=15) plt.ylabel("Count", fontsize=15) plt.show()
我們可以看到,這是一組明顯的不平衡數據集,參與過詐騙的用戶量遠遠少沒參與過詐騙的用戶,這意味著我們在建模前,首先要對數據集的不平衡性進行處理,否則模型會始終傾向于將用戶分入非詐騙組。
我們繼續分析另外兩個明確定義的變量,下圖是經過log處理后的消費額Amount與Class的箱型圖,從圖中我們可以看到,信用卡詐騙用戶的消費額范圍更廣,且IQR明顯高于非詐騙用戶,但最高的消費額存在于非詐騙組。
df['Amount_log'] = np.log(df['Amount'] + 0.01) # engineer the data for better visualization plt.figure(figsize=(7,5)) sns.boxplot(x = "Class", y = "Amount_log", data = df) plt.show()
最后我們分析時間與class的關系,
在不同的時間維度上,詐騙組和非詐騙組的消費額雖都整體偏低,但分布都非常均勻,該圖顯示時間與是否詐騙沒有什么明顯的關系。在之后建模時,我們可以考慮刪除時間變量。
我們現在再來看看變量之間的相關性
df_corr = df.corr() plt.figure(figsize=(7,5)) sns.heatmap(df_corr, cmap="YlGnBu") plt.title('Heatmap correlation') plt.show()
從上圖我們可以看出,絕大多數的變量之間都沒有相關關系,這也側面證明了變量也確實經過了PCA,不需要再次進行PCA的處理。
建模
首先,我們需要將數據集分為訓練集和測試集兩部分
X = df.drop(["Amount_log", "Time", "Class"],axis=1) y = df["Class"] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 12)處理不平衡數據有多種方法,考慮到訓練時間問題(因為我們這里選擇的是需要較長訓練時間的SVM),這里我們選擇undersampling,從多數集中抽樣,使詐騙組和非詐騙組中的用戶量成為同一個量級
undersampling_train = pd.concat([X_train,y_train],axis=1) undersampling_train_nonfraud = undersampling_train[undersampling_train['Class']==0].sample(300) undersampling_train_fraud = undersampling_train[undersampling_train['Class']==1] undersampling_train_total = pd.concat([undersampling_train_nonfraud,undersampling_train_fraud],axis=0) undersampling_X = undersampling_train_total.drop("Class",axis=1) undersampling_y = undersampling_train_total["Class"]我們選擇的方法是SVM
def confusion_matrix_1(CM):fig, ax = plot_confusion_matrix(conf_mat=CM)plt.title("The Confusion Matrix 1 of Undersampled dataset")plt.ylabel("Actual")plt.xlabel("Predicted")plt.show()print("The accuracy is "+str((CM[1,1]+CM[0,0])/(CM[0,0] + CM[0,1]+CM[1,0] + CM[1,1])*100) + " %")print("The recall from the confusion matrix is "+ str(CM[1,1]/(CM[1,0] + CM[1,1])*100) +" %")ss = SVC(kernel="linear") ss.fit(undersampling_X,undersampling_y) y_pred = ss.predict(X_test) cmss = confusion_matrix(y_test, y_pred) confusion_matrix_1(cmss) The accuracy is 95.20519086542512 % The recall from the confusion matrix is 87.09677419354838 %95%的正確率看起來還不錯,但我們在分析信用卡詐騙問題時,更多是用recall來衡量模型的準確度,而87%的recall看起來就有提升的空間了
調參
我們嘗試使用GridSearchCV來調整SVM的參數
turned_parameters = [{'kernel':['linear','rbf','poly'],'gamma':['auto'],'C': [1,10,100,1000]}] svm = GridSearchCV(SVC(), turned_parameters,cv=5,scoring='recall') svm.fit(undersampling_X, undersampling_y) print("Best parameters set found on Training dataset:") print() print(svm.best_params_)由GridSearchCV,最合適的參數為{‘C’: 100, ‘gamma’: ‘auto’, ‘kernel’: ‘rbf’},我們使用這組參數來重新對數據集進行建模
{'C': 100, 'gamma': 'auto', 'kernel': 'rbf'} The accuracy is 82.39796634925986 % The recall from the confusion matrix is 94.35483870967742 %
雖然總的正確率下降,但94%的recall要明顯優于原模型,故新模型要更加貼合數據。
總結
以上是生活随笔為你收集整理的用svm预测信用卡诈骗的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频去水印工具推荐-视频去水印步骤
- 下一篇: 中兴微电子招贤纳士