<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Build a model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=30, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, presort=False, random_state=1960,\n",
       "            splitter='best')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "ds = datasets.load_breast_cancer();\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "clf = DecisionTreeClassifier(min_samples_leaf=30, random_state = 1960)\n",
    "NC = 12\n",
    "\n",
    "X = ds.data[:,0:NC]\n",
    "y = ds.target\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=1960)\n",
    "\n",
    "clf.fit(X_train , y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decision Tree Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: Tree Pages: 1 -->\n",
       "<svg width=\"614pt\" height=\"433pt\"\n",
       " viewBox=\"0.00 0.00 614.00 433.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 429)\">\n",
       "<title>Tree</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-429 610,-429 610,4 -4,4\"/>\n",
       "<!-- 0 -->\n",
       "<g id=\"node1\" class=\"node\"><title>0</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.431373\" stroke=\"black\" d=\"M414,-425C414,-425 244,-425 244,-425 238,-425 232,-419 232,-413 232,-413 232,-354 232,-354 232,-348 238,-342 244,-342 244,-342 414,-342 414,-342 420,-342 426,-348 426,-354 426,-354 426,-413 426,-413 426,-419 420,-425 414,-425\"/>\n",
       "<text text-anchor=\"start\" x=\"240\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mean concave points ≤ 0.051</text>\n",
       "<text text-anchor=\"start\" x=\"293.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.462</text>\n",
       "<text text-anchor=\"start\" x=\"284\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 284</text>\n",
       "<text text-anchor=\"start\" x=\"274.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [103, 181]</text>\n",
       "<text text-anchor=\"start\" x=\"285\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = benign</text>\n",
       "</g>\n",
       "<!-- 1 -->\n",
       "<g id=\"node2\" class=\"node\"><title>1</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.956863\" stroke=\"black\" d=\"M302.5,-306C302.5,-306 187.5,-306 187.5,-306 181.5,-306 175.5,-300 175.5,-294 175.5,-294 175.5,-235 175.5,-235 175.5,-229 181.5,-223 187.5,-223 187.5,-223 302.5,-223 302.5,-223 308.5,-223 314.5,-229 314.5,-235 314.5,-235 314.5,-294 314.5,-294 314.5,-300 308.5,-306 302.5,-306\"/>\n",
       "<text text-anchor=\"start\" x=\"183.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mean area ≤ 571.85</text>\n",
       "<text text-anchor=\"start\" x=\"209.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.076</text>\n",
       "<text text-anchor=\"start\" x=\"200\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 177</text>\n",
       "<text text-anchor=\"start\" x=\"198\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [7, 170]</text>\n",
       "<text text-anchor=\"start\" x=\"201\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = benign</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;1 -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>0&#45;&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M299.857,-341.907C293.471,-333.014 286.648,-323.509 280.058,-314.331\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"282.767,-312.103 274.092,-306.021 277.081,-316.185 282.767,-312.103\"/>\n",
       "<text text-anchor=\"middle\" x=\"270.042\" y=\"-326.991\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">True</text>\n",
       "</g>\n",
       "<!-- 6 -->\n",
       "<g id=\"node7\" class=\"node\"><title>6</title>\n",
       "<path fill=\"#e58139\" fill-opacity=\"0.886275\" stroke=\"black\" d=\"M483,-306C483,-306 345,-306 345,-306 339,-306 333,-300 333,-294 333,-294 333,-235 333,-235 333,-229 339,-223 345,-223 345,-223 483,-223 483,-223 489,-223 495,-229 495,-235 495,-235 495,-294 495,-294 495,-300 489,-306 483,-306\"/>\n",
       "<text text-anchor=\"start\" x=\"341\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">mean perimeter ≤ 98.32</text>\n",
       "<text text-anchor=\"start\" x=\"378.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.184</text>\n",
       "<text text-anchor=\"start\" x=\"369\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 107</text>\n",
       "<text text-anchor=\"start\" x=\"367\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [96, 11]</text>\n",
       "<text text-anchor=\"start\" x=\"361\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = malignant</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;6 -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>0&#45;&gt;6</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M358.49,-341.907C364.951,-333.014 371.857,-323.509 378.524,-314.331\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"381.516,-316.168 384.562,-306.021 375.853,-312.054 381.516,-316.168\"/>\n",
       "<text text-anchor=\"middle\" x=\"388.473\" y=\"-327.013\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">False</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node3\" class=\"node\"><title>2</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.992157\" stroke=\"black\" d=\"M172.5,-187C172.5,-187 61.5,-187 61.5,-187 55.5,-187 49.5,-181 49.5,-175 49.5,-175 49.5,-116 49.5,-116 49.5,-110 55.5,-104 61.5,-104 61.5,-104 172.5,-104 172.5,-104 178.5,-104 184.5,-110 184.5,-116 184.5,-116 184.5,-175 184.5,-175 184.5,-181 178.5,-187 172.5,-187\"/>\n",
       "<text text-anchor=\"start\" x=\"57.5\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">radius error ≤ 0.354</text>\n",
       "<text text-anchor=\"start\" x=\"81.5\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.014</text>\n",
       "<text text-anchor=\"start\" x=\"72\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 141</text>\n",
       "<text text-anchor=\"start\" x=\"70\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 140]</text>\n",
       "<text text-anchor=\"start\" x=\"73\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = benign</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;2 -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>1&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M200.591,-222.907C190.365,-213.56 179.4,-203.538 168.888,-193.929\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"171.073,-191.184 161.33,-187.021 166.35,-196.351 171.073,-191.184\"/>\n",
       "</g>\n",
       "<!-- 5 -->\n",
       "<g id=\"node6\" class=\"node\"><title>5</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.800000\" stroke=\"black\" d=\"M295,-179.5C295,-179.5 215,-179.5 215,-179.5 209,-179.5 203,-173.5 203,-167.5 203,-167.5 203,-123.5 203,-123.5 203,-117.5 209,-111.5 215,-111.5 215,-111.5 295,-111.5 295,-111.5 301,-111.5 307,-117.5 307,-123.5 307,-123.5 307,-167.5 307,-167.5 307,-173.5 301,-179.5 295,-179.5\"/>\n",
       "<text text-anchor=\"start\" x=\"219.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.278</text>\n",
       "<text text-anchor=\"start\" x=\"214\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 36</text>\n",
       "<text text-anchor=\"start\" x=\"212\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [6, 30]</text>\n",
       "<text text-anchor=\"start\" x=\"211\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = benign</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;5 -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>1&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M248.469,-222.907C249.384,-212.204 250.375,-200.615 251.301,-189.776\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"254.801,-189.929 252.165,-179.667 247.826,-189.333 254.801,-189.929\"/>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node4\" class=\"node\"><title>3</title>\n",
       "<path fill=\"#399de5\" stroke=\"black\" d=\"M98,-68C98,-68 12,-68 12,-68 6,-68 0,-62 0,-56 0,-56 0,-12 0,-12 0,-6 6,-0 12,-0 12,-0 98,-0 98,-0 104,-0 110,-6 110,-12 110,-12 110,-56 110,-56 110,-62 104,-68 98,-68\"/>\n",
       "<text text-anchor=\"start\" x=\"27\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\n",
       "<text text-anchor=\"start\" x=\"10\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 111</text>\n",
       "<text text-anchor=\"start\" x=\"8\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [0, 111]</text>\n",
       "<text text-anchor=\"start\" x=\"11\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = benign</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;3 -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>2&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M93.9135,-103.726C89.0074,-95.0615 83.8179,-85.8962 78.8828,-77.1802\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"81.8274,-75.277 73.8546,-68.2996 75.7361,-78.726 81.8274,-75.277\"/>\n",
       "</g>\n",
       "<!-- 4 -->\n",
       "<g id=\"node5\" class=\"node\"><title>4</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.964706\" stroke=\"black\" d=\"M220,-68C220,-68 140,-68 140,-68 134,-68 128,-62 128,-56 128,-56 128,-12 128,-12 128,-6 134,-0 140,-0 140,-0 220,-0 220,-0 226,-0 232,-6 232,-12 232,-12 232,-56 232,-56 232,-62 226,-68 220,-68\"/>\n",
       "<text text-anchor=\"start\" x=\"144.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.064</text>\n",
       "<text text-anchor=\"start\" x=\"139\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 30</text>\n",
       "<text text-anchor=\"start\" x=\"137\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [1, 29]</text>\n",
       "<text text-anchor=\"start\" x=\"136\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = benign</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;4 -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>2&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M140.459,-103.726C145.444,-95.0615 150.717,-85.8962 155.732,-77.1802\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"158.888,-78.7128 160.841,-68.2996 152.821,-75.2219 158.888,-78.7128\"/>\n",
       "</g>\n",
       "<!-- 7 -->\n",
       "<g id=\"node8\" class=\"node\"><title>7</title>\n",
       "<path fill=\"#e58139\" fill-opacity=\"0.419608\" stroke=\"black\" d=\"M454,-179.5C454,-179.5 356,-179.5 356,-179.5 350,-179.5 344,-173.5 344,-167.5 344,-167.5 344,-123.5 344,-123.5 344,-117.5 350,-111.5 356,-111.5 356,-111.5 454,-111.5 454,-111.5 460,-111.5 466,-117.5 466,-123.5 466,-123.5 466,-167.5 466,-167.5 466,-173.5 460,-179.5 454,-179.5\"/>\n",
       "<text text-anchor=\"start\" x=\"369.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.464</text>\n",
       "<text text-anchor=\"start\" x=\"364\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 30</text>\n",
       "<text text-anchor=\"start\" x=\"358\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [19, 11]</text>\n",
       "<text text-anchor=\"start\" x=\"352\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = malignant</text>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;7 -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>6&#45;&gt;7</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M410.877,-222.907C410.054,-212.204 409.163,-200.615 408.329,-189.776\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"411.808,-189.369 407.551,-179.667 404.829,-189.906 411.808,-189.369\"/>\n",
       "</g>\n",
       "<!-- 8 -->\n",
       "<g id=\"node9\" class=\"node\"><title>8</title>\n",
       "<path fill=\"#e58139\" stroke=\"black\" d=\"M594,-179.5C594,-179.5 496,-179.5 496,-179.5 490,-179.5 484,-173.5 484,-167.5 484,-167.5 484,-123.5 484,-123.5 484,-117.5 490,-111.5 496,-111.5 496,-111.5 594,-111.5 594,-111.5 600,-111.5 606,-117.5 606,-123.5 606,-123.5 606,-167.5 606,-167.5 606,-173.5 600,-179.5 594,-179.5\"/>\n",
       "<text text-anchor=\"start\" x=\"517\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">gini = 0.0</text>\n",
       "<text text-anchor=\"start\" x=\"504\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">samples = 77</text>\n",
       "<text text-anchor=\"start\" x=\"502\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">value = [77, 0]</text>\n",
       "<text text-anchor=\"start\" x=\"492\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">class = malignant</text>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;8 -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>6&#45;&gt;8</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M459.45,-222.907C472.67,-211.101 487.095,-198.217 500.27,-186.45\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"502.737,-188.939 507.864,-179.667 498.075,-183.718 502.737,-188.939\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x7f7a53ec66a0>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import graphviz \n",
    "from sklearn import tree\n",
    "\n",
    "dot_data = tree.export_graphviz(clf, out_file=None, \n",
    "                         feature_names=ds.feature_names[0:NC],  \n",
    "                         class_names=ds.target_names,  \n",
    "                         filled=True, rounded=True,  \n",
    "                         special_characters=True)  \n",
    "graph = graphviz.Source(dot_data)  \n",
    "graph\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# sklearn_explain approach"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "CONST_SCORE_DETECTIOM -11.5129154649 11.5129154649 False\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "NON_USED_FEATURES ['mean radius', 'mean texture', 'mean smoothness', 'mean compactness', 'mean concavity', 'mean symmetry', 'mean fractal dimension', 'texture error']\n",
      "USED_FEATURES ['mean perimeter', 'mean area', 'mean concave points', 'radius error']\n",
      "USING_PROBABILITY_AS_SCORE\n",
      "Index(['mean radius', 'mean texture', 'mean perimeter', 'mean area',\n",
      "       'mean smoothness', 'mean compactness', 'mean concavity',\n",
      "       'mean concave points', 'mean symmetry', 'mean fractal dimension',\n",
      "       'radius error', 'texture error', 'Score', 'BinnedScore',\n",
      "       'mean perimeter_bin', 'mean area_bin', 'mean concave points_bin',\n",
      "       'radius error_bin', 'mean perimeter_encoded', 'mean area_encoded',\n",
      "       'mean concave points_encoded', 'radius error_encoded',\n",
      "       'mean perimeter_Effect', 'mean area_Effect',\n",
      "       'mean concave points_Effect', 'radius error_Effect', 'reason_1',\n",
      "       'reason_2', 'reason_3', 'reason_4', 'reason_1_idx', 'detailed_reason_1',\n",
      "       'reason_2_idx', 'detailed_reason_2', 'reason_3_idx',\n",
      "       'detailed_reason_3', 'reason_4_idx', 'detailed_reason_4'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# Explain the score = ln(p(1) / (1 - p(1)))\n",
    "\n",
    "\n",
    "import sklearn_explain.explainer as expl\n",
    "lExplainer = expl.cModelScoreExplainer(clf)\n",
    "lExplainer.mSettings.mFeatureNames = ds.feature_names[0:NC]\n",
    "lExplainer.mSettings.mExplanationOrder = 1\n",
    "    \n",
    "lExplainer.fit(X_train)\n",
    "df_rc = lExplainer.explain(X_test)\n",
    "\n",
    "print(df_rc.columns)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "USING_PROBABILITY_AS_SCORE\n"
     ]
    }
   ],
   "source": [
    "df_rc_2 = lExplainer.explain(X_test[0].reshape(1, -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1.17600000e+01,   2.16000000e+01,   7.47200000e+01,\n",
       "          4.27900000e+02,   8.63700000e-02,   4.96600000e-02,\n",
       "          1.65700000e-02,   1.11500000e-02,   1.49500000e-01,\n",
       "          5.88800000e-02,   4.06200000e-01,   1.21000000e+00]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test[0].reshape(1, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean radius</th>\n",
       "      <th>mean texture</th>\n",
       "      <th>mean perimeter</th>\n",
       "      <th>mean area</th>\n",
       "      <th>mean smoothness</th>\n",
       "      <th>mean compactness</th>\n",
       "      <th>mean concavity</th>\n",
       "      <th>mean concave points</th>\n",
       "      <th>mean symmetry</th>\n",
       "      <th>mean fractal dimension</th>\n",
       "      <th>...</th>\n",
       "      <th>reason_3</th>\n",
       "      <th>reason_4</th>\n",
       "      <th>reason_1_idx</th>\n",
       "      <th>detailed_reason_1</th>\n",
       "      <th>reason_2_idx</th>\n",
       "      <th>detailed_reason_2</th>\n",
       "      <th>reason_3_idx</th>\n",
       "      <th>detailed_reason_3</th>\n",
       "      <th>reason_4_idx</th>\n",
       "      <th>detailed_reason_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>11.76</td>\n",
       "      <td>21.6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.08637</td>\n",
       "      <td>0.04966</td>\n",
       "      <td>0.01657</td>\n",
       "      <td>0</td>\n",
       "      <td>0.1495</td>\n",
       "      <td>0.05888</td>\n",
       "      <td>...</td>\n",
       "      <td>mean perimeter</td>\n",
       "      <td>radius error</td>\n",
       "      <td>1</td>\n",
       "      <td>[(387.1 &lt; 'mean area' &lt;= 477.3)]</td>\n",
       "      <td>2</td>\n",
       "      <td>[('mean concave points' &lt;= 0.017368)]</td>\n",
       "      <td>0</td>\n",
       "      <td>[(71.884 &lt; 'mean perimeter' &lt;= 80.624)]</td>\n",
       "      <td>3</td>\n",
       "      <td>[(0.36444 &lt; 'radius error' &lt;= 0.55352)]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   mean radius  mean texture mean perimeter mean area  mean smoothness  \\\n",
       "0        11.76          21.6              1         1          0.08637   \n",
       "\n",
       "   mean compactness  mean concavity mean concave points  mean symmetry  \\\n",
       "0           0.04966         0.01657                   0         0.1495   \n",
       "\n",
       "   mean fractal dimension                   ...                     \\\n",
       "0                 0.05888                   ...                      \n",
       "\n",
       "         reason_3      reason_4  reason_1_idx  \\\n",
       "0  mean perimeter  radius error             1   \n",
       "\n",
       "                  detailed_reason_1  reason_2_idx  \\\n",
       "0  [(387.1 < 'mean area' <= 477.3)]             2   \n",
       "\n",
       "                       detailed_reason_2  reason_3_idx  \\\n",
       "0  [('mean concave points' <= 0.017368)]             0   \n",
       "\n",
       "                         detailed_reason_3  reason_4_idx  \\\n",
       "0  [(71.884 < 'mean perimeter' <= 80.624)]             3   \n",
       "\n",
       "                         detailed_reason_4  \n",
       "0  [(0.36444 < 'radius error' <= 0.55352)]  \n",
       "\n",
       "[1 rows x 38 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_rc_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>detailed_reason_1</th>\n",
       "      <th>detailed_reason_2</th>\n",
       "      <th>detailed_reason_3</th>\n",
       "      <th>detailed_reason_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[(387.1 &lt; 'mean area' &lt;= 477.3)]</td>\n",
       "      <td>[('mean concave points' &lt;= 0.017368)]</td>\n",
       "      <td>[(71.884 &lt; 'mean perimeter' &lt;= 80.624)]</td>\n",
       "      <td>[(0.36444 &lt; 'radius error' &lt;= 0.55352)]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  detailed_reason_1                      detailed_reason_2  \\\n",
       "0  [(387.1 < 'mean area' <= 477.3)]  [('mean concave points' <= 0.017368)]   \n",
       "\n",
       "                         detailed_reason_3  \\\n",
       "0  [(71.884 < 'mean perimeter' <= 80.624)]   \n",
       "\n",
       "                         detailed_reason_4  \n",
       "0  [(0.36444 < 'radius error' <= 0.55352)]  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_rc_2[[col for col in df_rc_2.columns if col.startswith('detailed')]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                   0\n",
      "mean area_Effect            0.759265\n",
      "mean concave points_Effect  0.515169\n",
      "mean perimeter_Effect       0.013671\n",
      "radius error_Effect        -0.027322\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7f7a5c03b898>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "<KEY>",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f7a5c03ba58>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "transposed = df_rc_2[effects].transpose()\n",
    "sorted_rc = transposed.sort_values(by=transposed.columns[0] , ascending=False)\n",
    "print(sorted_rc.head())\n",
    "sorted_rc.plot.barh(figsize=(12,8))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
<commit_msg>Updated thsi notebook<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Build a model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=30, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, presort=False, random_state=1960,\n",
       "            splitter='best')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "ds = datasets.load_breast_cancer();\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "clf = DecisionTreeClassifier(min_samples_leaf=30, random_state = 1960)\n",
    "NC = 12\n",
    "\n",
    "X = ds.data[:,0:NC]\n",
    "y = ds.target\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=1960)\n",
    "\n",
    "clf.fit(X_train , y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decision Tree Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: Tree Pages: 1 -->\n",
       "<svg width=\"614pt\" height=\"433pt\"\n",
       " viewBox=\"0.00 0.00 614.00 433.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 429)\">\n",
       "<title>Tree</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-429 610,-429 610,4 -4,4\"/>\n",
       "<!-- 0 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>0</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.431373\" stroke=\"#000000\" d=\"M414,-425C414,-425 244,-425 244,-425 238,-425 232,-419 232,-413 232,-413 232,-354 232,-354 232,-348 238,-342 244,-342 244,-342 414,-342 414,-342 420,-342 426,-348 426,-354 426,-354 426,-413 426,-413 426,-419 420,-425 414,-425\"/>\n",
       "<text text-anchor=\"start\" x=\"240\" y=\"-409.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">mean concave points ≤ 0.051</text>\n",
       "<text text-anchor=\"start\" x=\"293.5\" y=\"-394.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.462</text>\n",
       "<text text-anchor=\"start\" x=\"284\" y=\"-379.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 284</text>\n",
       "<text text-anchor=\"start\" x=\"274.5\" y=\"-364.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [103, 181]</text>\n",
       "<text text-anchor=\"start\" x=\"285\" y=\"-349.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = benign</text>\n",
       "</g>\n",
       "<!-- 1 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>1</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.956863\" stroke=\"#000000\" d=\"M302.5,-306C302.5,-306 187.5,-306 187.5,-306 181.5,-306 175.5,-300 175.5,-294 175.5,-294 175.5,-235 175.5,-235 175.5,-229 181.5,-223 187.5,-223 187.5,-223 302.5,-223 302.5,-223 308.5,-223 314.5,-229 314.5,-235 314.5,-235 314.5,-294 314.5,-294 314.5,-300 308.5,-306 302.5,-306\"/>\n",
       "<text text-anchor=\"start\" x=\"183.5\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">mean area ≤ 571.85</text>\n",
       "<text text-anchor=\"start\" x=\"209.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.076</text>\n",
       "<text text-anchor=\"start\" x=\"200\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 177</text>\n",
       "<text text-anchor=\"start\" x=\"198\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [7, 170]</text>\n",
       "<text text-anchor=\"start\" x=\"201\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = benign</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;1 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>0&#45;&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M299.6209,-341.8796C293.3906,-333.0534 286.7519,-323.6485 280.327,-314.5466\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"283.1308,-312.4494 274.5045,-306.2981 277.412,-316.4862 283.1308,-312.4494\"/>\n",
       "<text text-anchor=\"middle\" x=\"270.2568\" y=\"-327.2346\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">True</text>\n",
       "</g>\n",
       "<!-- 6 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>6</title>\n",
       "<path fill=\"#e58139\" fill-opacity=\"0.886275\" stroke=\"#000000\" d=\"M483,-306C483,-306 345,-306 345,-306 339,-306 333,-300 333,-294 333,-294 333,-235 333,-235 333,-229 339,-223 345,-223 345,-223 483,-223 483,-223 489,-223 495,-229 495,-235 495,-235 495,-294 495,-294 495,-300 489,-306 483,-306\"/>\n",
       "<text text-anchor=\"start\" x=\"341\" y=\"-290.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">mean perimeter ≤ 98.32</text>\n",
       "<text text-anchor=\"start\" x=\"378.5\" y=\"-275.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.184</text>\n",
       "<text text-anchor=\"start\" x=\"369\" y=\"-260.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 107</text>\n",
       "<text text-anchor=\"start\" x=\"367\" y=\"-245.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [96, 11]</text>\n",
       "<text text-anchor=\"start\" x=\"361\" y=\"-230.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = malignant</text>\n",
       "</g>\n",
       "<!-- 0&#45;&gt;6 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>0&#45;&gt;6</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M358.7289,-341.8796C365.0333,-333.0534 371.751,-323.6485 378.2524,-314.5466\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"381.1799,-316.4698 384.1442,-306.2981 375.4837,-312.4011 381.1799,-316.4698\"/>\n",
       "<text text-anchor=\"middle\" x=\"388.2542\" y=\"-327.2579\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">False</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>2</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.992157\" stroke=\"#000000\" d=\"M172.5,-187C172.5,-187 61.5,-187 61.5,-187 55.5,-187 49.5,-181 49.5,-175 49.5,-175 49.5,-116 49.5,-116 49.5,-110 55.5,-104 61.5,-104 61.5,-104 172.5,-104 172.5,-104 178.5,-104 184.5,-110 184.5,-116 184.5,-116 184.5,-175 184.5,-175 184.5,-181 178.5,-187 172.5,-187\"/>\n",
       "<text text-anchor=\"start\" x=\"57.5\" y=\"-171.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">radius error ≤ 0.354</text>\n",
       "<text text-anchor=\"start\" x=\"81.5\" y=\"-156.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.014</text>\n",
       "<text text-anchor=\"start\" x=\"72\" y=\"-141.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 141</text>\n",
       "<text text-anchor=\"start\" x=\"70\" y=\"-126.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [1, 140]</text>\n",
       "<text text-anchor=\"start\" x=\"73\" y=\"-111.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = benign</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;2 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>1&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M200.2318,-222.8796C190.2537,-213.6031 179.5881,-203.6874 169.3358,-194.1559\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"171.6663,-191.5436 161.9593,-187.2981 166.9,-196.6704 171.6663,-191.5436\"/>\n",
       "</g>\n",
       "<!-- 5 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>5</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.800000\" stroke=\"#000000\" d=\"M295,-179.5C295,-179.5 215,-179.5 215,-179.5 209,-179.5 203,-173.5 203,-167.5 203,-167.5 203,-123.5 203,-123.5 203,-117.5 209,-111.5 215,-111.5 215,-111.5 295,-111.5 295,-111.5 301,-111.5 307,-117.5 307,-123.5 307,-123.5 307,-167.5 307,-167.5 307,-173.5 301,-179.5 295,-179.5\"/>\n",
       "<text text-anchor=\"start\" x=\"219.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.278</text>\n",
       "<text text-anchor=\"start\" x=\"214\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 36</text>\n",
       "<text text-anchor=\"start\" x=\"212\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [6, 30]</text>\n",
       "<text text-anchor=\"start\" x=\"211\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = benign</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;5 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>1&#45;&gt;5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M248.4975,-222.8796C249.3938,-212.2134 250.3612,-200.7021 251.2688,-189.9015\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"254.7666,-190.0729 252.1164,-179.8149 247.7912,-189.4867 254.7666,-190.0729\"/>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>3</title>\n",
       "<path fill=\"#399de5\" stroke=\"#000000\" d=\"M98,-68C98,-68 12,-68 12,-68 6,-68 0,-62 0,-56 0,-56 0,-12 0,-12 0,-6 6,0 12,0 12,0 98,0 98,0 104,0 110,-6 110,-12 110,-12 110,-56 110,-56 110,-62 104,-68 98,-68\"/>\n",
       "<text text-anchor=\"start\" x=\"27\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.0</text>\n",
       "<text text-anchor=\"start\" x=\"10\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 111</text>\n",
       "<text text-anchor=\"start\" x=\"8\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [0, 111]</text>\n",
       "<text text-anchor=\"start\" x=\"11\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = benign</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;3 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>2&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M93.9135,-103.9815C89.0585,-95.2504 83.926,-86.0202 79.0371,-77.2281\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"81.9712,-75.3025 74.0524,-68.2637 75.8533,-78.7043 81.9712,-75.3025\"/>\n",
       "</g>\n",
       "<!-- 4 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>4</title>\n",
       "<path fill=\"#399de5\" fill-opacity=\"0.964706\" stroke=\"#000000\" d=\"M220,-68C220,-68 140,-68 140,-68 134,-68 128,-62 128,-56 128,-56 128,-12 128,-12 128,-6 134,0 140,0 140,0 220,0 220,0 226,0 232,-6 232,-12 232,-12 232,-56 232,-56 232,-62 226,-68 220,-68\"/>\n",
       "<text text-anchor=\"start\" x=\"144.5\" y=\"-52.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.064</text>\n",
       "<text text-anchor=\"start\" x=\"139\" y=\"-37.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 30</text>\n",
       "<text text-anchor=\"start\" x=\"137\" y=\"-22.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [1, 29]</text>\n",
       "<text text-anchor=\"start\" x=\"136\" y=\"-7.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = benign</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;4 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>2&#45;&gt;4</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M140.4589,-103.9815C145.3922,-95.2504 150.6074,-86.0202 155.5752,-77.2281\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"158.7681,-78.6918 160.6403,-68.2637 152.6737,-75.2483 158.7681,-78.6918\"/>\n",
       "</g>\n",
       "<!-- 7 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>7</title>\n",
       "<path fill=\"#e58139\" fill-opacity=\"0.419608\" stroke=\"#000000\" d=\"M454,-179.5C454,-179.5 356,-179.5 356,-179.5 350,-179.5 344,-173.5 344,-167.5 344,-167.5 344,-123.5 344,-123.5 344,-117.5 350,-111.5 356,-111.5 356,-111.5 454,-111.5 454,-111.5 460,-111.5 466,-117.5 466,-123.5 466,-123.5 466,-167.5 466,-167.5 466,-173.5 460,-179.5 454,-179.5\"/>\n",
       "<text text-anchor=\"start\" x=\"369.5\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.464</text>\n",
       "<text text-anchor=\"start\" x=\"364\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 30</text>\n",
       "<text text-anchor=\"start\" x=\"358\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [19, 11]</text>\n",
       "<text text-anchor=\"start\" x=\"352\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = malignant</text>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;7 -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>6&#45;&gt;7</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M410.8522,-222.8796C410.0455,-212.2134 409.1749,-200.7021 408.3581,-189.9015\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"411.8395,-189.5225 407.5952,-179.8149 404.8595,-190.0504 411.8395,-189.5225\"/>\n",
       "</g>\n",
       "<!-- 8 -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>8</title>\n",
       "<path fill=\"#e58139\" stroke=\"#000000\" d=\"M594,-179.5C594,-179.5 496,-179.5 496,-179.5 490,-179.5 484,-173.5 484,-167.5 484,-167.5 484,-123.5 484,-123.5 484,-117.5 490,-111.5 496,-111.5 496,-111.5 594,-111.5 594,-111.5 600,-111.5 606,-117.5 606,-123.5 606,-123.5 606,-167.5 606,-167.5 606,-173.5 600,-179.5 594,-179.5\"/>\n",
       "<text text-anchor=\"start\" x=\"517\" y=\"-164.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">gini = 0.0</text>\n",
       "<text text-anchor=\"start\" x=\"504\" y=\"-149.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">samples = 77</text>\n",
       "<text text-anchor=\"start\" x=\"502\" y=\"-134.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">value = [77, 0]</text>\n",
       "<text text-anchor=\"start\" x=\"492\" y=\"-119.3\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\" fill=\"#000000\">class = malignant</text>\n",
       "</g>\n",
       "<!-- 6&#45;&gt;8 -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>6&#45;&gt;8</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M459.8174,-222.8796C472.7697,-211.1138 486.854,-198.3197 499.7714,-186.5855\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"502.1761,-189.1296 507.2247,-179.8149 497.4694,-183.9482 502.1761,-189.1296\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.files.Source at 0x7fee50e61908>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import graphviz \n",
    "from sklearn import tree\n",
    "\n",
    "dot_data = tree.export_graphviz(clf, out_file=None, \n",
    "                         feature_names=ds.feature_names[0:NC],  \n",
    "                         class_names=ds.target_names,  \n",
    "                         filled=True, rounded=True,  \n",
    "                         special_characters=True)  \n",
    "graph = graphviz.Source(dot_data)  \n",
    "graph\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# sklearn_explain approach"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "USING_LOG_ODDS_AS_SCORE\n",
      "SCORE_QUANTILES {0: -inf, 1: -11.512915464920228, 2: 1.6094379124341005, 3: 3.367295829986474, 4: 11.51291546492478}\n",
      "CONST_SCORE_DETECTIOM -11.512915464920228 11.51291546492478 False\n",
      "NON_USED_FEATURES ['mean radius', 'mean texture', 'mean smoothness', 'mean compactness', 'mean concavity', 'mean symmetry', 'mean fractal dimension', 'texture error']\n",
      "USED_FEATURES ['mean perimeter', 'mean area', 'mean concave points', 'radius error']\n",
      "FEATURE_QUANTILES mean perimeter {0: -inf, 1: 71.884, 2: 80.624, 3: 90.566, 4: 111.44}\n",
      "FEATURE_QUANTILES mean area {0: -inf, 1: 387.1, 2: 477.3, 3: 602.0, 4: 922.44}\n",
      "FEATURE_QUANTILES mean concave points {0: -inf, 1: 0.017368, 2: 0.027374, 3: 0.047373999999999965, 4: 0.08419800000000001}\n",
      "FEATURE_QUANTILES radius error {0: -inf, 1: 0.21352, 2: 0.26870000000000005, 3: 0.36444, 4: 0.55352}\n",
      "Index(['mean radius', 'mean texture', 'mean perimeter', 'mean area',\n",
      "       'mean smoothness', 'mean compactness', 'mean concavity',\n",
      "       'mean concave points', 'mean symmetry', 'mean fractal dimension',\n",
      "       'radius error', 'texture error', 'Score', 'BinnedScore',\n",
      "       'mean perimeter_bin', 'mean area_bin', 'mean concave points_bin',\n",
      "       'radius error_bin', 'mean perimeter_encoded', 'mean area_encoded',\n",
      "       'mean concave points_encoded', 'radius error_encoded',\n",
      "       'mean perimeter_Effect', 'mean area_Effect',\n",
      "       'mean concave points_Effect', 'radius error_Effect', 'reason_1',\n",
      "       'reason_2', 'reason_3', 'reason_4', 'reason_1_idx', 'detailed_reason_1',\n",
      "       'reason_2_idx', 'detailed_reason_2', 'reason_3_idx',\n",
      "       'detailed_reason_3', 'reason_4_idx', 'detailed_reason_4'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# Explain the score = ln(p(1) / (1 - p(1)))\n",
    "\n",
    "\n",
    "import sklearn_explain.explainer as expl\n",
    "lExplainer = expl.cModelScoreExplainer(clf)\n",
    "lExplainer.mSettings.mFeatureNames = ds.feature_names[0:NC]\n",
    "lExplainer.mSettings.mExplanationOrder = 1\n",
    "    \n",
    "lExplainer.fit(X_train)\n",
    "df_rc = lExplainer.explain(X_test)\n",
    "\n",
    "print(df_rc.columns)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_rc_2 = lExplainer.explain(X_test[0].reshape(1, -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.176e+01, 2.160e+01, 7.472e+01, 4.279e+02, 8.637e-02, 4.966e-02,\n",
       "        1.657e-02, 1.115e-02, 1.495e-01, 5.888e-02, 4.062e-01, 1.210e+00]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test[0].reshape(1, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean radius</th>\n",
       "      <th>mean texture</th>\n",
       "      <th>mean perimeter</th>\n",
       "      <th>mean area</th>\n",
       "      <th>mean smoothness</th>\n",
       "      <th>mean compactness</th>\n",
       "      <th>mean concavity</th>\n",
       "      <th>mean concave points</th>\n",
       "      <th>mean symmetry</th>\n",
       "      <th>mean fractal dimension</th>\n",
       "      <th>...</th>\n",
       "      <th>reason_3</th>\n",
       "      <th>reason_4</th>\n",
       "      <th>reason_1_idx</th>\n",
       "      <th>detailed_reason_1</th>\n",
       "      <th>reason_2_idx</th>\n",
       "      <th>detailed_reason_2</th>\n",
       "      <th>reason_3_idx</th>\n",
       "      <th>detailed_reason_3</th>\n",
       "      <th>reason_4_idx</th>\n",
       "      <th>detailed_reason_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>11.76</td>\n",
       "      <td>21.6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.08637</td>\n",
       "      <td>0.04966</td>\n",
       "      <td>0.01657</td>\n",
       "      <td>0</td>\n",
       "      <td>0.1495</td>\n",
       "      <td>0.05888</td>\n",
       "      <td>...</td>\n",
       "      <td>mean area</td>\n",
       "      <td>mean perimeter</td>\n",
       "      <td>2</td>\n",
       "      <td>[(-inf &lt; 'mean concave points' &lt;= 0.017368)]</td>\n",
       "      <td>3</td>\n",
       "      <td>[(0.36444 &lt; 'radius error' &lt;= 0.55352)]</td>\n",
       "      <td>1</td>\n",
       "      <td>[(387.1 &lt; 'mean area' &lt;= 477.3)]</td>\n",
       "      <td>0</td>\n",
       "      <td>[(71.884 &lt; 'mean perimeter' &lt;= 80.624)]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   mean radius  mean texture mean perimeter mean area  mean smoothness  \\\n",
       "0        11.76          21.6              1         1          0.08637   \n",
       "\n",
       "   mean compactness  mean concavity mean concave points  mean symmetry  \\\n",
       "0           0.04966         0.01657                   0         0.1495   \n",
       "\n",
       "   mean fractal dimension                   ...                      reason_3  \\\n",
       "0                 0.05888                   ...                     mean area   \n",
       "\n",
       "         reason_4  reason_1_idx                             detailed_reason_1  \\\n",
       "0  mean perimeter             2  [(-inf < 'mean concave points' <= 0.017368)]   \n",
       "\n",
       "   reason_2_idx                        detailed_reason_2  reason_3_idx  \\\n",
       "0             3  [(0.36444 < 'radius error' <= 0.55352)]             1   \n",
       "\n",
       "                  detailed_reason_3  reason_4_idx  \\\n",
       "0  [(387.1 < 'mean area' <= 477.3)]             0   \n",
       "\n",
       "                         detailed_reason_4  \n",
       "0  [(71.884 < 'mean perimeter' <= 80.624)]  \n",
       "\n",
       "[1 rows x 38 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_rc_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>detailed_reason_1</th>\n",
       "      <th>detailed_reason_2</th>\n",
       "      <th>detailed_reason_3</th>\n",
       "      <th>detailed_reason_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[(-inf &lt; 'mean concave points' &lt;= 0.017368)]</td>\n",
       "      <td>[(0.36444 &lt; 'radius error' &lt;= 0.55352)]</td>\n",
       "      <td>[(387.1 &lt; 'mean area' &lt;= 477.3)]</td>\n",
       "      <td>[(71.884 &lt; 'mean perimeter' &lt;= 80.624)]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              detailed_reason_1  \\\n",
       "0  [(-inf < 'mean concave points' <= 0.017368)]   \n",
       "\n",
       "                         detailed_reason_2                 detailed_reason_3  \\\n",
       "0  [(0.36444 < 'radius error' <= 0.55352)]  [(387.1 < 'mean area' <= 477.3)]   \n",
       "\n",
       "                         detailed_reason_4  \n",
       "0  [(71.884 < 'mean perimeter' <= 80.624)]  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_rc_2[[col for col in df_rc_2.columns if col.startswith('detailed')]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                       0\n",
      "mean concave points_Effect  6.380884e-04\n",
      "radius error_Effect         1.576499e-05\n",
      "mean perimeter_Effect      -1.110223e-15\n",
      "mean area_Effect           -1.110223e-15\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fee4694c2e8>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "<KEY>",
      "text/plain": [
       "<Figure size 864x576 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "effects = [col for col in df_rc_2.columns if col.endswith('_Effect')]\n",
    "transposed = df_rc_2[effects].transpose()\n",
    "sorted_rc = transposed.sort_values(by=transposed.columns[0] , ascending=False)\n",
    "print(sorted_rc.head())\n",
    "sorted_rc.plot.barh(figsize=(12,8))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.6+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

{'fraction_non_alphanumeric': 0.25829953702042135, 'fraction_numerical': 0.14070631303161898, 'mean_word_length': 2.139372429722269, 'pattern_counts': {'":': 249, '<': 727, '<?xml version=': 2, '>': 697, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4314178', 'n_tokens_mistral': 26519, 'n_tokens_neox': 21525, 'n_words': 3361}