加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 网站设计 > 教程 > 正文

Python分析信用卡反欺诈!骗我程序员,不存在的

发布时间:2019-10-14 02:45:15 所属栏目:教程 来源:一枚程序媛呀
导读:副标题#e# 前言: 本文研究的是大数据量(284807条数据)下模型选择的问题,也参考了一些文献,但大多不够清晰,因此吐血整理本文,希望对大家有帮助; 本文试着从数据分析师的角度,设想拿到数据该如何寻找规律、选哪种模型来构建反欺诈模型?的角度来分析,以

下面我们看看各个单变量与class的相关性分析,为更直观展示,直接作图,如下:

  1. # 各个变量的矩阵分布 
  2. crecreditcard_data.hist(figsize=(15,15),bins=50) 
  3. plt.show() 
Python分析信用卡反欺诈!骗我程序员,不存在的

6、三种方法建模并分析

本部分将应用逻辑回归、随机森林、支持向量SVM三种方法建模分析,分别展开如下:

准备数据:

  1. # 先把数据分为欺诈组和正常组,然后按比例生产训练和测试数据集 
  2. # 分组 
  3. Fraud=crecreditcard_data[crecreditcard_data.Class == 1] 
  4. Normal=crecreditcard_data[crecreditcard_data.Class == 0] 
  5. # 训练特征集 
  6. x_train=Fraud.sample(frac=0.7) 
  7. x_train=pd.concat([x_train,Normal.sample(frac=0.7)],axis=0) 
  8. # 测试特征集 
  9. x_test=crecreditcard_data.loc[~crecreditcard_data.index.isin(x_train.index)] 
  10. # 标签集 
  11. y_train=x_train.Class 
  12. y_test=x_test.Class 
  13. # 去掉特征集里的标签和时间列 
  14. x_train=x_train.drop(['Class','Time'],axis=1) 
  15. x_test=x_test.drop(['Class','Time'],axis=1) 
  16. # 查看数据结构 
  17. print(x_train.shape,y_train.shape, 
  18.  'n',x_test.shape,y_test.shape) 
  19. (199364, 29) (199364,)  
  20.  (85443, 29) (85443,) 

6.1 逻辑回归方法

  1. from sklearn import metrics 
  2. import scipy.optimize as op 
  3. from sklearn.linear_model import LogisticRegression 
  4. from sklearn.cross_validation import KFold,cross_val_score 
  5. from sklearn.metrics import (precision_recall_curve, 
  6.  auc,roc_auc_score, 
  7.  roc_curve,recall_score, 
  8.  classification_report) 
  9. lrmodel = LogisticRegression(penalty='l2') 
  10. lrmodel.fit(x_train, y_train) 
  11. #查看模型 
  12. print('lrmodel') 
  13. print(lrmodel) 
  14. lrmodel 
  15. LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, 
  16.  intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1, 
  17.  penalty='l2', random_state=None, solver='liblinear', tol=0.0001, 
  18.  verbose=0, warm_start=False) 
  19. #查看混淆矩阵 
  20. ypred_lr=lrmodel.predict(x_test) 
  21. print('confusion_matrix') 
  22. print(metrics.confusion_matrix(y_test,ypred_lr)) 
  23. confusion_matrix 
  24. [[85284 11] 
  25.  [ 56 92]] 
  26. #查看分类报告 
  27. print('classification_report') 
  28. print(metrics.classification_report(y_test,ypred_lr)) 
  29. classification_report 
  30.  precision recall f1-score support 
  31.  0 1.00 1.00 1.00 85295 
  32.  1 0.89 0.62 0.73 148 
  33. avg / total 1.00 1.00 1.00 85443 
  34. #查看预测精度与决策覆盖面 
  35. print('Accuracy:%f'%(metrics.accuracy_score(y_test,ypred_lr))) 
  36. print('Area under the curve:%f'%(metrics.roc_auc_score(y_test,ypred_lr))) 
  37. Accuracy:0.999216 
  38. Area under the curve:0.810746 

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读