这是国外那大哥第二次下单,还是很好的完成了,但是第三次单子没有抓住,确实不会,出国留学生就是有钱,我也想(手动滑稽)
In this homework, we use logistic regression to predict the probability of default using incomeand balance on the Default data set. We will also estimate the test error of this logistic regressionmodel using the validation set approach. Do not forget to set a random seed before beginningyour analysis.

在这次作业中,我们使用logistic回归来预测违约的概率,使用默认数据集上的income和balance。我们还将使用validation set方法来估计这个logistic回归模型的测试误差。在开始分析之前,不要忘记设置一个随机种子。

  1. (a) Fit a multiple logistic regression model that uses income and balance to predict the probability of default, using only the observations

1.(A)拟合多元Logistic回归模型,利用收入和平衡来预测.违约概率,只使用观测结果

#导入包
import pandas as pd
from sklearn import metrics
import warnings
warnings.filterwarnings("ignore")
test=pd.read_excel('Default.xlsx')
test.head()
default student balance income
1 No No 729.526495 44361.625074
2 No Yes 817.180407 12106.134700
3 No No 1073.549164 31767.138947
4 No No 529.250605 35704.493935
5 No No 785.655883 38463.495879
#将类别型变量转化为数值变量
def fun(x):if 'No' in x:return 0else:return 1
test['default']=test.apply(lambda x: fun(x['default']),axis=1)
#定义训练集
X=test[['balance','income']]
y=test['default']
from sklearn.linear_model import LogisticRegression
# 准确率
lr_acc=[]
# 构建LogisticRegression模型(默认参数即可),并调用fit进行模型拟合
model = LogisticRegression()
model.fit(X,y)
# 计算LogisticRegression在测试集上的误差率
a=model.predict_proba(X)
# 打印误差率
result=[]
for i in range(len(a)):if a[i][1]>0.5:result.append(1)else:result.append(0)
print('误差: %.4f' % (1-metrics.recall_score(y,result,average='weighted')))
误差: 0.0336

(b) Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:

利用验证集方法,对模型的测试误差进行估计。为此,您必须执行以下步骤:

i. Split the sample set into a training set and a validation set.

将样本集分成训练集和验证集。

from sklearn.model_selection import train_test_split
# 使用train_test_split方法,划分训练集和测试集,指定80%数据为训练集,20%为验证集
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2,random_state=2020)
X_validation, X_test, y_validation, y_test = train_test_split(X_test,y_test, test_size=0.1,random_state=2020)

ii. Fit a multiple logistic regression model using only the training observations.

仅用训练观测值拟合多元Logistic回归模型。

from sklearn.linear_model import LogisticRegression
lr_acc=[]
model = LogisticRegression()
model.fit(X_train,y_train)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,penalty='l2', random_state=None, solver='liblinear', tol=0.0001,verbose=0, warm_start=False)

Obtain a prediction of default status for each individual in the validation set by computing theposterior probability of default for that individual, and classifying the individual to the defaultcategory if the posterior probability equals 0.5.

通过计算该个体的后验违约概率,获得验证集中每个个体的违约状态预测,如果后验概率等于0.5,则将该个体分类为defaultcategory。

a=model.predict_proba(X_validation)
result=[]
for i in range(len(a)):if a[i][1]>0.5:result.append(1)else:result.append(0)
print('误差: %.4f' % (1-metrics.recall_score(y_validation,result,average='weighted')))
误差: 0.0361

© Repeat the process in (b) three times, using three different splits of the observations into a
training set and a validation set. Comment on the results obtained
©在(b)中重复上述过程三次,将观察结果分成训练集和验证集。

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
for i in range(3):X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2,random_state=2020)X_validation, X_test, y_validation, y_test = train_test_split(X_test,y_test, test_size=0.1,random_state=2020)model = LogisticRegression()model.fit(X_train,y_train)a=model.predict_proba(X_validation)result=[]for i in range(len(a)):if a[i][1]>0.5:result.append(1)else:result.append(0)from sklearn import metricsprint('误差: %.4f' % (1-metrics.recall_score(y_validation,result,average='weighted')))
误差: 0.0361
误差: 0.0361
误差: 0.0361

(d) Now consider a logistic regression model that predicts the probability of default using
income, balance, and a dummy variable for student. Estimate the test error for this model using
the validation set approach. Comment on whether or not including a dummy variable for student
leads to a reduction in the test error rate.

现在考虑一个逻辑回归模型,该模型使用收入、余额和学生的虚拟变量来预测违约概率。使用验证集方法估计该模型的测试误差。评论是否包括一个虚拟变量的学生导致降低测试错误率。

def fun(x):if 'No' in x:return 0else:return 1
test['student']=test.apply(lambda x: fun(x['student']),axis=1)
X=test[['balance','income','student']]
y=test['default']
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2,random_state=2020)
X_validation, X_test, y_validation, y_test = train_test_split(X_test,y_test, test_size=0.1,random_state=2020)
model = LogisticRegression()
model.fit(X_train,y_train)
a=model.predict_proba(X_validation)
result=[]
for i in range(len(a)):if a[i][1]>0.5:result.append(1)else:result.append(0)
from sklearn import metrics
print('误差: %.4f' % (1-metrics.recall_score(y_validation,result,average='weighted')))
误差: 0.0361

答:影响不大

