业务基本背景:
一家家电相关企业的业务部门提出,希望能对用户做分组,还能对每个组的用户特征概括总结出来,便于后续精细化管理,且根据不同群体做定制化或者差异化的营销和关怀。
分析:
- 交付结果看,需要导出业务部门需要的分析结果为EXCEL文件,便于后续分析加工使用;RFM的结果还可能用于其他模型的建模使用,需要输出为本地文件并写入数据库。
- 了解到业务愿意投入较多得资源为客户服务,所以可以对客户管理进行细分。
思路:
基于用户分群需求,计划将RFM的3个维度分别做3个区间的离散化得到3x3x3个分类结果, 输出数据为为本地文件并写入mysql数据库便于后续分析应用。
import time
import numpy as np
import pandas as pd
import pymysql
from pyecharts.charts import Bar3D
from sklearn.ensemble import RandomForestClassifier
import warnings
import pyecharts.options as opts
warnings.filterwarnings('ignore')
path = '../../python_book_v2/chapter5/sales.xlsx'
sheet_name = ['2015', '2016', '2017', '2018', '会员等级']
sheet_data = [pd.read_excel(path, sheet_name=i) for i in sheet_name]
for each_name, each_data in zip(sheet_name, sheet_data[:-1]):
print('[data summary in {0:=^60}]'.format(each_name))
print('Over4 show:', '\n', each_data.head(4)) # 基本数据观察
print('Describe:', '\n', each_data.describe()) # 描述性统计
print('Na record:', each_data.isnull().any(axis=1).sum()) # 缺失值情况
print('Dtypes', '\n', each_data.dtypes) # 数据类型分布
# 数据预处理
for ind, value in enumerate(sheet_data[:-1