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

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

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

3、欺诈与时间序列分布关系

  1. # 查看二者的描述性统计,与时间的序列分布关系 
  2. print('Normal') 
  3. print(crecreditcard_data. 
  4.  Time[crecreditcard_data.Class == 0].describe()) 
  5. print('-'*25) 
  6. print('Fraud') 
  7. print(crecreditcard_data. 
  8.  Time[crecreditcard_data.Class == 1].describe()) 
  9. Normal 
  10. count 284315.000000 
  11. mean 94838.202258 
  12. std 47484.015786 
  13. min 0.000000 
  14. 25% 54230.000000 
  15. 50% 84711.000000 
  16. 75% 139333.000000 
  17. max 172792.000000 
  18. Name: Time, dtype: float64 
  19. ------------------------- 
  20. Fraud 
  21. count 492.000000 
  22. mean 80746.806911 
  23. std 47835.365138 
  24. min 406.000000 
  25. 25% 41241.500000 
  26. 50% 75568.500000 
  27. 75% 128483.000000 
  28. max 170348.000000 
  29. Name: Time, dtype: float64 
  30. f,(ax1,ax2)=plt.subplots(2,1,sharex=True,figsize=(12,6)) 
  31. bins=50 
  32. ax1.hist(crecreditcard_data.Time[crecreditcard_data.Class == 1],bins=bins) 
  33. ax1.set_title('欺诈(Fraud))',fontsize=22) 
  34. ax1.set_ylabel('交易量',fontsize=15) 
  35. ax2.hist(crecreditcard_data.Time[crecreditcard_data.Class == 0],bins=bins) 
  36. ax2.set_title('正常(Normal',fontsize=22) 
  37. plt.xlabel('时间(单位:秒)',fontsize=15) 
  38. plt.xticks(fontsize=15) 
  39. plt.ylabel('交易量',fontsize=15) 
  40. # plt.yticks(fontsize=22) 
  41. plt.show() 
Python分析信用卡反欺诈!骗我程序员,不存在的

欺诈与时间并没有必然联系,不存在周期性;

正常交易有明显的周期性,有类似双峰这样的趋势。

4、欺诈与金额的关系和分布情况

  1. print('欺诈') 
  2. print(crecreditcard_data.Amount[crecreditcard_data.Class ==1].describe()) 
  3. print('-'*25) 
  4. print('正常交易') 
  5. print(crecreditcard_data.Amount[crecreditcard_data.Class==0].describe()) 
  6. 欺诈 
  7. count 492.000000 
  8. mean 122.211321 
  9. std 256.683288 
  10. min 0.000000 
  11. 25% 1.000000 
  12. 50% 9.250000 
  13. 75% 105.890000 
  14. max 2125.870000 
  15. Name: Amount, dtype: float64 
  16. ------------------------- 
  17. 正常交易 
  18. count 284315.000000 
  19. mean 88.291022 
  20. std 250.105092 
  21. min 0.000000 
  22. 25% 5.650000 
  23. 50% 22.000000 
  24. 75% 77.050000 
  25. max 25691.160000 
  26. Name: Amount, dtype: float64 
  27. f,(ax1,ax2)=plt.subplots(2,1,sharex=True,figsize=(12,6)) 
  28. bins=30 
  29. ax1.hist(crecreditcard_data.Amount[crecreditcard_data.Class == 1],bins=bins) 
  30. ax1.set_title('欺诈(Fraud)',fontsize=22) 
  31. ax1.set_ylabel('交易量',fontsize=15) 
  32. ax2.hist(crecreditcard_data.Amount[crecreditcard_data.Class == 0],bins=bins) 
  33. ax2.set_title('正常(Normal)',fontsize=22) 
  34. plt.xlabel('金额($)',fontsize=15) 
  35. plt.xticks(fontsize=15) 
  36. plt.ylabel('交易量',fontsize=15) 
  37. plt.yscale('log') 
  38. plt.show() 
Python分析信用卡反欺诈!骗我程序员,不存在的

金额普遍较低,可见金额这一列的数据对分析的参考价值不大。

5、查看各个自变量(V1-V29)与因变量的关系

看看各个变量与正常、欺诈之间是否存在联系,为了更直观展示,通过distplot图来逐个判断,如下:

  1. features=[x for x in crecreditcard_data.columns  
  2.  if x not in ['Time','Amount','Class']] 
  3. plt.figure(figsize=(12,28*4)) 
  4. gs =gridspec.GridSpec(28,1) 
  5. import warnings 
  6. warnings.filterwarnings('ignore') 
  7. for i,cn in enumerate(crecreditcard_data[v_features]): 
  8.  ax=plt.subplot(gs[i]) 
  9.  sns.distplot(crecreditcard_data[cn][crecreditcard_data.Class==1],bins=50,color='red') 
  10.  sns.distplot(crecreditcard_data[cn][crecreditcard_data.Class==0],bins=50,color='green') 
  11.  ax.set_xlabel('') 
  12.  ax.set_title('直方图:'+str(cn)) 
  13. plt.savefig('各个变量与class的关系.png',transparent=False,bbox_inches='tight') 
  14. plt.show() 
Python分析信用卡反欺诈!骗我程序员,不存在的

红色表示欺诈,绿色表示正常

  • 两个分布的交叉面积越大,欺诈与正常的区分度最小,如V15;
  • 两个分布的交叉面积越小,则该变量对因变量的影响越大,如V14;

(编辑:源码网)

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

热点阅读