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 ...
随机推荐
- 当图片加载失败时更换图片, Firefox onerror 报错
当图片加载失败时更换图片. <!DOCTYPE html> <meta charset="UTF-8"> <img src="http:// ...
- 良品铺子:“新零售”先锋的IT必经之路
良品铺子:“新零售”先锋的IT必经之路 云计算 大数据 CIO班 CIO 互联网+ 物联网 电子政务 2017-12-29 09:25:34 来源:互联网抢沙发 摘要:2017年被称为“新零售”元年 ...
- Java 数据库篇
一.简易封装JDBC工具类: package com.jackie.MyBatis.main; import java.sql.Connection; import java.sql.DriverMa ...
- Oracle Single-Row Functions(单行函数)——NULL-Related Functions
参考资料:http://docs.oracle.com/database/122/SQLRF/Functions.htm#SQLRF006 Single-row functions return a ...
- Codeforces 769D k-Интересные пары чисел
题目链接:http://codeforces.com/contest/769/problem/D 搜索题 考虑这些数的值域较小,直接${O(2^{k})}$次方枚举每个数字二进制位上是否改变,剪枝一下 ...
- JAVA中使用浮点数类型计算时,计算精度的问题
标题 在Java中实现浮点数的精确计算 AYellow(原作) 修改 关键字 Java 浮点数 精确计算 问题的提出:如果我们编译运行下面这个程序会看到什么?publi ...
- idea 启动时报 error:java 无效的源发行版
说白了就是编译的版本不符合,有的地方是jdk1.7 有的地方是jdk1.8 所以你只要每个地方都保持一致就行. 每个地方!! 每个地方!! 每个地方!! 重要的设置说三遍! 以jdk1.7为例 fil ...
- SQL中的float类型的数据
问题1. 如何在SQL中默认的使用float类型的数据 SQL中想要通过计算的方式最快的得到一个float类型的数据,只需要运算的其中一个值后面加上小数点就ok. 比如 :9/2=4 但是 :9/2 ...
- Git安装与使用
转载自:https://www.cnblogs.com/smuxiaolei/p/7484678.html git 提交 全部文件 git add . git add xx命令可以将xx文件添加到暂 ...
- Opencv各种编码器下视频文件大小对比
转载自http://blog.csdn.net/dcrmg/article/details/52215930 做视频样本切割,切片用ffv1编码,比原数据大了几十倍,看到了这篇文章,防止找不到记录一下 ...