项目背景:
对于宝洁这一类的快消品企业,能够对商超门店的销售额做出精准的预测,尤其是能量化自身所控制的各种促销因素所能产生的效果,以对营销资源做出合理规划,是非常重要的。本项目中,我们根据电视广告,线上,线下,门店内,微信渠道等促销投入和销售额的结果进行回归分析,以实现对各类因素投入产出比做出评估。
分析需求说明:
对各类的营销因素,投入产出比进行评估分析。分析各类营销的效果与产出。
分析流程:
1.数据概况分析:数据行/列数量,缺失值的分布等。
2.单变量分析:数字型变量的指标描述,类别变量的分类占比情况。
3.多变量分析:按类别交叉对比,变量之间的相关性分析。
4.回归模型:模型建立,模型评估与优化。
分析过程:
#导入所需要的库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决中文显示问题-设置字体为黑体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
#读取数据
df = pd.read_csv(r'baojie.csv',index_col = 0)
df.info()
输出结果:

字段说明:
revenue :门店销售额
reach :微信推送次数
local_tv :本地电视广告投入
online :线上广告投入
instore :门店内海报陈列投入
person:门店销售人员投入
event促销事件:cobranding:品牌联合促销 holiday :节假日 ,special:门店特别促销,non-event无促销活动
观察数据,发现两个问题 local_tv存在缺失值,vent是object,无法放入模型中,需要对数据类型进行转化
df.isnull().sum()
输出结果:

df.describe()
输出结果:

观察数据分布描述,person中的max = 24有一些疑惑,一家店一天需要安排24个促销员吗?工作中可能需要跟业务人员核实确认。
#类别连梁event有多少个类别
df.event.unique()
输出结果:

与数据描述中的一直,一共有4类。之后需要对这4类变量进行分类。
#了解四类对应的收入情况
df.groupb(['event])['revenue'].describe()
输出结果:

df.groupby(['event'])['local_tv'].describe()
输出结果:

#处理event变量,让其变成哑变量
df.get_dummies(df)

df.info()
输出结果:

所有的object变量都已经消除了。
相关性分析:
df.corr()[['revenue']].sort_values('revenue',ascending=False)
输出结果:

我们粗略的可以看到 local_tv、person、instore、online这几个变量与销售额revenue具有比较强的正相关性、几个event数据虽然跟revenue有负相关性,但是相关性太弱,因此我们就不过分解读了。reach变量表明,微信的推送次数越多,顾客会越不愿意去购买产品。
#可视化分析
#线性关系可视化
#斜率与相关系数有关
sns.regplot('instore','revenue',df)
输出结果:

sns.regplot('person','revenue',df)
输出结果:

sns.regplot('local_tv','revenue',df)
输出结果:

数据中的local_tv还具有缺失值,对其进行填充
df.fillna(df.local_tv.mean())
df.info()
输出结果:

缺失值填充完成。object类型的变量已经消除,接下来需要进行建模
#线性回归建模、调包
from sklearn.linear_model import LinearRegression
model = LinearRegression()
y = df['revenue]
x = df[['local_tv','person','instore']]
model.fit(x,y)
#查看自变量系数与截距
print(model.coef_)
print(model.intercept_)
输出结果:


# 模型的评估,x为‘local_tv’,'person','instore'
score = model.score(x,y) #x和y的打分
predictions = model.predict(x) #计算y预测值
error = predictions-y #计算y的误差
rmse = (error**2).mean()**0.5 #计算rmse
mae = abs(error).mean()#计算mae
print(rmse)
print(mae)
输出结果:

现在我们对x进行调整、增加一个特征:online
x1 = df[['local_tv','person','instore','online']]
model.fit(x1,y)
print(model.coef_)
print(model.intercept_)
输出结果:


模型打分评估
#模型的评估,x为‘local_tv’,'person','instore','online'
score = model.score(x1,y) #x1和y的打分
predictions = model.predict(x1) #计算y预测值
error = predictions-y #计算y的误差
rmse = (error**2).mean()**0.5 #计算rmse
mae = abs(error).mean()#计算mae
print(rmse)
print(mae)
输出结果:

从两个X变量的模型打分结果来看 X1的rmse与mae值略微下降、说明四变量的误差较小,效果较好。
业务解读:
以三变量:local_tv 、person、instore三个变量为例来解读回归模型的结果。
宝洁销售额回归模型为:
销售额 = -5288 +1.75local_tv +2.050 * person +4.09instore
宝洁销售额预测结论为:
1.每提升1元的电视广告投入、可以得到1.75元的销售额回报,而没提升1元的店内海报投入则可以实现4.09元的销售回报
2.不断收集数据和添加新变量能提升对整体营销资源投入的把握。模型还可以不断的升级迭代
网友评论