问题定义

这是一个贷款的审批问题,假设你是一个银行的贷款审批员,现在有客户需要一定额度的贷款,他们填写了个人的信息(信息在datas.txt中给出),你需要根据他们的信息,建立一个分类模型,判断是否可以给他们贷款。

请根据所给的信息,建立分类模型,评价模型,同时将模型建立过程简单介绍一下,同时对各特征进行简单的解释说明。

Dataset

用户id,年龄,性别,申请金额,职业类型,教育程度,婚姻状态,房屋类型,户口类型,贷款用途,公司类型,薪水,贷款标记:0不放贷,1同意放贷

Data preprocessing

在对数据进行建模时,用户ID是没有用的。在描述用户信息的几个维度数据中,年龄,申请金额,薪水是连续值,剩下的是离散值。

通过观察发现有些数据存在数据缺失的情况,需要对这些数据进行处理,比如直接删除或者通过缺失值补全。

The Logit Function

The Logistic Regression

Model Data

 #逻辑回归模型
#对银行客户是否放贷进行分类 import pandas
import numpy
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, roc_auc_score data = pandas.read_csv("datas.csv")
data = data.dropna() # Randomly shuffle our data for the training and test set
admissions = data.loc[numpy.random.permutation(data.index)] # train with 700 and test with the following 300, split dataset
num_train = 14968
data_train = admissions[:num_train]
data_test = admissions[num_train:] # Fit Logistic regression to admit with features using the training set
logistic_model = LogisticRegression()
logistic_model.fit(data_train[['Age','Gender','AppAmount','Occupation',
'Education','Marital','Property','Residence',
'LoanUse','Company','Salary']], data_train['Label']) # Print the Models Coefficients
print(logistic_model.coef_) # .predict() using a threshold of 0.50 by default
predicted = logistic_model.predict(data_train[['Age','Gender','AppAmount','Occupation',
'Education','Marital','Property','Residence',
'LoanUse','Company','Salary']]) # The average of the binary array will give us the accuracy
accuracy_train = (predicted == data_train['Label']).mean() # Print the accuracy
print("Accuracy in Training Set = {s}".format(s=accuracy_train)) # Predicted to be admitted
predicted = logistic_model.predict(data_test[['Age','Gender','AppAmount','Occupation',
'Education','Marital','Property','Residence',
'LoanUse','Company','Salary']]) # What proportion of our predictions were true
accuracy_test = (predicted == data_test['Label']).mean()
print("Accuracy in Test Set = {s}".format(s=accuracy_test)) # Predict the chance of label from those in the training set
train_probs = logistic_model.predict_proba(data_train[['Age','Gender','AppAmount','Occupation',
'Education','Marital','Property','Residence',
'LoanUse','Company','Salary']])[:,1] test_probs = logistic_model.predict_proba(data_test[['Age','Gender','AppAmount','Occupation',
'Education','Marital','Property','Residence',
'LoanUse','Company','Salary']])[:,1] # Compute auc for training set
auc_train = roc_auc_score(data_train["Label"], train_probs) # Compute auc for test set
auc_test = roc_auc_score(data_test["Label"], test_probs) # Difference in auc values
auc_diff = auc_train - auc_test # Compute ROC Curves
roc_train = roc_curve(data_train["Label"], train_probs)
roc_test = roc_curve(data_test["Label"], test_probs) # Plot false positives by true positives
plt.plot(roc_train[0], roc_train[1])
plt.plot(roc_test[0], roc_test[1])

