线性回归与logistic回归综合实战

简介: 线性回归与logistic回归综合实战

综合应用

在上二个章节,分别介绍了二中回归的用户分层情况与lift(提升度)的图形制作,本章重在综合展现,从二个维度对用户进行分层

1 logistic回归建模

Part1: logistic model
file_path_logistic<-"data_response_model.csv" #change the location
raw_logistic<-read.csv(file_path_logistic,stringsAsFactors = F)   #read in your csv data
train_logistic<-raw_logistic[raw_logistic$segment=='build',]  #select build sample
var_list_logistic<-c('m1_WEB_MNTHS_SINCE_LAST_SES',
                     'm1_POS_MNTHS_LAST_ORDER',
                     'm1_POS_NUM_ORDERS_24MO',
                     'm1_pos_mo_btwn_fst_lst_order',
                     'm1_EM_COUNT_VALID',
                     'm1_POS_TOT_REVPERSYS',
                     'm1_EM_MONTHS_LAST_OPEN',
                     'm1_POS_LAST_ORDER_DPA'
)   #put the final model variables
mods_logistic<-train_logistic[,c('dv_response',var_list_logistic)]  #select Y and varibales you want to try
model_glm<-glm(dv_response~.,data=mods_logistic,family =binomial(link ="logit"))   #logistic model
summary(model_glm)  #model summary

直接用整理好的变量与数据对数据进行logistic建模

2线性回归建模

Part2: Linear model
file_path_linear<-"data_revenue_model.csv" #change the location
raw_linear<-read.csv(file_path_linear,stringsAsFactors = F)   #read in your csv data
train_linear<-raw_linear[raw_linear$segment=='build',]  #select build sample
var_list_linear<-c('m2_POS_REVENUE_BASE',
                   'm2_POS_LAST_TOTAL_REVENUE',
                   'm2_POS_MNTHS_LAST_ORDER',
                   'm2_POS_REVENUE_BASE_SP_6MO',
                   'm2_POS_SP_QTY_24MO',
                   'm2_POS_TOT_REVPERSYS',
                   'm2_WEB_MNTHS_SINCE_LAST_SES',
                   'm2_SH_MNTHS_LAST_INQUIRED' 
)   #put the final model variables
mods_linear<-train_linear[,c('dv_revenue',var_list_linear)] #select Y and varibales you want to try

model_lm<-lm(dv_revenue~.,data=mods_linear)   #Linear model
summary(model_lm)  #model summary

3 综合应用

Part3:application of 2 models

二种模型预测

file_path<-"two_stage_data.csv"
# read in data
raw<-read.csv(file_path,stringsAsFactors = F)
pred_prob_resp<-predict(model_glm,raw,type='response') #using the logistic model to predict response score
pred_prob_reve<-predict(model_lm,raw,type='response')  #using the linear model to predict revenue
3.1 combo performance

将预测金额*预测概率得出用户评分

combo<-pred_prob_resp*pred_prob_reve #usage 1
head(combo)

# 3.1.1 separate to 10 gorups based on combo
decile_combo<-cut(combo,unique(quantile(combo,(0:10)/10)),labels=10:1, include.lowest = T) 
table(decile_combo)

按照得出来的评分进行10分位数分组,排序划分用户层次,进行分组聚合计算活跃用户比列类似sql里面的count(*) group by ,计算本组活跃用户率/总体用户活跃率得出分组后的效果lift图
performance for response based on decile_combo

library(plyr)
#put actual response,predicted response,decile together
combo_resp<-data.frame(actual=raw$dv_response,pred_prob_resp=pred_prob_resp,decile_combo_resp=decile_combo)
#group by decile_combo_resp
combo_decile_sum_resp<-ddply(combo_resp,.(decile_combo_resp),summarise,cnt=length(actual),resp=sum(actual))
combo_decile_sum_resp
combo_decile_sum_resp2<-within(combo_decile_sum_resp,
                               {rr<-resp/cnt
                               index<-100*rr/(sum(resp)/sum(cnt))
                               })  #add rr,index
