基本簡介
根據美國數據庫營銷研究所Arthur Hughes的研究,客戶數據庫中有3個神奇的要素,這3個要素構成了數據分析最好的指標:
最近一次消費 (Recency)
消費頻率 (Frequency)
消費金額 (Monetary)
–來源百度百科
定義
Recency:最近一次消費,意指上一次購買距離現在的時間,一般按照天進行計算
Frequency:消費頻率是顧客在限定的期間內所購買的次數,時間範圍需要自己設定
Monetary:指的是一段時間(通常是1年)內的消費金額
實例
:
#定義相關函數
#三個指標的分值函數
def Recency(x):
if x>=0 and x<5:
return 5
elif x>=5 and x<15:
return 4
elif x>=15 and x<30:
return 3
elif x>=30 and x<90:
return 2
elif x>=90:
return 1
def Frequency(x):
if x>=0 and x<10:
return 1
elif x>=10 and x<30:
return 2
elif x>=30 and x<50:
return 3
elif x>=50 and x<100:
return 4
elif x>=100:
return 5
def Monetory(x):
if x>=0 and x<300:
return 1
elif x>=300 and x<1000:
return 2
elif x>=1000 and x<3000:
return 3
elif x>=3000 and x<10000:
return 4
elif x>=1000:
return 5
#判斷函數
def judge(x):
if x>avg:
return 1
else:
return 0
#定義RFM函數
def RFM(x):
if x.iloc[0]==1 and x.iloc[1]==1 and x.iloc[2]==1:
return "重要價值客戶"
elif x.iloc[0]==1 and x.iloc[1]==1 and x.iloc[2]==0:
return "重要潛力客戶"
elif x.iloc[0]==1 and x.iloc[1]==0 and x.iloc[2]==1:
return "重要發展客戶"
elif x.iloc[0]==1 and x.iloc[1]==0 and x.iloc[2]==0:
return "新客戶"
elif x.iloc[0]==0 and x.iloc[1]==1 and x.iloc[2]==1:
return "重要保持客戶"
elif x.iloc[0]==0 and x.iloc[1]==1 and x.iloc[2]==0:
return "一般客戶"
elif x.iloc[0]==0 and x.iloc[1]==0 and x.iloc[2]==1:
return "重要挽回客戶"
elif x.iloc[0]==0 and x.iloc[1]==0 and x.iloc[2]==0:
return "流失客戶"
#分值轉化
data["R-SCORE"] = data["R"].apply(Recency)
data["F-SCORE"] = data["F"].apply(Frequency)
data["M-SCORE"] = data["M"].apply(Monetory)
#查看分組情況,用來查看分組的分佈是否合理
grouped_r=data["R-SCORE"].groupby(data["R-SCORE"]).count()
grouped_f=data["F-SCORE"].groupby(data["F-SCORE"]).count()
grouped_m=data["M-SCORE"].groupby(data["M-SCORE"]).count()
#計算查看平均值
avg_r = data["R-SCORE"].mean()
avg_f = data["F-SCORE"].mean()
avg_m = data["M-SCORE"].mean()
display(avg_r,avg_f,avg_m)
#也可以用pandas_profiling進行數據預覽
import pandas_profiling
pandas_profiling.ProfileReport(data)
#0-1分類
for avg in [avg_r,avg_f,avg_m]:
if avg==avg_r:
data["R-SCORE是否大於均值"] = data["R-SCORE"].apply(judge)
elif avg==avg_f:
data["F-SCORE是否大於均值"] = data["F-SCORE"].apply(judge)
elif avg==avg_m:
data["M-SCORE是否大於均值"] = data["M-SCORE"].apply(judge)
#打標籤
data["RFM"] = data[["R-SCORE是否大於均值","F-SCORE是否大於均值","M-SCORE是否大於均值"]].apply(RFM,axis=1)
data.head()
#數據彙總
result = data.groupby("RFM").agg({"RFM":"count"})
#數據展示
from pyecharts import options as opts
from pyecharts.charts import TreeMap
data = [
{"value": int(result['RFM'][0]), "name": result['RFM'].index[0]},
{"value": int(result['RFM'][1]), "name": result['RFM'].index[1]},
{"value": int(result['RFM'][2]), "name": result['RFM'].index[2]},
{"value": int(result['RFM'][3]), "name": result['RFM'].index[3]},
{"value": int(result['RFM'][4]), "name": result['RFM'].index[4]},
{"value": int(result['RFM'][5]), "name": result['RFM'].index[5]},
{"value": int(result['RFM'][6]), "name": result['RFM'].index[6]},
{"value": int(result['RFM'][7]), "name": result['RFM'].index[7]},
]
c = (
TreeMap()
.add("RFM模型", data
,label_opts= opts.LabelOpts(font_size= 12,position = "inside"))
.set_global_opts(title_opts=opts.TitleOpts(title=""))
)
c.render_notebook()