Skip to content

Instantly share code, notes, and snippets.

@wolframalpha
Created May 28, 2020 17:43
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 wolframalpha/26687f4c0c95ec9d3295b610c1b1867e to your computer and use it in GitHub Desktop.
Save wolframalpha/26687f4c0c95ec9d3295b610c1b1867e to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
def calibration_error(y_true, y_prob, n_bins=5, strategy='uniform', return_expected_caliberation_error=True):
if strategy == 'quantile': # Determine bin edges by distribution of data
quantiles = np.linspace(0, 1, n_bins + 1)
bins = np.percentile(y_prob, quantiles * 100)
bins[-1] = bins[-1] + 1e-8
elif strategy == 'uniform':
bins = np.linspace(0., 1. + 1e-8, n_bins + 1)
else:
raise ValueError("Invalid entry to 'strategy' input. Strategy "
"must be either 'quantile' or 'uniform'.")
y_prob_max = np.max(y_prob, axis=-1)
binids = np.digitize(y_prob_max, bins) - 1
y_correct_classified = (np.argmax(y_true, axis=-1) == np.argmax(y_prob, axis=-1)).astype(int)
bin_sums = np.bincount(binids, weights=y_prob_max, minlength=len(bins))
bin_true = np.bincount(binids, weights=y_correct_classified, minlength=len(bins))
bin_total = np.bincount(binids, minlength=len(bins))
nonzero = bin_total != 0
# acc(Bm)
prob_true = bin_true[nonzero] / bin_total[nonzero]
#conf(Bm)
prob_pred = bin_sums[nonzero] / bin_total[nonzero]
expected_caliberation_error = np.sum(bin_total[nonzero] * np.abs(prob_true - prob_pred))/bin_total[nonzero].sum()
overconfidence_error = np.sum(bin_total[nonzero] * prob_pred * np.max(np.concatenate(((prob_pred - prob_true).reshape(-1, 1), np.zeros((1, len(prob_pred))).T), axis=1), axis=-1)/bin_total[nonzero].sum())
return prob_true, prob_pred, expected_caliberation_error, overconfidence_error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment