|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "Appraising Binary Classification Models\n", |
| 8 | + "======================================\n", |
| 9 | + "Interesting Theory Stuff : Accuracy, Precision, F-Measures, Recall, Specificity, Sensitivity\n" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "### Set up some data" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 20, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "import numpy as np\n", |
| 26 | + "\n", |
| 27 | + "y_pred = np.zeros(10000)\n", |
| 28 | + "y_test = np.zeros(10000)\n", |
| 29 | + "\n", |
| 30 | + "indices1 = np.random.randint(0,10000,300)\n", |
| 31 | + "indices2 = np.random.randint(0,10000,400)\n", |
| 32 | + "indices3 = np.random.randint(0,10000,500)\n", |
| 33 | + "y_pred[indices1] = 1\n", |
| 34 | + "y_test[indices2] = 1\n", |
| 35 | + "y_pred[indices3] = 1\n", |
| 36 | + "y_test[indices3] = 1\n" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 21, |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [ |
| 44 | + { |
| 45 | + "data": { |
| 46 | + "text/plain": [ |
| 47 | + "858.0" |
| 48 | + ] |
| 49 | + }, |
| 50 | + "execution_count": 21, |
| 51 | + "metadata": {}, |
| 52 | + "output_type": "execute_result" |
| 53 | + } |
| 54 | + ], |
| 55 | + "source": [ |
| 56 | + "np.sum(y_test)" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 22, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [ |
| 64 | + { |
| 65 | + "data": { |
| 66 | + "text/plain": [ |
| 67 | + "774.0" |
| 68 | + ] |
| 69 | + }, |
| 70 | + "execution_count": 22, |
| 71 | + "metadata": {}, |
| 72 | + "output_type": "execute_result" |
| 73 | + } |
| 74 | + ], |
| 75 | + "source": [ |
| 76 | + "np.sum(y_pred)" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": 23, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [ |
| 84 | + { |
| 85 | + "name": "stdout", |
| 86 | + "output_type": "stream", |
| 87 | + "text": [ |
| 88 | + "Accuracy score: 0.939\n" |
| 89 | + ] |
| 90 | + } |
| 91 | + ], |
| 92 | + "source": [ |
| 93 | + "from sklearn.metrics import accuracy_score\n", |
| 94 | + "print('Accuracy score: ', accuracy_score(y_test, y_pred)) \n" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 24, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [ |
| 102 | + { |
| 103 | + "data": { |
| 104 | + "text/plain": [ |
| 105 | + "array([[8879, 263],\n", |
| 106 | + " [ 347, 511]])" |
| 107 | + ] |
| 108 | + }, |
| 109 | + "execution_count": 24, |
| 110 | + "metadata": {}, |
| 111 | + "output_type": "execute_result" |
| 112 | + } |
| 113 | + ], |
| 114 | + "source": [ |
| 115 | + "from sklearn.metrics import confusion_matrix\n", |
| 116 | + "\n", |
| 117 | + "confusion_matrix(y_test,y_pred)" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": 11, |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [ |
| 125 | + { |
| 126 | + "name": "stdout", |
| 127 | + "output_type": "stream", |
| 128 | + "text": [ |
| 129 | + " precision recall f1-score support\n", |
| 130 | + "\n", |
| 131 | + " 0.0 0.96 0.97 0.97 9137\n", |
| 132 | + " 1.0 0.65 0.58 0.62 863\n", |
| 133 | + "\n", |
| 134 | + "avg / total 0.93 0.94 0.94 10000\n", |
| 135 | + "\n" |
| 136 | + ] |
| 137 | + } |
| 138 | + ], |
| 139 | + "source": [ |
| 140 | + "from sklearn.metrics import classification_report\n", |
| 141 | + "print(classification_report(y_test, y_pred))" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 12, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "from sklearn.metrics import precision_recall_curve\n", |
| 151 | + "\n", |
| 152 | + "precision, recall, thresholds = precision_recall_curve(y_test, y_pred)" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": 13, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [ |
| 160 | + { |
| 161 | + "data": { |
| 162 | + "text/plain": [ |
| 163 | + "array([ 0.0863 , 0.65116279, 1. ])" |
| 164 | + ] |
| 165 | + }, |
| 166 | + "execution_count": 13, |
| 167 | + "metadata": {}, |
| 168 | + "output_type": "execute_result" |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "precision" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": 14, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "array([ 1. , 0.58400927, 0. ])" |
| 184 | + ] |
| 185 | + }, |
| 186 | + "execution_count": 14, |
| 187 | + "metadata": {}, |
| 188 | + "output_type": "execute_result" |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "recall" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": 15, |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [ |
| 200 | + { |
| 201 | + "data": { |
| 202 | + "text/plain": [ |
| 203 | + "array([ 0., 1.])" |
| 204 | + ] |
| 205 | + }, |
| 206 | + "execution_count": 15, |
| 207 | + "metadata": {}, |
| 208 | + "output_type": "execute_result" |
| 209 | + } |
| 210 | + ], |
| 211 | + "source": [ |
| 212 | + "thresholds" |
| 213 | + ] |
| 214 | + }, |
| 215 | + { |
| 216 | + "cell_type": "code", |
| 217 | + "execution_count": null, |
| 218 | + "metadata": { |
| 219 | + "collapsed": true |
| 220 | + }, |
| 221 | + "outputs": [], |
| 222 | + "source": [] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "metadata": { |
| 228 | + "collapsed": true |
| 229 | + }, |
| 230 | + "outputs": [], |
| 231 | + "source": [] |
| 232 | + } |
| 233 | + ], |
| 234 | + "metadata": { |
| 235 | + "kernelspec": { |
| 236 | + "display_name": "Python 3.6", |
| 237 | + "language": "python", |
| 238 | + "name": "python36" |
| 239 | + }, |
| 240 | + "language_info": { |
| 241 | + "codemirror_mode": { |
| 242 | + "name": "ipython", |
| 243 | + "version": 3 |
| 244 | + }, |
| 245 | + "file_extension": ".py", |
| 246 | + "mimetype": "text/x-python", |
| 247 | + "name": "python", |
| 248 | + "nbconvert_exporter": "python", |
| 249 | + "pygments_lexer": "ipython3", |
| 250 | + "version": "3.6.2" |
| 251 | + } |
| 252 | + }, |
| 253 | + "nbformat": 4, |
| 254 | + "nbformat_minor": 2 |
| 255 | +} |
0 commit comments