Datawhale 学习笔记【阿里云天池 金融风控 - 贷款违约预测】Task2 数据分析
- 2023-01-19 浙江
本文字数:4165 字
阅读完需:约 14 分钟
赛题数据及背景
阿里云天池学习赛【金融风控-贷款违约预测】https://tianchi.aliyun.com/competition/entrance/531830/introduction
python 库的导入
import pandas as pd import numpy as npimport matplotlib.pyplot as pltimport seaborn as snsimport datetimeimport warningswarnings.filterwarnings('ignore')
以上库如果没有则需要安装,安装分三种情况:
只装了一个环境:pip install xxxxx
装了 python2 和 python3 的安装方式分别为:pip2 install xxxxx / pip3 install xxxxx
直接在 jupyter notebook 中安装:!pip install xxxxx
提示:默认安装的 pip 使用的是国外的源,下载速度很慢,这里推荐使用国内的镜像源来下载库。
国内镜像源网址及使用方法
镜像使用方法
临时使用此镜像下载库本文仅介绍临时使用按照以上安装方法(举例第一种情况):pip install xxxxx -i http://mirrors.aliyun.com/pypi/simple/如上所示在安装命令后带上 -i 镜像网址 即可从选择的镜像网址下载对应的库文件。
永久使用此镜像下载库
文件读取
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 799999Data columns (total 47 columns):id 800000 non-null int64loanAmnt 800000 non-null float64term 800000 non-null int64interestRate 800000 non-null float64installment 800000 non-null float64grade 800000 non-null objectsubGrade 800000 non-null objectemploymentTitle 799999 non-null float64employmentLength 753201 non-null objecthomeOwnership 800000 non-null int64annualIncome 800000 non-null float64verificationStatus 800000 non-null int64issueDate 800000 non-null datetime64[ns]isDefault 800000 non-null int64purpose 800000 non-null int64postCode 799999 non-null float64regionCode 800000 non-null int64dti 799761 non-null float64delinquency_2years 800000 non-null float64ficoRangeLow 800000 non-null float64ficoRangeHigh 800000 non-null float64openAcc 800000 non-null float64pubRec 800000 non-null float64pubRecBankruptcies 799595 non-null float64revolBal 800000 non-null float64revolUtil 799469 non-null float64totalAcc 800000 non-null float64initialListStatus 800000 non-null int64applicationType 800000 non-null int64earliesCreditLine 800000 non-null objecttitle 799999 non-null float64policyCode 800000 non-null float64n0 759730 non-null float64n1 759730 non-null float64n2 759730 non-null float64n2.1 759730 non-null float64n4 766761 non-null float64n5 759730 non-null float64n6 759730 non-null float64n7 759730 non-null float64n8 759729 non-null float64n9 759730 non-null float64n10 766761 non-null float64n11 730248 non-null float64n12 759730 non-null float64n13 759730 non-null float64n14 759730 non-null float64dtypes: datetime64[ns](1), float64(33), int64(9), object(4)memory usage: 286.9+ MB
#查询每个列,输出列名称以及该列为nan的数量train.isnull().sum()
id 0loanAmnt 0term 0interestRate 0installment 0grade 0subGrade 0employmentTitle 1employmentLength 46799homeOwnership 0annualIncome 0verificationStatus 0issueDate 0isDefault 0purpose 0postCode 1regionCode 0dti 239delinquency_2years 0ficoRangeLow 0ficoRangeHigh 0openAcc 0pubRec 0pubRecBankruptcies 405revolBal 0revolUtil 531totalAcc 0initialListStatus 0applicationType 0earliesCreditLine 0title 1policyCode 0n0 40270n1 40270n2 40270n2.1 40270n4 33239n5 40270n6 40270n7 40270n8 40271n9 40270n10 33239n11 69752n12 40270n13 40270n14 40270dtype: int64
缺失值处理方式
根据比赛情况我选择的处理方式【简单粗暴】
#80w数据删除后还剩60多w数据#删除有nan的行train = train.dropna()
特征值查询
这里推荐先使用以下代码进行特征值类别的查询,方便数据挖掘者进行判断简单来说就是看这个类别有多少个值是不一样的
print(train.apply(pd.Series.nunique, axis = 0))
id 686195loanAmnt 1529term 2interestRate 356installment 66414grade 7subGrade 35employmentTitle 217873employmentLength 11homeOwnership 6annualIncome 35865verificationStatus 3issueDate 77isDefault 2purpose 14postCode 927regionCode 51dti 5885delinquency_2years 30ficoRangeLow 38ficoRangeHigh 38openAcc 73pubRec 32pubRecBankruptcies 11revolBal 66702revolUtil 1230totalAcc 134initialListStatus 2applicationType 2earliesCreditLine 704title 24221policyCode 1n0 39n1 30n2 43n2.1 43n4 43n5 64n6 106n7 67n8 100n9 43n10 75n11 5n12 5n13 28n14 30dtype: int64
极值查询
print(train['annualIncome'].describe().astype(np.int))
count 800000mean 76133std 68947min 025% 4560050% 6500075% 90000max 10999200Name: annualIncome, dtype: int64
极值处理
删除一些极端的值,增加模型泛化能力【样本中出现了一些年收入过千万以上的,如果加入到模型的训练中会影响模型泛化能力(因为不是所有人都可以年入过千万)】以下代码只是举个例子,可以根据自己判断进行相应修改。
for variable in ['annualIncome']: train=train[train[variable]<train[variable].quantile(0.99)]
热力图
我们可以根据热力图来判断每个标签之间的相关性
corror = train.corr()corrorplt.figure(figsize = (50, 50))
# Heatmap of correlationssns.heatmap(corror, cmap = plt.cm.RdYlBu_r, vmin = -0.25, annot = True, vmax = 0.6)plt.title('Correlation Heatmap');plt.show()
总结
文章中只是记录一部分个人感觉通俗易懂的数据分析方式,借助于各个简单的统计量来对数据整体的了解,分析各个类型变量相互之间的关系,以及用合适的图形可视化出来直观观察。
一颗小树
计算机科学与技术 | 大三在读程序员 2021-04-28 加入
【坐标】浙江温州 【技能】python | c++ | js | html 【技术爱好】探究有趣有价值的技术 【游戏爱好】王者荣耀、多多自走棋 【树言树语】敢于仰望星空的人,眼中自有光芒在。










评论