20200210_logistic回归来预测违约的概率相关推荐

  1. oracle 逻辑回归,逻辑回归 (Logistic Regression):计算概率

    预计用时:10 分钟 许多问题需要将概率估算值作为输出.逻辑回归是一种极其高效的概率计算机制.实际上,您可以通过下两种方式之一使用返回的概率: "按原样" 转换成二元类别. 我们来 ...

  2. python回归分析预测模型_在Python中如何使用Keras模型对分类、回归进行预测

    姓名:代良全 学号:13020199007 转载自:https://www.jianshu.com/p/83ba11abdffc [嵌牛导读]: 在Python中如何使用Keras模型对分类.回归进行 ...

  3. 神经网络 测试集loss不下降_代码实践 | 全连接神经网络回归---房价预测

    学习目录 阿力阿哩哩:深度学习 | 学习目录​zhuanlan.zhihu.com 前面我们介绍了: 阿力阿哩哩:深度学习开端|全连接神经网络​zhuanlan.zhihu.com 4.7代码实践 & ...

  4. 机器学习(一)——BP、RBF(径向基)、GRNN(广义回归)、PNN(概率)神经网络对比分析(附程序、数据)

    对比分析了几种常见的神经网络<BP.RBF(径向基).GRNN(广义回归).PNN(概率)>,并在文末附代码,想要训练测试数据可以QQ2859340499. 1. BP神经网络: BP神经 ...

  5. r ridge回归_R语言逻辑回归和泊松回归模型对发生交通事故概率建模

    原文链接 http://tecdat.cn/?p=14139 我们考虑风险敞口,计算包含风险敞口的多个数量(经验均值和经验方差)的非参数估计量.如果要对二项式变量建模. 这里的模型如下: 未观察到该期 ...

  6. python 逻辑回归,预测银行客户是否购买定期存款

    问题: 逻辑回归其实是一个二分类问题,预测银行客户是否购买定期存款我们会提出以下一些问题: (1)影响银行客户购买定期存款的因素有哪些 (2)对于类别变量我们应该怎样处理, (3)我们应该怎样进行特征 ...

  7. 数据分析+分类模型预测乳腺癌患病概率

    一.前言     本文利用python预处理数据集,再通过机器学习模型:LR.SGD预测乳腺癌患病概率,对比两个模型的预测效果,选择最优的预测方式. 二.数据集说明 数据集源于威斯康星州临床科学中心. ...

  8. R语言编写自定义函数、评估回归模型预测变量的相对重要性(Relative importance)、通过在所有可能的子模型中添加一个预测变量而获得的R方的平均增加、评估预测变量的重要度、并通过点图可视化

    R语言编写自定义函数.评估回归模型预测变量的相对重要性(Relative importance).通过在所有可能的子模型中添加一个预测变量而获得的R方的平均增加.来评估预测变量的重要程度.并通过点图可 ...

  9. Python使用tpot获取最优模型、将最优模型应用于交叉验证数据集(5折)获取数据集下的最优表现,并将每一折(fold)的预测结果、概率、属于哪一折与测试集标签、结果、概率一并整合输出为结果文件

    Python使用tpot获取最优模型.将最优模型应用于交叉验证数据集(5折)获取数据集下的最优表现,并将每一折(fold)的预测结果.概率.属于哪一折与测试集标签.结果.概率一并整合输出为结果文件 目 ...

  10. 基于京东手机销售数据用回归决策树预测价格

    今天给大家推荐一个数据分析与挖掘的实战项目案例"基于京东手机销售数据用回归决策树预测价格".该项目先基于京东手机销售数据做出一系列分析后,利用回归决策树仅根据手机外部特征进行价格预 ...

最新文章

  1. python储存数据的容器_Python基础四容器类数据
  2. h2 迁移到 mysql_[saiku] 将saiku自带的H2嵌入式数据库迁移到本地mysql数据库
  3. Web前端,高性能优化
  4. SQL Server遍历表的几种方法
  5. Greg and Array CodeForces - 296C(差分数组+线段树)
  6. Python自动化运维之常用模块—logging
  7. python selenium 处理弹窗_转:python selenium 弹出框处理的实现
  8. 【高校宿舍管理系统】第十一章 学生系统
  9. vue 事件调用 传参_vue如何在父组件指定点击事件后向子组件传递参数并调用子组件的事件?...
  10. 酷盘API C# 测试版
  11. springsecurity实现记住我的功能,将用户的登录信息保存到本地浏览器,即使关闭浏览器也不用登录
  12. robocopy 备份_Windows 7系统强大的复制命令robocopy的操作方法介绍
  13. 在韩家炜老师的实验室和家里作客 — 旅美散记之二
  14. 工商行政许可信息爬取及展示系统的设计与实现
  15. 【CSS】从熟悉到更熟悉
  16. JAVA程序设计题解与上机指导 第四版 第8章 Java的图形用户界面设计 8.2 创建“My JFrame”
  17. STM32---外部中断
  18. win11怎样修改开机音乐 windows11修改开机音乐的步骤教程
  19. Consul arch(二) 逆熵 anti-entropy
  20. PostgreSQL-12 通过归档WAL日志同步主备数据

热门文章

  1. http1.0 与 http1.1
  2. C++数据类型之结构体的练习(用到结构体的赋值,利用结构体元素排序以及查找)
  3. 【react】 react---项目的-----------简单路由配置
  4. WEB前端开发规范文档+CSS命名规范
  5. nginx服务器上 font awesome 字体不能正常显示
  6. 网站移植到linux上后常犯的错误
  7. [最小割][Kruskal] Luogu P5039 最小生成树
  8. ehcache讲解及实例
  9. (3)Deep Learning之神经网络和反向传播算法
  10. RDLC报表上下标实现