文章目录

  • 一、相关库
  • 二、数据读取
  • 三、数据清洗——删除无关、重复数据
  • 四、数据清洗——类型转换
    • 1、数据集划分
    • 2、缺失值处理
    • 3、异常值处理
    • 4、离散特征编码
    • 5、日期特征处理
    • 6、特征组合
  • 五、数据集划分
  • 六、模型构建
  • 七、模型评估

数据传送门(与之前的不同): https://pan.baidu.com/s/1G1b2QJjYkkk7LDfGorbj5Q

目标:数据集是金融数据(非脱敏),要预测贷款用户是否会逾期。表格中 “status” 是结果标签:0表示未逾期,1表示逾期。

任务:数据类型转换和缺失值处理(尝试不同的填充看效果)以及及其他你能借鉴的数据探索。

一、相关库

# -*- coding:utf-8 -*-
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
import xgboost as xgb
import lightgbm as lgb
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt

二、数据读取

file_path = "data.csv"
data = pd.read_csv(file_path, encoding='gbk')
print(data.head())
print(data.shape)

结果输出

   Unnamed: 0   custid       ...        latest_query_day loans_latest_day
0           5  2791858       ...                    12.0             18.0
1          10   534047       ...                     4.0              2.0
2          12  2849787       ...                     2.0              6.0
3          13  1809708       ...                     2.0              4.0
4          14  2499829       ...                    22.0            120.0[5 rows x 90 columns]
(4754, 90)

遇到的问题:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte

原因:‘utf-8’不能解码字节(0xbf),也就是这个字节超出了utf-8的表示范围了
解决方法:显式添加编码方式。亲测:encoding=‘gbk’ 或’ISO-8859-1’编码。

三、数据清洗——删除无关、重复数据

## 删除与个人身份相关的列
data.drop(['custid', 'trade_no', 'bank_card_no', 'id_name'], axis=1, inplace=True)## 删除列中数据均相同的列
X = data.drop(labels='status',axis=1)
L = []
for col in X:if len(X[col].unique()) == 1:L.append(col)
for col in L:X.drop(col, axis=1, inplace=True)

四、数据清洗——类型转换

1、数据集划分

划分不同数据类型:数值型、非数值型、标签
使用:Pandas对象有 select_dtypes() 方法可以筛选出特定数据类型的特征
参数:include 包括(默认);exclude 不包括

X_num = X.select_dtypes(include='number').copy()
X_str = X.select_dtypes(exclude='number').copy()
y = data['status']

2、缺失值处理

发现缺失值方法:缺失个数、缺失率

# 使用缺失率(可以了解比重)并按照值降序排序 ascending=False
X_num_miss = (X_num.isnull().sum() / len(X_num)).sort_values(ascending=False)
print(X_num_miss.head())
print('----------' * 5)
X_str_miss = (X_str.isnull().sum() / len(X_str)).sort_values(ascending=False)
print(X_str_miss.head())

输出结果

student_feature                     0.630627
cross_consume_count_last_1_month    0.089609
latest_one_month_apply              0.063946
query_finance_count                 0.063946
latest_six_month_apply              0.063946
dtype: float64
--------------------------------------------------
latest_query_time          0.063946
loans_latest_time          0.062474
reg_preference_for_trad    0.000421
dtype: float64

分析:缺失率最高的特征是student_feature,为 63.0627% > 50% ,其他的特征缺失率都在10%以下。

  • 高缺失率特征处理:EM插补、多重插补。
    ==》由于两种方法比较复杂,这里先将缺失值归为一类,用0填充。
  • 其他特征:平均数、中位数、众数…
## student_feature特征处理设置为0
X_num.fillna(0, inplace = True)## 其他特征插值: 众数
X_num.fillna(X_num.mode().iloc[0, :], inplace=True)
X_str.fillna(X_str.mode().iloc[0, :], inplace=True)

3、异常值处理

  • 箱型图的四分位距(IQR)
