客户价值通过三个维度分成了8个等级
高价值客户(高、高、高):最近消费时间较短、消费频度和消费金额都较高,这是需要重点关注和维护的客户。
重点保持客户(低、高、高):最近消费时间较长,消费频次和消费金额都较高,说明这是个一段时间没来的忠实客户,需要主动保持联系。
重点发展客户(高、低、高):最近消费时间较短、消费金额高,但消费频次较低。忠诚度不高,但是消费能力强,是非常有潜力的客户,需要重点跟进
模型实现
我模拟生成了一个Dataframe,里面包含三个维度,R,F,M,数据由np.random随机生成
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
data = {"R":np.random.randint(0,30,1000),"F":np.random.randint(0,20,1000),"M":np.random.randint(0,2000,1000)}
frame = pd.DataFrame(data)
frame
frame.describe()
clf = KMeans(n_clusters = 8,random_state = 50)
clf.fit(frame)
我们看看这8个簇中心点的坐标分别在哪里
8point = pd.DataFrame(clf.cluster_centers_,columns = ['R','F','M'])
8point
然后我们找寻RFM三个类别的中心点,根据大于小于这个中心点,我们把总的数据分成8个类型。
rmd = 8point['R'].median()
fmd = 8point['F'].median()
mmd = 8point['M'].median()
rmd,fmd,mmd
我们写一个函数,对表格进行加工贴上标签
def customer_type(frame):
customer_type = []
for i in range(len(frame)):
if frame.iloc[i,0]>=rmd and frame.iloc[i,1]>=fmd and frame.iloc[i,2]>=mmd:
customer_type.append('高价值客户')
elif frame.iloc[i,0]<rmd and frame.iloc[i,1]>=fmd and frame.iloc[i,2]>=mmd:
customer_type.append('重点保持客户')
elif frame.iloc[i,0]>=rmd and frame.iloc[i,1]<fmd and frame.iloc[i,2]>=mmd:
customer_type.append('重点发展客户')
elif frame.iloc[i,0]<rmd and frame.iloc[i,1]<fmd and frame.iloc[i,2]>=mmd:
customer_type.append('重点挽留客户')
elif frame.iloc[i,0]>=rmd and frame.iloc[i,1]>=fmd and frame.iloc[i,2]<mmd:
customer_type.append('一般价值客户')
elif frame.iloc[i,0]<rmd and frame.iloc[i,1]>=fmd and frame.iloc[i,2]<mmd:
customer_type.append('一般保持客户')
elif frame.iloc[i,0]>=rmd and frame.iloc[i,1]<fmd and frame.iloc[i,2]<mmd:
customer_type.append('一般发展客户')
else:
customer_type.append('潜在客户')
frame['客户分类'] = customer_type
我们对我们的数据使用这个函数
customer_type(frame)
frame
frame.groupby(by='客户分类').size()
这里用matplotlib做了一个粗略的图,感觉要做好看的图还是得导出数据用tableau
最后看看高价值用户有哪些:
frame[frame['客户分类']=='高价值客户']