combo_decile_sum_resp3<-combo_decile_sum_resp2[order(combo_decile_sum_resp2[,1],decreasing=T),] # order decile
View(combo_decile_sum_resp3)

在这里插入图片描述
从图中可以发现提升效果很明显

再从线性回归预测的用户消费金额进行分析,同样进行group by ,使用本组平均/全体平均*100得到提升度lift

performance for revenue based on decile_combo
put actual revenue,predicted revenue,decile together

combo_reve<-data.frame(actual=raw$dv_revenue,pred_prob_reve=pred_prob_reve,decile_combo_reve=decile_combo)
#group by decile_combo_reve
combo_decile_sum_reve<-ddply(combo_reve,.(decile_combo_reve),summarise,cnt=length(actual),rev=sum(actual))
combo_decile_sum_reve
combo_decile_sum_reve2<-within(combo_decile_sum_reve,
                               {rev_avg<-rev/cnt
                               index<-100*rev_avg/(sum(rev)/sum(cnt))
                               })  #add rev_avg,index
combo_decile_sum_reve3<-combo_decile_sum_reve2[order(combo_decile_sum_reve2[,1],decreasing=T),]  # order decile
View(combo_decile_sum_reve3)

4 消费金额与响应率交叉表制作

response part
separate into 10 groups based on predict_response

decile_resp<-cut(pred_prob_resp,unique(quantile(pred_prob_resp,(0:10)/10)),labels=10:1, include.lowest = T)
table(decile_resp)

revenue part
separate into 10 groups based on predict_revenue

decile_rev<-cut(pred_prob_reve,unique(quantile(pred_prob_reve,(0:10)/10)),labels=10:1, include.lowest = T)
table(decile_rev)

set together

decile_cross<-data.frame( #rid=raw$rid,
 dv_response=raw$dv_response,
 dv_revenue=raw$dv_revenue,
 pred_prob_resp=pred_prob_resp,
 pred_prob_reve=pred_prob_reve,
 decile_resp=decile_resp,
 decile_reve=decile_rev
)  

View(decile_cross)

在这里插入图片描述

decile_cross
put decile_resp,decile_reve together

cross_table_freq<-table(decile_resp=decile_cross$decile_resp,decile_reve=decile_cross$decile_reve)
View(cross_table_freq)
cross_table_pct<-prop.table(cross_table_freq)
View(cross_table_pct)

在这里插入图片描述
从结果发现清楚的将用户分成消费金额10个层次,用户活跃程度10个层次。10*10=100 总共100个层次的用户

