Skip to content

Instantly share code, notes, and snippets.

@gbushnell
Last active October 15, 2020 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbushnell/005a3107de8a56c4ab10d920b60184c6 to your computer and use it in GitHub Desktop.
Save gbushnell/005a3107de8a56c4ab10d920b60184c6 to your computer and use it in GitHub Desktop.
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