Skip to content

Instantly share code, notes, and snippets.

@zero731
Created March 11, 2021 00:33
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 zero731/7b9b09b0942322043a083682411a8cdb to your computer and use it in GitHub Desktop.
Save zero731/7b9b09b0942322043a083682411a8cdb to your computer and use it in GitHub Desktop.
get churn probabilities and deciles for example churn analysis
## Get a Pandas Series that contains the predicted likelihood
## (probability) that each customer in the test set would churn
churn_probs = []
for obsv in rf.predict_proba(X_test_tf):
churn_probs.append(obsv[1])
churn_probs = pd.Series(data=churn_probs)
## Reset the index of y_test set
y_test_ri = y_test.reset_index(drop=True)
## Create a new DataFrame that includes our predictors (X) from the
## test set, our target (y) from the test set, and the probability
## of churn output by the model for each customer
prob_df = X_test.copy().reset_index(drop=True)
prob_df['Churn'] = y_test_ri
prob_df['churn_prob'] = churn_probs
## Split the test set into deciles based on predicted churn probability
prob_df['decile_rank'] = pd.qcut(prob_df['churn_prob'],
10, labels=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment