目录
项目背景
RFM是一种经典的业务分析模型。通过对客户购买行为的3个维度R(最近购买),F(购买频次),M(购买)金额进行打分,进而对客户价值类型进行分类。经典的RFM模型将客户分为8类。不同类型的客户对企业的贡献是截然不同的,针对不同价值的客户企业需要制定不同的营销对策。
- 近度(Recency,最近一次消费到当前的时间间隔)
- 频度(Frequency,最近一段时间内的消费次数)
- 额度(Monetory,最近一段时间内的消费金额总额/均额)
实战案例
电子产品销售数据分析及RFM用户价值分析
内容是分析消费类电子产品(主要指不同品牌手机及配件)在某电商平台的销售数据。
全代码notebook文件及原始数据文件附于文末。
1 读取数据
1.1 字段解析
- Unnamed: 行号
- event_time:下单时间
- order_id:订单编号
- product_id:产品标号
- category_id :类别编号
- category_code :类别
- brand :品牌
- price :价格
- user_id :用户编号
- age :年龄
- sex :性别
- local:省份
1.2 数据基础信息查看
1.3 数据存储最小格式
看上一步最后,数据大小50+MB,读取还是需要5-10秒的,这里介绍一段缩减数据类存储空间的代码。
# reduce memory
def reduce_mem_usage(df, verbose=True):
start_mem = df.memory_usage().sum() / 1024**2
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
df[col] = df[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
df[col] = df[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
df[col] = df[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
df[col] = df[col].astype(np.int64)
else:
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
df[col] = df[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
df[col] = df[col].astype(np.float32)
else:
df[col] = df[col].astype(np.float64)
end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
return df
本案例执行后代码可缩减12.5%
2 数据清洗
清晰必做的3个工作,处理缺失值、重复值、异常值
2.1 查看缺失值比例
df.isnull().mean()
缺失率23%的字段:category_code,保留,固定值填充
# category_code本身为字符型,且缺失率在23% ,不宜直接删除,考虑用字符‘N’表示缺失进行填充
df['category_code'] = df['category_code'].fillna('N')
缺失率4%的字段:brand,删除缺失行
# 只保留非缺失行的数据
df = df[df.brand.notnull()]
2.2 查看重复值比例
df.duplicated().mean()