Datawhale学习笔记【阿里云天池 金融风控-贷款违约预测】Task2 数据分析

赛题数据及背景

阿里云天池学习赛【金融风控-贷款违约预测】
https://tianchi.aliyun.com/competition/entrance/531830/introduction

python库的导入

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
import warnings
warnings.filterwarnings('ignore')

以上库如果没有则需要安装,安装分三种情况:

  1. 只装了一个环境:pip install xxxxx
  2. 装了python2和python3的安装方式分别为:pip2 install xxxxx / pip3 install xxxxx
  3. 直接在jupyter notebook中安装:!pip install xxxxx

提示:默认安装的pip使用的是国外的源,下载速度很慢,这里推荐使用国内的镜像源来下载库。

国内镜像源网址及使用方法

镜像名称镜像网址
阿里云http://mirrors.aliyun.com/pypi/simple/
豆瓣http://pypi.douban.com/simple/
清华大学https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学http://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学http://pypi.hustunique.com/

镜像使用方法

  1. 临时使用此镜像下载库
    本文仅介绍临时使用
    按照以上安装方法(举例第一种情况):pip install xxxxx -i http://mirrors.aliyun.com/pypi/simple/
    如上所示在安装命令后带上 -i 镜像网址 即可从选择的镜像网址下载对应的库文件。
  2. 永久使用此镜像下载库

文件读取

train = pd.read_csv('./train.csv')
test = pd.read_csv('./testA.csv')

小提示:如果发现数据与列数没有对齐或者出现错误的情况
尝试一下加入 pd.read_csv(’./train.csv’,sep=’,’)
sep后面带上分隔符

数据的总体了解

查看数据集的样本个数和原始特征维度

test.shape
(200000, 48)
train.shape
(800000, 47)
train.columns
Index(['id', 'loanAmnt', 'term', 'interestRate', 'installment', 'grade',
,       'subGrade', 'employmentTitle', 'employmentLength', 'homeOwnership',
,       'annualIncome', 'verificationStatus', 'issueDate', 'isDefault',
,       'purpose', 'postCode', 'regionCode', 'dti', 'delinquency_2years',
,       'ficoRangeLow', 'ficoRangeHigh', 'openAcc', 'pubRec',
,       'pubRecBankruptcies', 'revolBal', 'revolUtil', 'totalAcc',
,       'initialListStatus', 'applicationType', 'earliesCreditLine', 'title',
,       'policyCode', 'n0', 'n1', 'n2', 'n2.1', 'n4', 'n5', 'n6', 'n7', 'n8',
,       'n9', 'n10', 'n11', 'n12', 'n13', 'n14'],
,      dtype='object')

具体的标签对应说明,比赛介绍里有给出,task1赛题理解也有列出,这里就不再赘述。

查看数据集中特征缺失值,唯一值等

检查缺失值