Logistic Regression:银行贷款申请审批实例的更多相关文章

  1. Logistic Regression vs Decision Trees vs SVM: Part II

    This is the 2nd part of the series. Read the first part here: Logistic Regression Vs Decision Trees ...

  2. 统计学习方法笔记 Logistic regression

    logistic distribution 设X是连续随机变量,X服从逻辑斯谛分布是指X具有下列分布函数和密度函数: 式中,μ为位置参数,γ>0为形状参数. 密度函数是脉冲函数 分布函数是一条S ...

  3. Machine Learning - 第3周(Logistic Regression、Regularization)

    Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...

  4. 【机器学习】Octave 实现逻辑回归 Logistic Regression

    ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...

  5. 机器学习之LinearRegression与Logistic Regression逻辑斯蒂回归(三)

    一 评价尺度 sklearn包含四种评价尺度 1 均方差(mean-squared-error) 2 平均绝对值误差(mean_absolute_error) 3 可释方差得分(explained_v ...

  6. Python机器学习算法 — 逻辑回归(Logistic Regression)

    逻辑回归--简介 逻辑回归(Logistic Regression)就是这样的一个过程:面对一个回归或者分类问题,建立代价函数,然后通过优化方法迭代求解出最优的模型参数,然后测试验证我们这个求解的模型 ...

  7. 通俗地说逻辑回归【Logistic regression】算法(二)sklearn逻辑回归实战

    前情提要: 通俗地说逻辑回归[Logistic regression]算法(一) 逻辑回归模型原理介绍 上一篇主要介绍了逻辑回归中,相对理论化的知识,这次主要是对上篇做一点点补充,以及介绍sklear ...

  8. 机器学习-非线性回归(Logistic Regression)及应用

    1. 概率 1.1 定义:概率(Probability):对一件事情发生的可能性的衡量. 1.2 范围:0 <= P <= 1 1.3 计算方法: 1.3.1 根据个人置信 1.3.2 根 ...

  9. 机器学习——逻辑回归(Logistic Regression)

    1 前言 虽然该机器学习算法名字里面有"回归",但是它其实是个分类算法.取名逻辑回归主要是因为是从线性回归转变而来的. logistic回归,又叫对数几率回归. 2 回归模型 2. ...

随机推荐

  1. 自定义报表开发(HTML/XML)

    定义报表执行的包或存储过程: --创建包头 CREATE OR REPLACE PACKAGE XXPLM_AARONTEST001 IS PROCEDURE MAIN(errbuf OUT VARC ...

  2. ZendStudio9之SVN项目代码提示丢失解决

    前几天转移服务器,SVN 也重建了个,但用着重建的项目发现代码提示丢失了...好郁闷..搞了半天终于找到解决的方法了! 如果你还保留有以前的 SVN 项目本地完整备份,可以直接拷贝以下三个文件到新项目 ...

  3. 拓展自定义编辑器窗口(EditorGUILayout类)

    Unity支持自行创建窗口,也支持自定义窗口布局.在Project视图中创建一个Editor文件夹,在文件夹中再创建一条脚本. 自定义窗口需要让脚本继承EditorWindow再设置MenuItem, ...

  4. robotium如何定位控件?

    search类获取当前所有的view,然后根据类型或者文本去筛选,找到view后获取坐标,然后点击坐标.本质都是通过坐标点击.solo.clickonScreen方法,底层调用MotionEvent类 ...

  5. 使用firbug调试程序写更高质量的代码设置方法

    在搜狐浏览器内输入about:config 在搜索栏中输入:strict 双击javascript.options.strict,将值变为true

  6. Javascript 数字保留2位小数

    整理使用Javascript函数将数值保留两位小数: 1.num.toFixed(2) //进位 2.(Math.round(num * 100) / 100).toFixed(2) //进位 3.( ...

  7. bison实例

    逆波兰记号计算器[文件名rpcalc.y]%{ #define YYSTYPE double #include <stdio.h> #include <math.h> #inc ...

  8. Oralce生成前N年的年数据

    今天做一个统计报表的时候正好碰到这个问题,原来,一般是通过后台代码来生成.现在直接通过oracle来生成,记录一下. 方法一: SELECT YEAR FROM ( , UNION SELECT TO ...

  9. Openlayer 3 的点击弹出框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. apue学习记录——配置apue.3e,实现P4‘ls例子

    #include"apue.h" #include<dirent.h> int main(int argc,char *argv[]) { DIR *dp; struc ...