# -*- 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值的更多相关文章

  1. [python] 使用scikit-learn工具计算文本TF-IDF值

    在文本聚类.文本分类或者比较两个文档相似程度过程中,可能会涉及到TF-IDF值的计算.这里主要讲述基于Python的机器学习模块和开源工具:scikit-learn.        希望文章对你有所帮 ...

  2. python 遍历字典中的键和值

    #遍历字典中的所有键和值 zd1={"姓名":"张三","年龄":20,"性别":"女"} zd2= ...

  3. 关于Java中计算日期差值不准确问题

    1.字符串日期相减 如:2016-4-1,必须先将此字符串转成Date对象,并且, 格式必须为:yyyy—MM—dd  HH:mm:ss. 如果不转就直接计算(2016-4-1)两个这样的日期,则误差 ...

  4. python 将数组中取某一值的元素全部替换为其他元素的方法

    这里的问题是在做House Price Prediction的时候遇到的,尝试对GarageArea做log转化,但是由于有些房子没有车库,所以GarageArea = 0,再通过log转化变成-in ...

  5. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  6. Python实现计算圆周率π的值到任意位的方法示例

    Python实现计算圆周率π的值到任意位的方法示例 本文实例讲述了Python实现计算圆周率π的值到任意位的方法.分享给大家供大家参考,具体如下: 一.需求分析 输入想要计算到小数点后的位数,计算圆周 ...

  7. 计算KS值的标准代码

    计算KS值的标准代码 from scipy.stats import ks_2samp get_ks = lambda y_pred,y_true: ks_2samp(y_pred[y_true==1 ...

  8. Python学习第六篇——字典中的键和值

    favorite_language ={ "jen":"python", "sarah":"c", "edwa ...

  9. 【381】python 获取列表中重复元素的索引值

    参考:获取python的list中含有重复值的index方法_python_脚本之家 核心思想:建立字典,遍历列表,把列表中每个元素和其索引添加到字典里面 cc = [1, 2, 3, 2, 4] f ...

随机推荐

  1. KNN——图像分类

    内容参考自:https://zhuanlan.zhihu.com/p/20894041?refer=intelligentunit 用像素点的rgb值来判断图片的分类准确率并不高,但是作为一个练习kn ...

  2. Java 数据库篇

    一.简易封装JDBC工具类: package com.jackie.MyBatis.main; import java.sql.Connection; import java.sql.DriverMa ...

  3. CART决策树

     CART(Classification and Regression tree)分类回归树由L.Breiman,J.Friedman,R.Olshen和C.Stone于1984年提出.ID3中根据属 ...

  4. _event_stop

    EventId 事件ID TeamId 事件玩家分组,攻守(防守为1,进攻为2),自定义阵营(_faction表自定义阵营ID),公会(公会guid) StopType 结束事件需要满足的条件,枚举类 ...

  5. 【BZOJ】1052: [HAOI2007]覆盖问题

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1052 大概自己YY了个贪心然后过了... 二分答案,考虑如何check: 找到一个最小的矩 ...

  6. winfrom 动态添加控件,以及删除

      private void btnadd_Click(object sender, EventArgs e)         {             int fileCount = 0;     ...

  7. Adobe Photoshop CC 2018 v19.0 简体中文正式版下载安装破解(附注册机+破解教程) 32/64位(安装破解注意事项是什么)

    Adobe Photoshop CC 2018 v19.0 简体中文正式版下载安装破解(附注册机+破解教程) 32/64位(安装破解注意事项是什么) 一.总结 一句话总结:下载安装破解教程文中都有,需 ...

  8. Eclipse中打包插件Fat Jar的安装与使用

    转自:https://www.cnblogs.com/wbyp/p/6222182.html     Eclipse可以安装一个叫Fat Jar的插件,用这个插件打包非常方便,Fat Jar的功能非常 ...

  9. Superclass和Constructor Chaining

    A subclass inherits accessible date fields and methods from its superclass. Does it inherit construc ...

  10. Python 编程快速上手 第十四章 处理 CSV 文件和 JSON 数据

    前言 这一章分为两个部分,处理 CSV 格式的数据和处理 JSON 格式个数据. 处理 CSV 理解 csv csv 的每一行代表了电子表格中的每一行,每个逗号分开两个单元格csv 的内容全部为文本, ...