library(sqldf)
a=sqldf("select decile_reve as rank,
sum(case decile_resp when 1 then Freq else 0 end) as '1',
sum(case decile_resp when 2 then Freq else 0 end) as '2',
sum(case decile_resp when 3 then Freq else 0 end) as '3',
sum(case decile_resp when 4 then Freq else 0 end) as '4',
sum(case decile_resp when 5 then Freq else 0 end) as '5',
sum(case decile_resp when 6 then Freq else 0 end) as '6',
sum(case decile_resp when 7 then Freq else 0 end) as '7',
sum(case decile_resp when 8 then Freq else 0 end) as '8',
sum(case decile_resp when 9 then Freq else 0 end) as '9',
sum(case decile_resp when 10 then Freq else 0 end) as '10'
from cross_table_freq
group by rank*1")

执行sql语句,得到的排序就是用户消费和响应等级客户群体数量的交叉表

在这里插入图片描述
将分好群体的客户与客户原始数据相结合,再使用可视化工具对数据进行分组呈现,客户的标签就呈现出来了。

目录
相关文章
|
机器学习/深度学习 人工智能 数据安全/隐私保护
免费交互式大模型在线图像去除水印.擦除.替换和增强照片项目代码(免费在线图像修复工具)
免费交互式大模型在线图像去除水印.擦除.替换和增强照片项目代码(免费在线图像修复工具)
|
9月前
|
存储 网络协议 安全
C语言「内存对齐潜规则」:结构体里看不见的填充字节
内存对齐是CPU硬件要求的数据地址约束规则:变量须存于其字节大小的整数倍地址。编译器自动插入填充字节确保对齐,导致结构体体积“膨胀”、硬件寄存器读写错位或协议异常。合理排序成员(从大到小)、慎用`packed`、明确对齐控制,是嵌入式与底层开发的关键避坑要点。(239字)
1158 7
|
8月前
|
人工智能 弹性计算 数据可视化
OpenClaw部署成本说明及一键部署指南
OpenClaw是开源智能办公助手,支持任务管理与多平台接入。本文详解其零软件费、低硬件成本(云服务器首月9.9元起)及模型调用策略(百炼新用户享7000万Token免费额度),并提供阿里云一键部署指南,全程可视化、免写代码,5分钟上线专属AI助理!
1220 15
|
11月前
|
人工智能
智创未来,智能体拥抱科技,重燃探索宇宙的热情
智能体正开启人类探索宇宙的新纪元。它们自主决策、处理海量数据、先行建造星际基地,成为深空探索的先遣军。从火星到遥远星系,智能体延伸了人类的触角,让星辰大海不再遥不可及。这是技术的飞跃,更是探索精神的传承。
|
存储 调度 KVM
深入浅出KVM虚拟化技术原理——Ansible安全基线配置(一)
本文深入解析KVM虚拟化核心机制,涵盖内核如何调度QEMU进程与KVM模块协同工作、CPU虚拟化扩展(VT-x/AMD-V)的硬件加速原理,以及存储池的管理与优势,助你全面掌握KVM底层运行逻辑。
873 11
|
新能源
大盘择时:慎用固定均线!12年回测A股数据揭示择时策略的3大适应性缺陷
动量策略加入50日与200日均线择时后收益下降,主要因均线滞后、逻辑冲突及市场变化。解决方案包括动态调整择时参数、结合多指标验证、优化动量执行细节,提升策略适应性与收益表现。
|
机器学习/深度学习 运维 数据挖掘
运维告警不是“玄学”:聊聊怎么用机器学习优化事件关联分析
运维告警不是“玄学”:聊聊怎么用机器学习优化事件关联分析
653 3
|
负载均衡 安全 应用服务中间件
《Nginx反向代理MinIO集群全实战:负载均衡配置、SELinux安全策略与生产环境调优指南》
SELinux(Security-Enhanced Linux) 是 Linux 内核的一个安全模块,用于提供强制访问控制(MAC, Mandatory Access Control),比传统的 Linux 文件权限(DAC, Discretionary Access Control)更严格。用执行命令时不受SELinux的限制,但Nginx Worker 进程:以。在一台新的虚拟机或现有虚拟机上安装 Nginx,这里使用。),确认请求被正确转发到 MinIO 集群。编辑 Nginx 配置文件。
1254 1
|
机器学习/深度学习 数据采集 人工智能
MATLAB在机器学习模型训练与性能优化中的应用探讨
本文介绍了如何使用MATLAB进行机器学习模型的训练与优化。MATLAB作为强大的科学计算工具,提供了丰富的函数库和工具箱,简化了数据预处理、模型选择、训练及评估的过程。文章详细讲解了从数据准备到模型优化的各个步骤,并通过代码实例展示了SVM等模型的应用。此外,还探讨了超参数调优、特征选择、模型集成等优化方法,以及深度学习与传统机器学习的结合。最后,介绍了模型部署和并行计算技巧,帮助用户高效构建和优化机器学习模型。
MATLAB在机器学习模型训练与性能优化中的应用探讨
|
SQL Oracle 关系型数据库
解读数据传输DTS技术架构及最佳实践
在阿里云数据库技术峰会上,阿里巴巴高级技术专家付大超(千震)针对于云计算时代最好的数据传输产品阿里云DTS的架构设计、基本原理以及相关的应用场景进行了精彩分享。帮助大家了解了阿里是如何实现异地多活和异构多活的,以及通过DTS轻松实现迁移、双同同步、容灾、订阅的真实案例。
15418 1