Skip to content

Instantly share code, notes, and snippets.

@gbushnell

gbushnell/RFM_rank.py Secret

Last active Oct 15, 2020
Embed
What would you like to do?
RFM rank customers
#split metric X into quartiles
def get_ranks(x):
pct_75 = np.percentile(x, 75)
pct_50 = np.percentile(x, 50)
pct_25 = np.percentile(x, 25)
return {'max':x.max(), '75':pct_75, '50':pct_50, '25':pct_25, 'min':x.min()}
#rank Recency metric
def rank_r(x):
i = x.R_days_since_last_invoice
r = 0
if i <= R_pct['25']:
r = 1
if (i >= R_pct['25']) and (i < R_pct['50']):
r = 2
if (i >= R_pct['50']) and (i < R_pct['75']):
r = 3
if i >= R_pct['75']:
r = 4
return r
#.... F and M are ranked similarly, see GitHub for full code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment