首页 > 其他分享 >建立KS评估模型

建立KS评估模型

时间:2022-10-18 17:55:33浏览次数:38  
标签:plt pred 模型 df3 KS print train test 评估

导入资源包

from sklearn.linear_model import LogisticRegression
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

加载数据

# x_train,训练数据集数据
# x_test,测试数据集数据
# y_train,训练数据集的标签
# y_test ,测试数据集的标签
df = pd.read_excel('股票客户流失.xlsx',engine='openpyxl')
print(df.head())


x = df.drop(columns='是否流失')
y = df['是否流失']

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)

建立模型

lr = LogisticRegression()

训练模型

lr.fit(x_train, y_train)

预测模型

y_pred = lr.predict(x_test)

打印对比预测结果和实际样本值


df1 = pd.DataFrame()
df1['实际值'] = list(y_test)
df1['预测值'] = list(y_pred)
print(df1.head(10))

精确度评估


score = accuracy_score(y_test, y_pred)
print('精确度:', str(score))
score1 = lr.score(x_test, y_test)
print('精确度:', str(score1))

打印预测结果概率



y_pred_proba = lr.predict_proba(x_test)
print(y_pred_proba[:5])
df2 = pd.DataFrame(y_pred_proba, columns=['不流失概率', '流失概率'])
df2 = df2.sort_values('流失概率', ascending=False)
print(df2)

【9】打印模型评估参数


from sklearn.metrics import classification_report

res = classification_report(y_test, y_pred)
print(res)

# 【10】绘制ROC曲线


from sklearn.metrics import roc_curve

fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:, 1])
df3 = pd.DataFrame()
df3['假报警率'] = fpr
df3['命中率'] = tpr
df3['阈值'] = thres
df3['ks'] = df3['命中率'] - df3['假报警率']
print(df3.head())

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['simhei']

plt.plot(thres[1:], tpr[1:])
plt.plot(thres[1:], fpr[1:])
plt.plot(thres[1:], tpr[1:] - fpr[1:])
plt.xlabel("阈值")
plt.legend(["命中率", '假报警率', 'ks曲线'])
plt.gca().invert_xaxis()
plt.show()

m1 = max(df3['ks'])

print(df3[df3['ks'] == m1])

标签:plt,pred,模型,df3,KS,print,train,test,评估
From: https://www.cnblogs.com/JK8395/p/16803494.html

相关文章

  • PXE高效网络装机与Kickstart无人值守安装
    一、PXE概述1、PXE(PrebooteXcutionEnvironment)的概念PXE(预启动执行环境,在操作系统之前运行)由Intel公司开发的网络引导技术,工作在Client/Server模式,允许客户机通过......
  • Dubbo——Dubbo中的URL统一资源模型与Dubbo协议
    一、URL简介在互联网领域,每个信息资源都有统一的且在网上唯一的地址,该地址就叫URL(UniformResourceLocator,统一资源定位符),它是互联网的统一资源定位标志,也就是指网络地址......
  • 算法测试-定义算法模型评测标准
    测试方案将产品方期望转化为约束条件约定通过测试的标准:一定数据的算法结果中能满足约束条件的最低比例准备测试数据,统计计算结果能达到各项约束条件的比例示例具......
  • java DFA算法模型
    importjava.util.HashMap;importjava.util.HashSet;importjava.util.Map;importjava.util.Set;/***敏感词过滤器*/publicclassSensitive{/**敏......
  • mmdetection测试模型指标(输出每个类别的误检率、漏检率、正确率)
    #!/usr/bin/envpython#-*-coding:utf-8-*-#file:model_test1.py#@author:jory.d#@contact:#@time:2022/01/0722:41#@desc:模型测试,查看误检和漏检......
  • 生成式模型 vs 判别式模型
    生成式模型和判别式模型的概念是机器学习领域非常重要的基础知识,但能准确区分开二者并不是一件非常容易的事情,笔者经常是看一遍忘一遍,为了巩固下知识点,我将从以下几个方面对......
  • SOLIDWORKS 2023新增功能 - PDM数据管理
    1.增强的文件跟踪•按单个用户跟踪文件下载。•使用新的文件获取日志界面轻松查看单个用户的获取操作。•查看有关文件下载人员、下载目标位置和下载时间的关键信息......
  • UESTC 491 Tricks in Bits
    ​​TricksinBits​​TimeLimit: 1000MS MemoryLimit: 65535KB 64bitIOFormat: %lld&%llu​​Submit​​​ ​​Status​​DescriptionGiven N unsigned......
  • ac 849 dijkstra
    时间复杂度为n^2#include<bits/stdc++.h>usingnamespacestd;constintN=510;intn,m;intdist[N];//每个点到起点的最短距离boolst[N];//判断某个点......
  • [答疑]低版本的EA能打开高版本的模型吗
    Morris2018-11-1019:22低版本的EA能打开高版本的模型吗?潘加宇:我一般都是用最新版本的EA。根据以往的经验,好像我用最新版本做出来的eap文件,没有学员反应说打不开,学员那边可......