## 异常值处理:箱型图的四分位距(IQR)
def iqr_outlier(x, thre = 1.5):x_cl = x.copy()q25, q75 = x.quantile(q = [0.25, 0.75])iqr = q75 - q25top = q75 + thre * iqrbottom = q25 - thre * iqrx_cl[x_cl > top] = topx_cl[x_cl < bottom] = bottomreturn  x_cl
X_num_cl = pd.DataFrame()
for col in X_num.columns:X_num_cl[col] = iqr_outlier(X_num[col])
X_num = X_num_cl

4、离散特征编码

  • 序号编码:用于有大小关系的数据
  • one-hot编码:用于无序关系的数据
X_str_oh = pd.get_dummies(X_str['reg_preference_for_trad'])

5、日期特征处理

X_date = pd.DataFrame()
X_date['latest_query_time_year'] = pd.to_datetime(X_str['latest_query_time']).dt.year
X_date['latest_query_time_month'] = pd.to_datetime(X_str['latest_query_time']).dt.month
X_date['latest_query_time_weekday'] = pd.to_datetime(X_str['latest_query_time']).dt.weekday
X_date['loans_latest_time_year'] = pd.to_datetime(X_str['loans_latest_time']).dt.year
X_date['loans_latest_time_month'] = pd.to_datetime(X_str['loans_latest_time']).dt.month
X_date['loans_latest_time_weekday'] = pd.to_datetime(X_str['loans_latest_time']).dt.weekday

6、特征组合

X = pd.concat([X_num, X_str_oh, X_date], axis=1, sort=False)
print(X.shape)

五、数据集划分

## 预处理:标准化
# X_std = StandardScaler().fit(X)## 划分数据集
X_std_train, X_std_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2019)

六、模型构建

## 模型1:Logistic Regression
lr = LogisticRegression()
lr.fit(X_std_train, y_train)## 模型2:Decision Tree
dtc = DecisionTreeClassifier(max_depth=8)
dtc.fit(X_std_train,y_train)# ## 模型3:SVM
# svm = SVC(kernel='linear',probability=True)
# svm.fit(X_std_train,y_train)## 模型4:Random Forest
rfc = RandomForestClassifier()
rfc.fit(X_std_train,y_train)## 模型5:XGBoost
xgbc = xgb.XGBClassifier()
xgbc.fit(X_std_train,y_train)## 模型6:LightGBM
lgbc = lgb.LGBMClassifier()
lgbc.fit(X_std_train,y_train)

七、模型评估

## 模型评估
def model_metrics(clf, X_test,  y_test):y_test_pred = clf.predict(X_test)y_test_prob = clf.predict_proba(X_test)[:, 1]accuracy = accuracy_score(y_test, y_test_pred)print('The accuracy: ', accuracy)precision = precision_score(y_test, y_test_pred)print('The precision: ', precision)recall = recall_score(y_test, y_test_pred)print('The recall: ', recall)f1_score = recall_score(y_test, y_test_pred)print('The F1 score: ', f1_score)print('----------------------------------')# roc_auc_score = roc_auc_score(y_test, y_test_prob)# print('The AUC of: ', roc_auc_score)model_metrics(lr,X_std_test,y_test)
model_metrics(dtc,X_std_test,y_test)
model_metrics(rfc,X_std_test,y_test)
model_metrics(xgbc,X_std_test,y_test)
model_metrics(lgbc,X_std_test,y_test)

