{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import os\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(13)\n",
    "from sklearn.datasets import fetch_openml\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import plot_tree, DecisionTreeClassifier\n",
    "from sklearn import metrics\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from scipy.io.arff import loadarff\n",
    "\n",
    "# installable with: `pip install imodels`\n",
    "import imodels\n",
    "from imodels import SLIMRegressor, BayesianRuleListClassifier, RuleFitRegressor, GreedyRuleListClassifier\n",
    "from imodels import SLIMClassifier, OneRClassifier, BoostedRulesClassifier\n",
    "\n",
    "# change working directory to project root\n",
    "if os.getcwd().split('/')[-1] != 'imodels':\n",
    "    os.chdir('..')\n",
    "\n",
    "\n",
    "def get_diabetes_data():\n",
    "    '''load (classification) data on diabetes\n",
    "    '''\n",
    "    data = loadarff(\"imodels/tests/test_data/diabetes.arff\")\n",
    "    data_np = np.array(list(map(lambda x: np.array(list(x)), data[0])))\n",
    "    X = data_np[:, :-1].astype('float32')\n",
    "    y_text = data_np[:, -1].astype('str')\n",
    "    y = (y_text == 'tested_positive').astype(int)  # labels 0-1\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.75)  # split\n",
    "    feature_names = [\"#Pregnant\", \"Glucose concentration test\", \"Blood pressure(mmHg)\",\n",
    "                     \"Triceps skin fold thickness(mm)\",\n",
    "                     \"2-Hour serum insulin (mu U/ml)\", \"Body mass index\", \"Diabetes pedigree function\", \"Age (years)\"]\n",
    "    return X_train, X_test, y_train, y_test, feature_names\n",
    "\n",
    "\n",
    "X_train, X_test, y_train, y_test, feat_names = get_diabetes_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**fit a decent model**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/chandan/venvs/imodels_venv/lib/python3.7/site-packages/scikit_learn-0.24.2-py3.7-macosx-10.13-x86_64.egg/sklearn/linear_model/_logistic.py:765: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.7569444444444444"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = LogisticRegression()\n",
    "m.fit(X_train, y_train)\n",
    "m.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**look at its errors**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shapes (576, 8) (576, 1)\n",
      "DecisionTreeClassifier(max_depth=3)\n"
     ]
    }
   ],
   "source": [
    "model, feature_names = imodels.explain_classification_errors(X_test, m.predict(X_test), y_test,\n",
    "                                                             feature_names=feat_names,\n",
    "                                                             classifier=DecisionTreeClassifier(max_depth=3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 99480 characters]\n",
      "text/plain": [
       "<Figure size 1800x1200 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(dpi=300)\n",
    "plot_tree(model, feature_names=feature_names)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "formats": "ipynb,../imodels/tests/notebooks//py:percent"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