#详细查询
train.isnull().info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 800000 entries, 0 to 799999
Data columns (total 47 columns):
id                    800000 non-null int64
loanAmnt              800000 non-null float64
term                  800000 non-null int64
interestRate          800000 non-null float64
installment           800000 non-null float64
grade                 800000 non-null object
subGrade              800000 non-null object
employmentTitle       799999 non-null float64
employmentLength      753201 non-null object
homeOwnership         800000 non-null int64
annualIncome          800000 non-null float64
verificationStatus    800000 non-null int64
issueDate             800000 non-null datetime64[ns]
isDefault             800000 non-null int64
purpose               800000 non-null int64
postCode              799999 non-null float64
regionCode            800000 non-null int64
dti                   799761 non-null float64
delinquency_2years    800000 non-null float64
ficoRangeLow          800000 non-null float64
ficoRangeHigh         800000 non-null float64
openAcc               800000 non-null float64
pubRec                800000 non-null float64
pubRecBankruptcies    799595 non-null float64
revolBal              800000 non-null float64
revolUtil             799469 non-null float64
totalAcc              800000 non-null float64
initialListStatus     800000 non-null int64
applicationType       800000 non-null int64
earliesCreditLine     800000 non-null object
title                 799999 non-null float64
policyCode            800000 non-null float64
n0                    759730 non-null float64
n1                    759730 non-null float64
n2                    759730 non-null float64
n2.1                  759730 non-null float64
n4                    766761 non-null float64
n5                    759730 non-null float64
n6                    759730 non-null float64
n7                    759730 non-null float64
n8                    759729 non-null float64
n9                    759730 non-null float64
n10                   766761 non-null float64
n11                   730248 non-null float64
n12                   759730 non-null float64
n13                   759730 non-null float64
n14                   759730 non-null float64
dtypes: datetime64[ns](1), float64(33), int64(9), object(4)
memory usage: 286.9+ MB
#查询每个列,输出列名称以及该列为nan的数量
train.isnull().sum()
id                        0
loanAmnt                  0
term                      0
interestRate              0
installment               0
grade                     0
subGrade                  0
employmentTitle           1
employmentLength      46799
homeOwnership             0
annualIncome              0
verificationStatus        0
issueDate                 0
isDefault                 0
purpose                   0
postCode                  1
regionCode                0
dti                     239
delinquency_2years        0
ficoRangeLow              0
ficoRangeHigh             0
openAcc                   0
pubRec                    0
pubRecBankruptcies      405
revolBal                  0
revolUtil               531
totalAcc                  0
initialListStatus         0
applicationType           0
earliesCreditLine         0
title                     1
policyCode                0
n0                    40270
n1                    40270
n2                    40270
n2.1                  40270
n4                    33239
n5                    40270
n6                    40270
n7                    40270
n8                    40271
n9                    40270
n10                   33239
n11                   69752
n12                   40270
n13                   40270
n14                   40270
dtype: int64
缺失值处理方式

根据比赛情况我选择的处理方式【简单粗暴】

#80w数据删除后还剩60多w数据
#删除有nan的行
train = train.dropna()

特征值查询

这里推荐先使用以下代码进行特征值类别的查询,方便数据挖掘者进行判断
简单来说就是看这个类别有多少个值是不一样的

print(train.apply(pd.Series.nunique, axis = 0))
id                    686195
loanAmnt                1529
term                       2
interestRate             356
installment            66414
grade                      7
subGrade                  35
employmentTitle       217873
employmentLength          11
homeOwnership              6
annualIncome           35865
verificationStatus         3
issueDate                 77
isDefault                  2
purpose                   14
postCode                 927
regionCode                51
dti                     5885
delinquency_2years        30
ficoRangeLow              38
ficoRangeHigh             38
openAcc                   73
pubRec                    32
pubRecBankruptcies        11
revolBal               66702
revolUtil               1230
totalAcc                 134
initialListStatus          2
applicationType            2
earliesCreditLine        704
title                  24221
policyCode                 1
n0                        39
n1                        30
n2                        43
n2.1                      43
n4                        43
n5                        64
n6                       106
n7                        67
n8                       100
n9                        43
n10                       75
n11                        5
n12                        5
n13                       28
n14                       30
dtype: int64

极值查询

print(train['annualIncome'].describe().astype(np.int))
count      800000
mean        76133
std         68947
min             0
25%         45600
50%         65000
75%         90000
max      10999200
Name: annualIncome, dtype: int64
极值处理

删除一些极端的值,增加模型泛化能力【样本中出现了一些年收入过千万以上的,如果加入到模型的训练中会影响模型泛化能力(因为不是所有人都可以年入过千万)】
以下代码只是举个例子,可以根据自己判断进行相应修改。

for variable in ['annualIncome']:
    train=train[train[variable]<train[variable].quantile(0.99)]

热力图

我们可以根据热力图来判断每个标签之间的相关性

corror  = train.corr()
corror
plt.figure(figsize = (50, 50))

# Heatmap of correlations
sns.heatmap(corror, cmap = plt.cm.RdYlBu_r, vmin = -0.25, annot = True, vmax = 0.6)
plt.title('Correlation Heatmap');
plt.show()

热力图

总结

文章中只是记录一部分个人感觉通俗易懂的数据分析方式,借助于各个简单的统计量来对数据整体的了解,分析各个类型变量相互之间的关系,以及用合适的图形可视化出来直观观察。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值