题解 | #某店铺用户消费特征评分#
某店铺用户消费特征评分
https://www.nowcoder.com/practice/200c824e9ed4428491c27d65ec56067d
def rank(s,q):
top_quantile=s[q].quantile(0.75)
down_quantile=s[q].quantile(0.25)
med=s[q].median()
quartile=[]
if q=='recency':
for i in s[q]:
if i<=dowm_quantile:
quartile.append('4')
elif i>down_quantile and i<=med:
quartile.append('3')
elif i>med and i<=top_quantile:
quartile.append('2')
else:
quartile.append('1')
else:
for i in s[q]:
if i<=dowm_quantile:
quartile.append('1')
elif i>down_quantile and i<=med:
quartile.append('2')
elif i>med and i<=top_quantile:
quartile.append('3')
else:
quartile.append('4')
return quartile
sales['R_Quartile']=rank(sales,'recency')
sales['F_Quartile']=rank(sales,'frequency')
sales['M_Quartile']=rank(sales,'monetary')
top_quantile=s[q].quantile(0.75)
down_quantile=s[q].quantile(0.25)
med=s[q].median()
quartile=[]
if q=='recency':
for i in s[q]:
if i<=dowm_quantile:
quartile.append('4')
elif i>down_quantile and i<=med:
quartile.append('3')
elif i>med and i<=top_quantile:
quartile.append('2')
else:
quartile.append('1')
else:
for i in s[q]:
if i<=dowm_quantile:
quartile.append('1')
elif i>down_quantile and i<=med:
quartile.append('2')
elif i>med and i<=top_quantile:
quartile.append('3')
else:
quartile.append('4')
return quartile
sales['R_Quartile']=rank(sales,'recency')
sales['F_Quartile']=rank(sales,'frequency')
sales['M_Quartile']=rank(sales,'monetary')