python, 在信用评级中,计算KS statistic值
# -*- coding: utf-8 -*- import pandas as pd
from sklearn.grid_search import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.utils import shuffle
import numpy as np
from sklearn import metrics
from sklearn.metrics import log_loss, recall_score, precision_score, accuracy_score,f1_score
from sklearn.metrics import roc_curve, precision_recall_curve, roc_auc_score
# from sklearn.model_selection import cross_val_score
import lightgbm def ks_statistic(Y,Y_hat):
data = {"Y":Y,"Y_hat":Y_hat}
df = pd.DataFrame(data)
bins = np.array([-0.1,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
category = pd.cut(df["Y_hat"],bins=bins)
category = category.sort_values()
#max_index = len(np.unique(df["Y_hat"]))
Y = df.ix[category.index,:]['Y']
Y_hat = df.ix[category.index,:]['Y_hat']
df2 = pd.concat([Y,Y_hat],axis=1)
df3 = pd.pivot_table(df2,values = ['Y_hat'],index ='Y_hat',columns='Y',aggfunc=len,fill_value=0)
df4 = np.cumsum(df3)
df5 = df4/df4.iloc[:,1].max()
ks = max(abs(df5.iloc[:,0] - df5.iloc[:,1]))
return ks/len(bins) df = pd.read_csv('DC_ALL_20170217.csv', header=0)
X = df[df.columns.drop(['user_id','overdue'])].fillna(-999)
# X = df[['count','time_stamp','credit_limit','credit_card_use_rate','credit_count_x','bank_count','sex','occupation','education','marriage','hukou']]
y = df['overdue']
train = X.head(55596)
test = X.tail(69495-55596) train_label = y.head(55596).convert_objects(convert_numeric=True)
X_train, X_test, y_train, y_test = train_test_split(\
train.values, train_label, test_size=0.2, random_state=42) max_depth = 5
subsample=0.8
learning_rate=0.01
n_estimators=400
random_state=3
nthread=4
is_unbalance=True
objective ='binary'
LGBM = lightgbm.LGBMClassifier(max_depth=max_depth, learning_rate=learning_rate,
n_estimators=n_estimators, objective=objective,is_unbalance=is_unbalance, nthread=nthread,subsample=subsample)
LGBM.fit(X_train, y_train)
y_test_v = LGBM.predict(X_test)
y_test_p = LGBM.predict_proba(X_test)[:, 1] print 'auc: ', roc_auc_score(y_test, y_test_p)
print 'log_loss: ', log_loss(y_test, y_test_p)
print 'precision: ', precision_score(y_test, y_test_v)
print 'recall: ', recall_score(y_test, y_test_v)
print 'accuracy: ', accuracy_score(y_test, y_test_v)
print 'f1_score: ', f1_score(y_test, y_test_v)
print 'ks_statistic: ', ks_statistic(y_test.values, y_test_v)
python, 在信用评级中,计算KS statistic值的更多相关文章
- [python] 使用scikit-learn工具计算文本TF-IDF值
在文本聚类.文本分类或者比较两个文档相似程度过程中,可能会涉及到TF-IDF值的计算.这里主要讲述基于Python的机器学习模块和开源工具:scikit-learn. 希望文章对你有所帮 ...
- python 遍历字典中的键和值
#遍历字典中的所有键和值 zd1={"姓名":"张三","年龄":20,"性别":"女"} zd2= ...
- 关于Java中计算日期差值不准确问题
1.字符串日期相减 如:2016-4-1,必须先将此字符串转成Date对象,并且, 格式必须为:yyyy—MM—dd HH:mm:ss. 如果不转就直接计算(2016-4-1)两个这样的日期,则误差 ...
- python 将数组中取某一值的元素全部替换为其他元素的方法
这里的问题是在做House Price Prediction的时候遇到的,尝试对GarageArea做log转化,但是由于有些房子没有车库,所以GarageArea = 0,再通过log转化变成-in ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- Python实现计算圆周率π的值到任意位的方法示例
Python实现计算圆周率π的值到任意位的方法示例 本文实例讲述了Python实现计算圆周率π的值到任意位的方法.分享给大家供大家参考,具体如下: 一.需求分析 输入想要计算到小数点后的位数,计算圆周 ...
- 计算KS值的标准代码
计算KS值的标准代码 from scipy.stats import ks_2samp get_ks = lambda y_pred,y_true: ks_2samp(y_pred[y_true==1 ...
- Python学习第六篇——字典中的键和值
favorite_language ={ "jen":"python", "sarah":"c", "edwa ...
- 【381】python 获取列表中重复元素的索引值
参考:获取python的list中含有重复值的index方法_python_脚本之家 核心思想:建立字典,遍历列表,把列表中每个元素和其索引添加到字典里面 cc = [1, 2, 3, 2, 4] f ...
随机推荐
- Android四种布局方式
线性布局 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap ...
- CentOS7时间和日期的同步 (chrony和)
CentOS 6版本,使用 hwclock CentOS 7版本,使用timedatectl 1.基本概念 1.1 GMT,UTC,CST,DST时间 世界标准时间 整个地球分为二十四时区,每个时区都 ...
- Spring Boot 发布方法 - 原创
发布方式 构建Jar包,cmd命令行运行Spring Boot程序 第一步:在pom.xml中将packing节点值修改为jar,如下面加粗部分: <groupId>com.example ...
- 动态拼接SQL 语句
public T Get<T>(int id) { Type type = typeof(T); string columnStrings = string.Join(",&qu ...
- js全选 反选
// 全选 反选 allChoose: function (o) { var obj = $.extend(true, { id: "#id", name: "name& ...
- L1-033 出生年
不难,代码: #include <queue> #include <functional> #include <stdio.h> #include <stri ...
- XML.libXml2_ZC
1.字符串比较函数: xmlStrcmp(...) 这是大小写敏感的比较 xmlStrcasecmp(...) 这是大小写不敏感的比较(忽略字符串里面字符的大小写) 2.查找节点 2.1.循环 2.2 ...
- 虚拟机安装---vm12+ubuntukylin16.04
虚拟机安装及部分问题解决方案: https://blog.csdn.net/qq_35208390/article/details/78271743 安装vmware tools后,需设置共享文件夹才 ...
- 搞定'express' 不是内部或外部命令,也不是可运行的程序或批处理文件
1 -- 官方下nodejs,一路next完成, node -v 没问题 可以检测到版本号,node环境ok~ 2-- 本地 安装express :npm install express -g 检测 ...
- (转)Qt中图片相对位置的引用
原文作者:locky1218 原文地址:https://blog.csdn.net/locky1218/article/details/9749703/ 一般使用相对位置的时候可能无法显示图片,可 ...