|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Building the ML Model " |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": { |
| 14 | + "collapsed": true |
| 15 | + }, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "#import packages \n", |
| 19 | + "import numpy as np\n", |
| 20 | + "import pandas as pd\n", |
| 21 | + "from sklearn.linear_model import LogisticRegression\n", |
| 22 | + " \n", |
| 23 | + "import pickle\n", |
| 24 | + "import warnings\n", |
| 25 | + "warnings.filterwarnings(\"ignore\")" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 3, |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [ |
| 33 | + { |
| 34 | + "name": "stdout", |
| 35 | + "output_type": "stream", |
| 36 | + "text": [ |
| 37 | + "1000\n", |
| 38 | + "[[ 7 27 1]\n", |
| 39 | + " [ 2 43 0]\n", |
| 40 | + " [ 7 26 1]\n", |
| 41 | + " [ 8 29 1]\n", |
| 42 | + " [ 3 42 0]]\n" |
| 43 | + ] |
| 44 | + } |
| 45 | + ], |
| 46 | + "source": [ |
| 47 | + "#https://raw.githubusercontent.com/sarwansingh/Python/master/ClassExamples/data/student-pass-fail-data.csv\n", |
| 48 | + "data = pd.read_csv(\"student-pass-fail-data.csv\")\n", |
| 49 | + "data = np.array(data)\n", |
| 50 | + "print(len(data))\n", |
| 51 | + "print(data[:5])" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 4, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [ |
| 59 | + { |
| 60 | + "name": "stdout", |
| 61 | + "output_type": "stream", |
| 62 | + "text": [ |
| 63 | + "Rows in X 1000 \n", |
| 64 | + "Rows in y 1000\n", |
| 65 | + "[[ 7 27]\n", |
| 66 | + " [ 2 43]\n", |
| 67 | + " [ 7 26]\n", |
| 68 | + " [ 8 29]\n", |
| 69 | + " [ 3 42]] [1 0 1 1 0]\n" |
| 70 | + ] |
| 71 | + } |
| 72 | + ], |
| 73 | + "source": [ |
| 74 | + "# extract the input matrix from data in X\n", |
| 75 | + "X = data[:, 0:-1]\n", |
| 76 | + "# extract the labels in y \n", |
| 77 | + "y = data[:, -1]\n", |
| 78 | + "#casting the entire data into int\n", |
| 79 | + "y = y.astype('int')\n", |
| 80 | + "X = X.astype('int')\n", |
| 81 | + "\n", |
| 82 | + "print(\"Rows in X \" , len(X) , \" \\nRows in y \", len(y))\n", |
| 83 | + "print(X[:5],y[:5])\n" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 5, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "data": { |
| 93 | + "text/plain": [ |
| 94 | + "LogisticRegression()" |
| 95 | + ] |
| 96 | + }, |
| 97 | + "execution_count": 5, |
| 98 | + "metadata": {}, |
| 99 | + "output_type": "execute_result" |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "#build the logisticRegression model\n", |
| 104 | + "log_reg = LogisticRegression()\n", |
| 105 | + "# fit the model with data\n", |
| 106 | + "log_reg.fit(X,y)" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": 6, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "name": "stdout", |
| 116 | + "output_type": "stream", |
| 117 | + "text": [ |
| 118 | + "[[7, 27]]\n", |
| 119 | + "[[ 0.12276493 0.87723507]]\n", |
| 120 | + "[1]\n", |
| 121 | + "[0]\n" |
| 122 | + ] |
| 123 | + } |
| 124 | + ], |
| 125 | + "source": [ |
| 126 | + "#testing the build model \n", |
| 127 | + "test=[[ 7,27]]\n", |
| 128 | + "print(test)\n", |
| 129 | + "\n", |
| 130 | + "print(log_reg.predict_proba(test))\n", |
| 131 | + "print(log_reg.predict(test)) # pass\n", |
| 132 | + "print(log_reg.predict([[2,43]])) #fail" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": 7, |
| 138 | + "metadata": {}, |
| 139 | + "outputs": [ |
| 140 | + { |
| 141 | + "name": "stdout", |
| 142 | + "output_type": "stream", |
| 143 | + "text": [ |
| 144 | + "[[ 9.99990993e-01 9.00739994e-06]]\n", |
| 145 | + "[1]\n" |
| 146 | + ] |
| 147 | + } |
| 148 | + ], |
| 149 | + "source": [ |
| 150 | + "# Saving model to disk\n", |
| 151 | + "#Serialization - saving the state (in memory) to file (on disk)\n", |
| 152 | + "pickle.dump(log_reg,open('model.pkl','wb'))\n", |
| 153 | + "\n", |
| 154 | + "# Loading model to compare the results\n", |
| 155 | + "#Deserialization - reteriving the file (on disk) to memory (RAM)\n", |
| 156 | + "model=pickle.load(open('model.pkl','rb'))\n", |
| 157 | + "print(model.predict_proba([[7,43]]))\n", |
| 158 | + "print(model.predict([[7,27]]))" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "metadata": { |
| 165 | + "collapsed": true |
| 166 | + }, |
| 167 | + "outputs": [], |
| 168 | + "source": [] |
| 169 | + }, |
| 170 | + { |
| 171 | + "cell_type": "markdown", |
| 172 | + "metadata": {}, |
| 173 | + "source": [ |
| 174 | + "## use the model and display in Flask" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": null, |
| 180 | + "metadata": { |
| 181 | + "collapsed": true |
| 182 | + }, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "#app.py\n", |
| 186 | + "from flask import Flask,request, url_for, redirect, render_template\n", |
| 187 | + "import pickle\n", |
| 188 | + "import numpy as np\n", |
| 189 | + "\n", |
| 190 | + "app = Flask(__name__)\n", |
| 191 | + "model=pickle.load(open('model.pkl','rb'))\n", |
| 192 | + "\n", |
| 193 | + "@app.route('/')\n", |
| 194 | + "def home():\n", |
| 195 | + " return render_template(\"index.html\")\n", |
| 196 | + "\n", |
| 197 | + "@app.route('/predict',methods=['POST','GET'])\n", |
| 198 | + "def predict():\n", |
| 199 | + " # receive the values send by user in three text boxes thru request object -> requesst.form.values()\n", |
| 200 | + " \n", |
| 201 | + " int_features = [int(x) for x in request.form.values()]\n", |
| 202 | + " final_features = [np.array(int_features)]\n", |
| 203 | + " #print(final_features)\n", |
| 204 | + " #prediction = model.predict(final_features)\n", |
| 205 | + " #output = round(prediction[0], 2)\n", |
| 206 | + " \n", |
| 207 | + " prediction=model.predict_proba(final_features)\n", |
| 208 | + " output='{0:.{1}f}'.format(prediction[0][1], 2)\n", |
| 209 | + " \n", |
| 210 | + " #print(output )\n", |
| 211 | + "\n", |
| 212 | + " return render_template('index.html', pred='Student passing probability is : {}'.format(output))\n", |
| 213 | + "\n", |
| 214 | + "if __name__ == '__main__':\n", |
| 215 | + " app.run(debug=False)\n" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": null, |
| 221 | + "metadata": { |
| 222 | + "collapsed": true |
| 223 | + }, |
| 224 | + "outputs": [], |
| 225 | + "source": [ |
| 226 | + " " |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "code", |
| 231 | + "execution_count": null, |
| 232 | + "metadata": { |
| 233 | + "collapsed": true |
| 234 | + }, |
| 235 | + "outputs": [], |
| 236 | + "source": [ |
| 237 | + " " |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "metadata": { |
| 244 | + "collapsed": true |
| 245 | + }, |
| 246 | + "outputs": [], |
| 247 | + "source": [] |
| 248 | + }, |
| 249 | + { |
| 250 | + "cell_type": "code", |
| 251 | + "execution_count": null, |
| 252 | + "metadata": { |
| 253 | + "collapsed": true |
| 254 | + }, |
| 255 | + "outputs": [], |
| 256 | + "source": [] |
| 257 | + } |
| 258 | + ], |
| 259 | + "metadata": { |
| 260 | + "kernelspec": { |
| 261 | + "display_name": "Python 3", |
| 262 | + "language": "python", |
| 263 | + "name": "python3" |
| 264 | + }, |
| 265 | + "language_info": { |
| 266 | + "codemirror_mode": { |
| 267 | + "name": "ipython", |
| 268 | + "version": 3 |
| 269 | + }, |
| 270 | + "file_extension": ".py", |
| 271 | + "mimetype": "text/x-python", |
| 272 | + "name": "python", |
| 273 | + "nbconvert_exporter": "python", |
| 274 | + "pygments_lexer": "ipython3", |
| 275 | + "version": "3.6.3" |
| 276 | + } |
| 277 | + }, |
| 278 | + "nbformat": 4, |
| 279 | + "nbformat_minor": 2 |
| 280 | +} |
0 commit comments