import numpy as np
np.random.seed(10)
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import (RandomTreesEmbedding, RandomForestClassifier,
GradientBoostingClassifier)
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.pipeline import make_pipeline
n_estimator = 10
X, y = make_classification(n_samples=80000)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)
X_train, X_train_lr, y_train, y_train_lr = train_test_split(X_train,
y_train,
test_size=0.5)
rf = RandomForestClassifier(max_depth=3, n_estimators=n_estimator)
rf_enc = OneHotEncoder()
rf_lm = LogisticRegression()
rf.fit(X_train, y_train)
rf_enc.fit(rf.apply(X_train))
rf_lm.fit(rf_enc.transform(rf.apply(X_train_lr)), y_train_lr)
y_pred_rf_lm = rf_lm.predict_proba(rf_enc.transform(rf.apply(X_test)))[:, 1]
fpr_rf_lm, tpr_rf_lm, _ = roc_curve(y_test, y_pred_rf_lm)
grd = GradientBoostingClassifier(n_estimators=n_estimator)
grd_enc = OneHotEncoder()
grd_lm = LogisticRegression()
grd.fit(X_train, y_train) #GBDT建模
grd_enc.fit(grd.apply(X_train)[:, :, 0])
grd_lm.fit(grd_enc.transform(grd.apply(X_train_lr)[:, :, 0]), y_train_lr)
y_pred_grd_lm = grd_lm.predict_proba(
grd_enc.transform(grd.apply(X_test)[:, :, 0]))[:, 1]
fpr_grd_lm, tpr_grd_lm, _ = roc_curve(y_test, y_pred_grd_lm)
y_pred_grd = grd.predict_proba(X_test)[:, 1]
fpr_grd, tpr_grd, _ = roc_curve(y_test, y_pred_grd)
y_pred_rf = rf.predict_proba(X_test)[:, 1]
fpr_rf, tpr_rf, _ = roc_curve(y_test, y_pred_rf)
plt.figure(2)
plt.xlim(0, 0.2)
plt.ylim(0.8, 1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_rf, tpr_rf, label='RF')
plt.plot(fpr_rf_lm, tpr_rf_lm, label='RF + LR')
plt.plot(fpr_grd, tpr_grd, label='GBT')
plt.plot(fpr_grd_lm, tpr_grd_lm, label='GBT + LR')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve (zoomed in at top left)')
plt.legend(loc='best')
plt.show()
gbdt+lr代码
2018-08-09 15:07:49 · 1854阅读
算法
机器学习
评论


更多博客推荐
- 洛伦兹曲线(Lorenz curve)提升指数、提升表和提升图
-
洛伦兹曲线(Lorenz curve)提升指数、提升表和提升图
PythonEducation · 200阅读 · 2021-02-17 19:38:48
- PSi-Population Stability Index (PSI)模型分稳定性评估指标
-
PSi-Population Stability Index (PSI)模型分稳定性评估指标
PythonEducation · 163阅读 · 2021-02-17 19:36:42
- PSi-Population Stability Index (PSI)模型分稳定性评估指标
-
PSi-Population Stability Index (PSI)模型分稳定性评估指标
PythonEducation · 145阅读 · 2021-02-17 19:36:38
- 支持向量机SVM原理(参数解读和python脚本)
-
支持向量机SVM原理(参数解读和python脚本)
PythonEducation · 208阅读 · 2021-02-17 19:33:40
- 深入剖析 RSA 密钥原理及实践
-
本文从算法原理和组成结构出发,对RSA密钥进行了深入剖析,之后通过案例对RSA证书在不同场景下的应用做了介绍。
vivo互联网 · 157阅读 · 2021-01-19 10:03:41
- 数据挖掘竞赛-优胜解决方案实战
-
点击下载:数据挖掘竞赛-优胜解决方案实战请添加链接描述提取码:6v59课程以真实企业数据集与任务需求为背景,结合竞赛优胜解决方案,从实战角度出发,一步步讲解如何应用机器学习算法与数据挖掘技巧在实际问题中。课程全部章节内容皆为项目实战,每章带领大家从零开始完成一套解决方案分析与实际建模流程。选择当下最主流的python语言及其工具包当做核心工具,整体风格通俗易懂,旨在用最接地气的方式带领同学们挑战数
张晓动 · 376阅读 · 2021-01-12 14:51:36
- 细数最流行的人工智能、深度学习常用框架,不止有TensorFlow,Java也可以进行人工智能开发
-
人工智能是计算机科学的一个分支,它企图了解智能的实质,并生产出一种新的能以人类智能相似的方式做出反应的智能机器,该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等
弗拉德x · 567阅读 · 2020-12-15 08:59:44
- 机器学习 | 基于机器学习的供应链管理之销售库存优化分析(实操分享)
-
本次是用机器学习做出未来一定时期内的销售量预测,从而辅助指导销售库存计划的决策分析,以达到合理配置库存,减少资源成本浪费的目的。实操内容有点多,虽然我已经尽量删减了。有兴趣的朋友可以关注+收藏,后面慢慢看哟。如果觉得内容还行,请多多鼓励;如果有啥想法,评论留言or私信。那么我们开始说正事了~一、数据准备阶段数据集描述用于技术验证的数据集来自kaggle上的医药销售预测项目Rossmann Stor
Elyn88 · 270阅读 · 2020-12-10 16:34:09
Copyright©2005-2021 51CTO.COM 版权所有 未经许可 请勿转载