2.逻辑回归(Liner Regression)----- 代码实现

信用评分的机器学习

银行在市场经济中发挥着至关重要的作用。他们决定谁可以获得资金,以什么条件获得资金,可以做出或破坏投资决策。为了使市场和社会发挥作用,个人和公司都需要获得信贷。

信用评分算法对违约概率进行猜测,是银行用来确定是否应该发放贷款的方法。这项比赛要求参赛者通过预测某人在未来两年内遭遇财务困境的概率,提高信用评分的最新水平。数据集

属性信息:
在这里插入图片描述
Read the data into Pandas

import pandas as pd

pd.set_option('display.max_columns', 500)
import zipfile
with zipfile.ZipFile('KaggleCredit2.csv.zip', 'r') as z:   ##读取zip里的文件
    f = z.open('KaggleCredit2.csv')
    data = pd.read_csv(f, index_col=0)
data.head()

data.shape

data.isnull().sum(axis=0)


data.dropna(inplace=True)   ##去掉为空的数据
data.shape


y = data['SeriousDlqin2yrs']
X = data.drop('SeriousDlqin2yrs', axis=1)


y.mean() ##求取均值


#把数据切分成训练集和测试集

from sklearn import model_selection
x_tran,x_test,y_tran,y_test=model_selection.train_test_split(X,y,test_size=0.2)
print(x_test.shape)


#使用logistic regression/决策树/SVM/KNN...等sklearn分类算法进行分类,尝试查sklearn API了解模型参数含义,调整不同的参数。
from sklearn.linear_model import LogisticRegression
## https://blog.csdn.net/sun_shengyun/article/details/53811483
lr=LogisticRegression(multi_class='ovr',solver='sag',class_weight='balanced')
lr.fit(x_tran,y_tran)
score=lr.score(x_tran,y_tran)
print(score) ##最好的分数是1

#在测试集上进行预测,计算准确度
from sklearn.metrics import accuracy_score
## https://blog.csdn.net/qq_16095417/article/details/79590455
train_score=accuracy_score(y_tran,lr.predict(x_tran))
test_score=lr.score(x_test,y_test)
print('训练集准确率:',train_score)
print('测试集准确率:',test_score)


#查看sklearn的官方说明,了解分类问题的评估标准,并对此例进行评估。
##召回率
from sklearn.metrics import recall_score
train_recall=recall_score(y_tran,lr.predict(x_tran),average='macro')
test_recall=recall_score(y_test,lr.predict(x_test),average='macro')
print('训练集召回率:',train_recall)
print('测试集召回率:',test_recall)


#银行通常会有更严格的要求,因为fraud带来的后果通常比较严重,一般我们会调整模型的标准。
#比如在logistic regression当中,一般我们的概率判定边界为0.5,但是我们可以把阈值设定低一些,来提高模型的“敏感度”,试试看把阈值设定为0.3,再看看这时的评估指标(主要是准确率和召回率)。
#tips:sklearn的很多分类模型,predict_prob可以拿到预估的概率,可以根据它和设定的阈值大小去判断最终结果(分类类别)
import numpy as np
y_pro=lr.predict_proba(x_test) ##获取预测概率值
y_prd2 = [list(p>=0.3).index(1) for i,p in enumerate(y_pro)]   ##设定0.3阈值,把大于0.3的看成1分类。
train_score=accuracy_score(y_test,y_prd2)
print(train_score)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unknown To Known

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值