金融贷款逾期的模型构建5——数据预处理相关推荐

  1. 金融贷款逾期的模型构建6——特征选择

    文章目录 一.IV值 1.概述 2.IV计算 (1)WOE (2)IV 计算 二.实现 0.相关模块 1.IV值 2.Random Forest 3.特征合并 4.模型构建 5.模型评估 数据传送门( ...

  2. 金融贷款逾期的模型构建1

    数据 data_all.csv文件是非原始数据,已经处理过了.数据是金融数据, 我们要做的是预测贷款用户是否会逾期.表格中, status是标签: 0表示未逾期, 1表示逾期. 任务--模型构建 给定 ...

  3. 金融贷款逾期的模型构建2——集成模型

    任务--模型构建 构建随机森林.GBDT.XGBoost和LightGBM这4个模型,并对每一个模型进行评分,评分方式任意,例如准确度和auc值. 1.相关安装资源 随机森林.GBDT均在sklear ...

  4. 金融贷款逾期的模型构建7——模型融合

    文章目录 一.集成学习 1.Bagging 2.Boosting 3.Stacking (1)核心图解 a.构建新的训练集 b.构建新的测试集 c.最终的训练与预测 (2)示例 a.构建新的训练集 b ...

  5. 金融贷款逾期的模型构建4——模型调优

    文章目录 一.任务 二.概述 1.参数说明 2.常用方法 二.实现 1.模块引入 2.模型评估函数 3.数据读取 4.Logistic Regression (1)调参部分 (2)模型评估 5.SVM ...

  6. 金融贷款逾期的模型构建3——模型评估

    文章目录 一.评价指标 1.基本概念 2.准确率(accuracy) 3.精确率(precision) 4.召回率(recall) 5.F1值 6.roc曲线 和 auc值 二.模型评估 1.Logi ...

  7. 一周算法实践---金融贷款逾期模型

    金融贷款逾期模型 1.读取数据 import pandas as pd data_all = pd.read_csv('../data/data_all.csv')` 2.划分数据集 from skl ...

  8. 互联网金融信用评分卡模型构建

    互联网金融信用评分卡模型构建 背景介绍 信用风险计量体系包括主体评级模型和债项评级两部分. 主体评级和债项评级均有一系列评级模型组成,其中主体评级模型可用"四张卡"来表示,分别是A ...

  9. R语言构建文本分类模型:文本数据预处理、构建词袋模型(bag of words)、构建xgboost文本分类模型、基于自定义函数构建xgboost文本分类模型

    R语言构建文本分类模型:文本数据预处理.构建词袋模型(bag of words).构建xgboost文本分类模型.基于自定义函数构建xgboost文本分类模型 目录

最新文章

  1. win10进不了微软服务器,Microsoft帐户无法登录怎么办 Win10微软账户登录不上解决方法...
  2. 云栖专辑|阿里开发者们的第二个感悟:PG大V德哥的使命感与开放心态
  3. vue监听h5页面返回健(微信和支付宝浏览器亲测):
  4. QML和C ++之间的数据类型转换---枚举
  5. 使用intellij查看scala变量的具体类型
  6. 机器学习在SAP Cloud for Customer中的应用
  7. Vector shrink 请求容器降低其容量和size匹配 shrink_to_fit();
  8. java 模块化_Java模块化方法–模块,模块,模块
  9. Esxi遇到问题汇总。
  10. iOS 给测试人员测试手机APP的四种方法:真机运行(略),打ipa包,(testFlighe)邮件,蒲公英(二)testflight
  11. VS2010开发环境之使用技巧
  12. 大数据产品概念和分类
  13. App登陆页面必备(一)
  14. 安装pip的三种方法
  15. android app与gprs通信,gprs连接管理app
  16. 软件实施人员具备的技能和素养
  17. 关于牛顿-欧拉法的外推和内推的理解
  18. 学习秦始皇 五招打造无敌团队
  19. php模拟登陆正方教务管理系统(thinkPHP5.0)
  20. BlazeDS是什么?

热门文章

  1. 并发集合和普通集合以及安全集合的区别
  2. JUC系列(二)回顾Synchronized关键字
  3. python吃内存还是cpu_Python2 得到 CPU 和内存信息要怎么实现呢?
  4. unix查找的字符串包含特殊字符_python3从零学习-5.4.7、Unix风格路径名模式扩展glob...
  5. python做数据分析难么_做统计学习,数据分析应该学Python还是R?
  6. docker mysql映射端口映射_docker的简单操作和端口映射
  7. 怎样能用计算机打出表白数字,怎么用数字表白?盘点数字表白暗语
  8. 运行时间_一种简单、实用的测量程序运行时间的方法
  9. _Linux系统编程—信号集操作函数
  10. php对提交数据转码,如何使用php程序实现媒体转码消息的接收