{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import LinearSVC\n",
    "from sklearn.calibration import calibration_curve as skcalibration_curve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calibration_curve(y_true, y_prob, normalize=False, n_bins=5):\n",
    "    if normalize:\n",
    "        y_prob = (y_prob - y_prob.min()) / (y_prob.max() - y_prob.min())\n",
    "    bins = np.linspace(0, 1 + 1e-8, n_bins + 1)\n",
    "    binids = np.digitize(y_prob, bins) - 1\n",
    "    bin_sums = np.bincount(binids, weights=y_prob, minlength=len(bins))\n",
    "    bin_true = np.bincount(binids, weights=y_true, minlength=len(bins))\n",
    "    bin_total = np.bincount(binids, minlength=len(bins))\n",
    "    nonzero = bin_total != 0\n",
    "    prob_true = (bin_true[nonzero] / bin_total[nonzero])\n",
    "    prob_pred = (bin_sums[nonzero] / bin_total[nonzero])\n",
    "    return prob_true, prob_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = load_iris(return_X_y=True)\n",
    "X, y = X[y != 2], y[y != 2]\n",
    "clf = LogisticRegression(max_iter=10000).fit(X, y)\n",
    "y_prob = clf.predict_proba(X)[:, 1]\n",
    "prob_true1, prob_pred1 = calibration_curve(y, y_prob)\n",
    "prob_true2, prob_pred2 = skcalibration_curve(y, y_prob)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = load_iris(return_X_y=True)\n",
    "X, y = X[y != 2], y[y != 2]\n",
    "clf = LinearSVC(max_iter=10000).fit(X, y)\n",
    "y_prob = clf.decision_function(X)\n",
    "prob_true1, prob_pred1 = calibration_curve(y, y_prob, normalize=True)\n",
    "prob_true2, prob_pred2 = skcalibration_curve(y, y_prob, normalize=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dev",
   "language": "python",
   "name": "dev"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}