多元线型回归案例-客户价值分析

 知识代码

# 一元多次线性回归代码实现

# 【1】导入资源包
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# 【2】加载样本数据,核心是给x,y赋值
x = [[1], [2], [4], [5]]  #y = ax+b
y = [2,6,8,15]

#【2.1】多项式数据转变 y = ax^2+bx+c
from sklearn.preprocessing import PolynomialFeatures
pf = PolynomialFeatures(degree=2)
x2 = pf.fit_transform(x)
print(x2)


#【3】模型搭建
lr = LinearRegression()

#【4】模型训练
lr.fit(x2,y)

#【5】模型预测
res = lr.predict(pf.fit_transform([[10]]))
print(res)

#拓展
#【6】显示模型参数
print('回归系数:',str(lr.coef_))
print('截距:',str(lr.intercept_))
print('模型函数: y = ',str(lr.coef_[2]),'*x^2 +(',str(lr.coef_[1]),'*x) +',str(lr.intercept_))

#【7】样本及模型可视化
plt.scatter(x,y)
plt.plot(x,lr.predict(pf.fit_transform(x)))
# plt.plot(x,lr.predict(x2))
plt.show()

需求

客户价值分析。客户价值预测就是指客户未来一段时间能带来多少利润,其利润的来源可能 来自于信用卡的年费、取现手续费、分期手续费、境外交易手续费用等。而 分析出客户的价值后,在进行营销、电话接听、催收、产品咨询等各项服务 时,就可以针对高价值的客户进行区别于普通客户的服务,有助于进一步挖 掘这些高价值客户的价值,并提高这些高价值客户的忠诚度。

数据

客户价值数据表.xlsx

客户价值历史贷款金额贷款次数学历月收入性别
115064882295671
1157519442107670
116370663293170
983355032105170
1205784733112671
116172143295170
152264053397671
1521820842114170
1211581342111671
107547523392670
1354677132107670
124236522299670
1029453532101670
1291752833113671
1792872243135671
148553264295170
1496811533115671
864500433105670
114966522291171
1689886733102171
117062923295671
1175778832106170
94243623290670
108155343297170
1591856243116670
1514609243120671
1383916342106170
100042723291171
69841702286171
1910971443108171
1843953743118670
1144563522102671
1363672233118671
1416548253120171
1187657822106171
1208611133110170
1186600022100171
1500930143127171
133483173292171
109742833293171
110455762284170
135755254398170
1096625322105670
1558477932102171
1681775233103171
103752313296671
1333548523105670
920447432108670
1183648522105170

代码

# 【1】导入资源包
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import pandas as pd

# 【2】加载样本数据,核心是给x,y赋值

df = pd.read_excel('客户价值数据表.xlsx')
print(df.head())

# x = df.drop(columns='客户价值')
x = df[['历史贷款金额','贷款次数','学历','月收入']]
y = df['客户价值']


#【3】模型搭建
lr = LinearRegression()

#【4】模型训练
lr.fit(x,y)

#【5】模型预测
# res = lr.predict(pf.fit_transform([[10]]))
# print(res)

#拓展
#【6】显示模型参数
print('回归系数:',str(lr.coef_))
print('截距:',str(lr.intercept_))
print('模型函数: y = ',str(lr.coef_[2]),'*x^2 +(',str(lr.coef_[1]),'*x) +',str(lr.intercept_))

#【7】样本及模型可视化
# plt.scatter(x,y)
# plt.plot(x,lr.predict(pf.fit_transform(x)))
# # plt.plot(x,lr.predict(x2))
# plt.show()

#【8】模型评估
import statsmodels.api as sma
res = sma.OLS(y,sma.add_constant(x)).fit()
print(res.summary())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数智侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值