Skip to content

Instantly share code, notes, and snippets.

@bbennett36
Created May 11, 2020 14:31
Show Gist options
  • Save bbennett36/06b867b4fc73a92819bea5fe7aa0c884 to your computer and use it in GitHub Desktop.
Save bbennett36/06b867b4fc73a92819bea5fe7aa0c884 to your computer and use it in GitHub Desktop.
quantile plot
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
def get_bins(scores, q=10, adjust_endpoints=False):
_, bins = pd.qcut(scores, q=q, retbins=True)
if adjust_endpoints:
bins[0] = -0.1
bins[len(bins)-1] = 1.1
return bins
def get_quantiles_from_bins(scores, bins, one_high=True):
quantiles = pd.Series(np.digitize(scores, bins, right=True))
if one_high:
q = len(bins)-1
quantiles = q - quantiles + 1
return quantiles
def plot_quantiles(target, probs, q=10, figsize=(8,5),
title='Quantile Bar Graph'):
if q==10:
quant_type = 'Decile'
elif q==20:
quant_type = 'Ventile'
else:
quant_type = 'Quantile'
# Get quantiles from probabilities
bins = get_bins(probs, q=q, adjust_endpoints=True)
quantiles = get_quantiles_from_bins(probs, bins, one_high=True)
# Calculate event rate for each quantile
quantile_labels = sorted(set(quantiles))
rates = []
for q in quantile_labels:
arr = target[quantiles == q]
pos_vals = arr.sum()
rate = pos_vals / float(len(arr))
rates.append(rate)
# Plot event rate for each quantiles
fig, ax = plt.subplots(figsize=figsize)
ax.bar(quantile_labels, rates, align='center')
ax.set_xticks(quantile_labels)
plt.title(title)
plt.ylabel('Event Rate')
plt.xlabel(quant_type)
plt.close(fig)
return fig
# Keep in mind that this is set to create 10 deciles. If you want more than 10, change the parameter in the "get_bins" and "plot_quantiles" function
# Example of how to run this -
plot_quantiles(y_test, scores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment