知识代码
# 一元多次线性回归代码实现
# 【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
客户价值 | 历史贷款金额 | 贷款次数 | 学历 | 月收入 | 性别 |
1150 | 6488 | 2 | 2 | 9567 | 1 |
1157 | 5194 | 4 | 2 | 10767 | 0 |
1163 | 7066 | 3 | 2 | 9317 | 0 |
983 | 3550 | 3 | 2 | 10517 | 0 |
1205 | 7847 | 3 | 3 | 11267 | 1 |
1161 | 7214 | 3 | 2 | 9517 | 0 |
1522 | 6405 | 3 | 3 | 9767 | 1 |
1521 | 8208 | 4 | 2 | 11417 | 0 |
1211 | 5813 | 4 | 2 | 11167 | 1 |
1075 | 4752 | 3 | 3 | 9267 | 0 |
1354 | 6771 | 3 | 2 | 10767 | 0 |
1242 | 3652 | 2 | 2 | 9967 | 0 |
1029 | 4535 | 3 | 2 | 10167 | 0 |
1291 | 7528 | 3 | 3 | 11367 | 1 |
1792 | 8722 | 4 | 3 | 13567 | 1 |
1485 | 5326 | 4 | 2 | 9517 | 0 |
1496 | 8115 | 3 | 3 | 11567 | 1 |
864 | 5004 | 3 | 3 | 10567 | 0 |
1149 | 6652 | 2 | 2 | 9117 | 1 |
1689 | 8867 | 3 | 3 | 10217 | 1 |
1170 | 6292 | 3 | 2 | 9567 | 1 |
1175 | 7788 | 3 | 2 | 10617 | 0 |
942 | 4362 | 3 | 2 | 9067 | 0 |
1081 | 5534 | 3 | 2 | 9717 | 0 |
1591 | 8562 | 4 | 3 | 11667 | 0 |
1514 | 6092 | 4 | 3 | 12067 | 1 |
1383 | 9163 | 4 | 2 | 10617 | 0 |
1000 | 4272 | 3 | 2 | 9117 | 1 |
698 | 4170 | 2 | 2 | 8617 | 1 |
1910 | 9714 | 4 | 3 | 10817 | 1 |
1843 | 9537 | 4 | 3 | 11867 | 0 |
1144 | 5635 | 2 | 2 | 10267 | 1 |
1363 | 6722 | 3 | 3 | 11867 | 1 |
1416 | 5482 | 5 | 3 | 12017 | 1 |
1187 | 6578 | 2 | 2 | 10617 | 1 |
1208 | 6111 | 3 | 3 | 11017 | 0 |
1186 | 6000 | 2 | 2 | 10017 | 1 |
1500 | 9301 | 4 | 3 | 12717 | 1 |
1334 | 8317 | 3 | 2 | 9217 | 1 |
1097 | 4283 | 3 | 2 | 9317 | 1 |
1104 | 5576 | 2 | 2 | 8417 | 0 |
1357 | 5525 | 4 | 3 | 9817 | 0 |
1096 | 6253 | 2 | 2 | 10567 | 0 |
1558 | 4779 | 3 | 2 | 10217 | 1 |
1681 | 7752 | 3 | 3 | 10317 | 1 |
1037 | 5231 | 3 | 2 | 9667 | 1 |
1333 | 5485 | 2 | 3 | 10567 | 0 |
920 | 4474 | 3 | 2 | 10867 | 0 |
1183 | 6485 | 2 | 2 | 10517 | 0 |
代码
# 【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())