一、前言:电商用户画像分析:用户画像是通过分析用户的基础信息、特征偏好、社会属性等各维度的数据,刻画出用户的信息全貌,它是建立在一系列属性数据之上的目标用户模型。用户画像的本质是一个用以描述用户需求的工具。用户画像一般是产品设计、运营人员从用户群体中抽象出来的典型用户,从中可以挖掘用户价值,提供个性化推荐、精准营销等服务。 ![]() 二、理解数据与分析思路1.导入使用的包: #加上后可以直接在界面画图.
%matplotlib inline
#导入包
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 减少绘图产生的问题
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 指定字体
matplotlib.rcParams['font.family']='sans-serif'
matplotlib.rcParams['axes.unicode_minus'] = False #解决负号'-'显示为方块的问题
# 导入数据库包,从数据库里读取数据与存储数据
import pymysql
pymysql.install_as_MySQLdb()
from sqlalchemy import create_engine
import gc #gc模块可以回收内存
import warnings
warnings.filterwarnings('ignore')#减少报错
from datetime import datetime
2.导入数据:
3.理解数据: df.head()
![]() order_data共有五个字段,分别为,其中behavior_type中1为浏览,2为收藏,3为加购,4为购买
![]() 4.分析维度: ![]() 三、数据预处理:1.缺失值处理: ![]() ![]() 2.时间格式转换: ![]() 3.制作用户标签表: ![]() 四、分析过程:1、用户活跃的时间 time_brows = df[df['behavior_type']==1].groupby(['user_id','hour']).agg({'item_id':'count'}).reset_index()
time_brows.rename(columns={'item_id':'hour_counts'},inplace=True)
#求出每个用户最活跃的时间段
time_brows_max = time_brows.groupby('user_id').hour_counts.max().reset_index()
time_brows_max.rename(columns = {'hour_counts':'hour_counts_max'},inplace = True)
time_brows = pd.merge(time_brows,time_brows_max,how ='left',on = 'user_id')
#选取各用户浏览次数最多的时段,如有并列最多的时段,用逗号连接
time_brows_hour = time_brows.loc[time_brows['hour_counts']==time_brows['hour_counts_max'],'hour'].groupby(time_brows['user_id']).aggregate(lambda x:','.join(x)).reset_index()
#将用户浏览活跃时间段加入到用户标签表中
labels = pd.merge(labels,time_brows_hour,how = 'left',on = 'user_id')
labels.rename(columns = {'hour':'time_browse'},inplace = True)
#查看标签表
labels.head()
![]() 1.2 用户购买活跃时间段
![]() 1.3 清除缓存: del time_brows
del time_brows_max
del time_buy
del time_buy_max
del time_buy_hour
del time_brows_hour
gc.collect()
2、关于类目的用户行为
2.1 浏览最多的类目 #对用户和类目进行分组,统计浏览次数
df_browse_count = df_browse.groupby(['user_id','item_category']).item_id.count().reset_index()
df_browse_count.rename(columns = {'item_id':'item_category_counts'},inplace = True)
#按照用户进行分组,统计出每个用户浏览最多的类目
df_browse_count_max = df_browse_count.groupby('user_id').item_category_counts.max().reset_index()
df_browse_count_max.rename(columns = {'item_category_counts':'item_category_counts_max'},inplace = True)
df_browse_count = pd.merge(df_browse_count,df_browse_count_max,how = 'left',on = 'user_id')
#将类目转换成字符串格式,否则会报错
df_browse_count['item_category'] = df_browse_count['item_category'].astype(str)
#选取各用户浏览次数最多的类目,如有并列最多的类目,用逗号连接
df_browse_count= df_browse_count.loc[df_browse_count['item_category_counts']==df_browse_count['item_category_counts_max'],'item_category'].groupby(df_browse_count['user_id']).aggregate(lambda x:','.join(x)).reset_index()
#将用户浏览最多的类目加到标签表
labels = pd.merge(labels,df_browse_count,how = 'left',on = 'user_id')
labels.rename(columns = {'tiem_category':'cate_most_browse'},inplace = True)
labels.head()
![]() 2.2 收藏最多的类目
![]() 2.3 加购最多的类目 df_cart_count = df_cart.groupby(['user_id','item_category']).item_id.count().reset_index()
df_cart_count.rename(columns = {'item_id':'item_category_count'},inplace = True)
df_cart_count_max = df_cart_count.groupby('user_id').item_category_count.max().reset_index()
df_cart_count_max.rename(columns = {'item_category_count':'item_category_count_max'},inplace = True)
df_cart_count = pd.merge(df_cart_count,df_cart_count_max,how = 'left',on ='user_id')
df_cart_count['item_category'] = df_cart_count['item_category'].astype(str)
df_cart = df_cart_count.loc[df_cart_count['item_category_count']==df_cart_count['item_category_count_max'],'item_category'].groupby(df_cart_count['user_id']).aggregate(lambda x:','.join(x)).reset_index()
df_cart.rename(columns = {'item_category':'cate_most_cart'},inplace = True)
labels = pd.merge(labels,df_cart,how ='left',on = 'user_id')
labels.head()
![]() 2.4 购买最多的类目
![]() 3、30天用户行为 3.1 近30天购买次数 #将购买行为按用户进行分组,统计次数
df_count_30_buy = df[df['behavior_type'] == 4].groupby('user_id').item_id.count().reset_index()
labels = pd.merge(labels,df_count_30_buy,how = 'left',on = 'user_id')
labels.rename(columns = {'item_id':'count_30_buy'},inplace = True)
labels.head()
3.2 近30天加购次数
3.3 近30天活跃天数 #对用户进行分组,统计活跃的天数,包括浏览、收藏、加购、购买
counts_30_active = df.groupby('user_id')['date'].nunique()
labels = pd.merge(labels,counts_30_active,how='left',on='user_id')
labels.rename(columns={'date':'count_30_active'},inplace=True)
labels.head()
4、7天用户行为 ![]() 本维度分析以2014/12/18作为最后一天去分析 4.1 近7天购买次数
4.2 近7天加购次数 df_near_7_cart = df_near_7[df_near_7['behavior_type'] == 3].groupby('user_id').item_id.count().reset_index()
df_near_7_cart.rename(columns = {'item_id':'count_7_cart'},inplace = True)
labels = pd.merge(labels,df_near_7_cart,how = 'left',on = 'user_id')
labels.head()
4.3 近7天活跃次数
5.最后一次行为距今天数(今天取2014/12/18) #5.1 上次浏览距今天数
day_browes = df[df['behavior_type']== 1].groupby('user_id').date.max().apply(lambda x:(datetime.strptime('2014-12-19','%Y-%m-%d')-x).days)
labels = pd.merge(labels,day_browes,how = 'left',on = 'user_id')
labels.rename(columns = {'date':'day_browes'},inplace = True)
5.2 上次加购距今天数
5.3 上次购买距今天数 #5.3 上次购买距今天数
day_buy = df[df['behavior_type']== 4].groupby('user_id').date.max().apply(lambda x:(datetime.strptime('2014-12-19','%Y-%m-%d')-x).days)
labels = pd.merge(labels,day_buy,how = 'left',on = 'user_id')
labels.rename(columns = {'date':'day_buy'},inplace = True)
6.最近两次购买间隔天数
7.是否有商品浏览未下单 #取出浏览未购买的数据
df_browse_buy = df.loc[(df['behavior_type'] == 1 )| (df['behavior_type'] == 4),['user_id','item_id','behavior_type','time']]
#对上表进行数据透视表
browse_not_buy = pd.pivot_table(df_browse_buy,index = ['user_id','item_id'],columns = ['behavior_type'],values = ['time'],aggfunc = ['count'])
browse_not_buy.columns = ['browes','buy']
#nall值填充0
browse_not_buy.fillna(0,inplace = True)
#如果有浏览未购买的将browse_not_buy填充为1
browse_not_buy.loc[(browse_not_buy['browes'] > 0) & browse_not_buy['buy'] == 0,'browes_not_buy'] = 1
#统计用户有多少个商品浏览未购买
browse_not_buy = browse_not_buy.groupby('user_id').browes_not_buy.sum().reset_index()
labels = pd.merge(labels,browse_not_buy,how = 'left',on ='user_id')
#如果有浏览为购买的用户标签改为'是'
labels['browes_not_buy'] = labels['browes_not_buy'].apply(lambda x:'是' if x>0 else '否')
labels.rename(columns = {'browes_not_buy':'browse_not_buy'},inplace = True)
8.是否有商品加购未下单
9、用户属性标签 #提取购买用户的数据,并根据用户进行分组,统计购买次数
buy_again = df[df['behavior_type'] == 4].groupby('user_id').time.count().reset_index()
buy_again.rename(columns = {'time':'buy_again'},inplace = True)
labels = pd.merge(labels,buy_again,how = 'left',on = 'user_id')
#如果购买次数大于1则为复购用户
labels['buy_again'] = labels['buy_again'].apply(lambda x:'是' if x > 1 else '否' if x == 1 else '未购买')
labels.head()
9.2 访问活跃度
![]() 总体上看,访问天数多的访客比访问天数少的访客数量多,且以20次左右为拐点,因此定义访问天数小于20次的为低活跃,访问天数大于等于20次的定义为高活跃。 #定义活跃度大于20则为高活跃度用户
labels['buy_active_level'] = '高'
labels.loc[labels['count_30_active']<=19,'buy_active_level'] = '低'
9.3 购买的品类是否单一
9.4 用户价值分组 #获取标签统计值day_buy得到距今购买天数,并进行统计计数
last_buy_days = labels['day_buy'].value_counts().sort_index()
#对统计计数的结果进行画图
plt.figure(figsize=(16,9))
last_buy_days.plot(title='最后一次购买距今天数与人数的关系',fontsize=18)
plt.ylabel('购买人数',fontsize=14)
plt.xlabel('距今天数',fontsize=14)
![]() 可以看出大于8天为低
#定义一个函数用来转换标签值
def trans_value(x):
if x == '高高':
return '重要价值客户'
elif x == '低高':
return '重要深耕客户'
elif x == '高低':
return '重要唤回客户'
else:
return '即将流失客户'
#使用定义的函数转换标签纸
labels['rfm'] = labels['rfm_value'].apply(trans_value)
#删除buy_days_level
labels.drop(['buy_days_level','rfm_value'],axis=1,inplace=True)
#统计rfm各个值的情况
labels['rfm'].value_counts()
|
|