首页 > 技术文章 > RFM 模型 , 用户生命周期

Teyisang 2020-12-04 15:33 原文

 

 

def rfm_func(x):
    level = x.map(lambda x:'1' if x>=0 else '0')
    label = level.R + level.F + level.M
    d = {
        '111':'重要价值客户',
        '011':'重要保持客户',
        '101':'重要挽留客户',
        '001':'重要发展客户',
        '110':'一般价值客户',
        '010':'一般保持客户',
        '100':'一般挽留客户',
        '000':'一般发展客户'
    }
    result = d[label]
    return result
rfm['label'] = rfm.apply(lambda x:x-x.mean()).apply(rfm_func,axis = 1)
rfm.head()

 

 

 

### 第五部分:用户的生命周期
- 将用户划分为活跃用户和其他用户
    - 统计每个用户每个月的消费次数
    - 统计每个用户每个月是否消费,消费记录为1否则记录为0
        - 知识点:DataFrame的apply和applymap的区别
            - applymap:返回df
            - 将函数做用于DataFrame中的所有元素(elements)
            - apply:返回Series
            - apply()将一个函数作用于DataFrame中的每个行或者列
    - 将用户按照每一个月份分成:
        - unreg:观望用户(前两月没买,第三个月才第一次买,则用户前两个月为观望用户)
        - unactive:首月购买后,后序月份没有购买则在没有购买的月份中该用户的为非活跃用户
        - new:当前月就进行首次购买的用户在当前月为新用户
        - active:连续月份购买的用户在这些月中为活跃用户
        - return:购买之后间隔n月再次购买的第一个月份为该月份的回头客


# 统计每个用户每个月的消费次数
user_month_f = df.pivot_table(index='user_id',columns='month',aggfunc={'user_id':'count'}).fillna(0)

 

# 统计每个用户每个月是否消费,消费记录为1否则记录为0
df_purchase = user_month_f.applymap(lambda x:1 if x>0 else 0)
df_purchase.shape  # (23570, 18)

 

#将df_purchase中的原始数据0和1修改为new,unactive......,返回新的df叫做df_purchase_new

#将df_purchase中的原始数据0和1修改为new,unactive......,返回新的df叫做df_purchase_new
#固定算法
def active_status(data):
    status = []#某个用户每一个月的活跃度
    for i in range(18):
        
        #若本月没有消费
        if data[i] == 0:
            if len(status) > 0:
                if status[i-1] == 'unreg':
                    status.append('unreg')
                else:
                    status.append('unactive')
            else:
                status.append('unreg')
                    
        #若本月消费
        else:
            if len(status) == 0:
                status.append('new')
            else:
                if status[i-1] == 'unactive':
                    status.append('return')
                elif status[i-1] == 'unreg':
                    status.append('new')
                else:
                    status.append('active')
    return status

pivoted_status = df_purchase.apply(active_status,axis = 1) 
pivoted_status.head()
out:
user_id
1 [new, unactive, unactive, unactive, unactive, ... 2 [new, unactive, unactive, unactive, unactive, ... 3 [new, unactive, return, active, unactive, unac... 4 [new, unactive, unactive, unactive, unactive, ... 5 [new, active, unactive, return, active, active... dtype: object

 

推荐阅读