|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": { |
| 7 | + "collapsed": true |
| 8 | + }, |
| 9 | + "outputs": [], |
| 10 | + "source": [ |
| 11 | + "import numpy as np\n", |
| 12 | + "import pandas as pd\n", |
| 13 | + "from sklearn.linear_model import LogisticRegression\n", |
| 14 | + "from sklearn.model_selection import train_test_split\n", |
| 15 | + "\n", |
| 16 | + "import pickle\n", |
| 17 | + "import warnings\n", |
| 18 | + "warnings.filterwarnings(\"ignore\")" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "data = pd.read_csv(\"Forest_fire.csv\")\n", |
| 28 | + "data = np.array(data)\n", |
| 29 | + "print(data[:5])" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": null, |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "X = data[1:, 1:-1]\n", |
| 39 | + "y = data[1:, -1]\n", |
| 40 | + "y = y.astype('int')\n", |
| 41 | + "X = X.astype('int')\n", |
| 42 | + "print(len(X))\n", |
| 43 | + "print(X[:5],y[:5])\n" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)\n", |
| 53 | + "print(len(X_train) , len(X_test))\n", |
| 54 | + "print(len(y_train) , len(y_test))" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "log_reg = LogisticRegression()\n", |
| 64 | + "\n", |
| 65 | + "log_reg.fit(X_train, y_train)" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "inputt=[int(x) for x in \"45 32 60\".split(' ')]\n", |
| 75 | + "final=[np.array(inputt)]\n", |
| 76 | + "print(final)\n", |
| 77 | + "b = log_reg.predict_proba(final)\n", |
| 78 | + "print(b)\n", |
| 79 | + "\n", |
| 80 | + "pickle.dump(log_reg,open('model.pkl','wb'))\n", |
| 81 | + "model=pickle.load(open('model.pkl','rb'))" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "#inputt=[int(x) for x in \"45 32 60\".split(' ')]\n", |
| 91 | + "#final=[np.array(inputt)]\n", |
| 92 | + "#print(final)\n", |
| 93 | + "b = log_reg.predict([[45,32,60]])\n", |
| 94 | + "print(b)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "## use the model and display in Flask" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": null, |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [ |
| 109 | + { |
| 110 | + "name": "stderr", |
| 111 | + "output_type": "stream", |
| 112 | + "text": [ |
| 113 | + " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n", |
| 114 | + "127.0.0.1 - - [27/Aug/2020 16:20:42] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n", |
| 115 | + "127.0.0.1 - - [27/Aug/2020 16:20:42] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n", |
| 116 | + "127.0.0.1 - - [27/Aug/2020 16:20:42] \"\u001b[33mGET /.js/materialize.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 117 | + "127.0.0.1 - - [27/Aug/2020 16:20:42] \"\u001b[33mGET /js/init.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 118 | + "127.0.0.1 - - [27/Aug/2020 16:20:42] \"\u001b[33mGET /.js/materialize.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 119 | + "127.0.0.1 - - [27/Aug/2020 16:20:42] \"\u001b[33mGET /js/init.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 120 | + "127.0.0.1 - - [27/Aug/2020 16:20:50] \"\u001b[37mPOST /predict HTTP/1.1\u001b[0m\" 200 -\n", |
| 121 | + "127.0.0.1 - - [27/Aug/2020 16:20:50] \"\u001b[33mGET /js/init.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 122 | + "127.0.0.1 - - [27/Aug/2020 16:20:50] \"\u001b[33mGET /.js/materialize.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 123 | + "127.0.0.1 - - [27/Aug/2020 16:20:50] \"\u001b[33mGET /.js/materialize.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 124 | + "127.0.0.1 - - [27/Aug/2020 16:20:50] \"\u001b[33mGET /js/init.js HTTP/1.1\u001b[0m\" 404 -\n" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "name": "stdout", |
| 129 | + "output_type": "stream", |
| 130 | + "text": [ |
| 131 | + "[40, 40, 60]\n", |
| 132 | + "[array([40, 40, 60])]\n" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "name": "stderr", |
| 137 | + "output_type": "stream", |
| 138 | + "text": [ |
| 139 | + "[2020-08-27 16:21:02,633] ERROR in app: Exception on /predict [POST]\n", |
| 140 | + "Traceback (most recent call last):\n", |
| 141 | + " File \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\flask\\app.py\", line 1982, in wsgi_app\n", |
| 142 | + " response = self.full_dispatch_request()\n", |
| 143 | + " File \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\flask\\app.py\", line 1614, in full_dispatch_request\n", |
| 144 | + " rv = self.handle_user_exception(e)\n", |
| 145 | + " File \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\flask\\app.py\", line 1517, in handle_user_exception\n", |
| 146 | + " reraise(exc_type, exc_value, tb)\n", |
| 147 | + " File \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\flask\\_compat.py\", line 33, in reraise\n", |
| 148 | + " raise value\n", |
| 149 | + " File \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\flask\\app.py\", line 1612, in full_dispatch_request\n", |
| 150 | + " rv = self.dispatch_request()\n", |
| 151 | + " File \"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\flask\\app.py\", line 1598, in dispatch_request\n", |
| 152 | + " return self.view_functions[rule.endpoint](**req.view_args)\n", |
| 153 | + " File \"<ipython-input-1-60d99ab23a5c>\", line 15, in predict\n", |
| 154 | + " int_features=[int(x) for x in request.form.values()]\n", |
| 155 | + " File \"<ipython-input-1-60d99ab23a5c>\", line 15, in <listcomp>\n", |
| 156 | + " int_features=[int(x) for x in request.form.values()]\n", |
| 157 | + "ValueError: invalid literal for int() with base 10: ''\n", |
| 158 | + "127.0.0.1 - - [27/Aug/2020 16:21:02] \"\u001b[1m\u001b[35mPOST /predict HTTP/1.1\u001b[0m\" 500 -\n", |
| 159 | + "127.0.0.1 - - [27/Aug/2020 16:21:09] \"\u001b[37mPOST /predict HTTP/1.1\u001b[0m\" 200 -\n", |
| 160 | + "127.0.0.1 - - [27/Aug/2020 16:21:09] \"\u001b[33mGET /.js/materialize.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 161 | + "127.0.0.1 - - [27/Aug/2020 16:21:09] \"\u001b[33mGET /js/init.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 162 | + "127.0.0.1 - - [27/Aug/2020 16:21:10] \"\u001b[33mGET /.js/materialize.js HTTP/1.1\u001b[0m\" 404 -\n", |
| 163 | + "127.0.0.1 - - [27/Aug/2020 16:21:10] \"\u001b[33mGET /js/init.js HTTP/1.1\u001b[0m\" 404 -\n" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "name": "stdout", |
| 168 | + "output_type": "stream", |
| 169 | + "text": [ |
| 170 | + "[10, 10, 10]\n", |
| 171 | + "[array([10, 10, 10])]\n" |
| 172 | + ] |
| 173 | + } |
| 174 | + ], |
| 175 | + "source": [ |
| 176 | + "from flask import Flask,request, url_for, redirect, render_template\n", |
| 177 | + "import pickle\n", |
| 178 | + "import numpy as np\n", |
| 179 | + "\n", |
| 180 | + "app = Flask(__name__)\n", |
| 181 | + "model=pickle.load(open('model.pkl','rb'))\n", |
| 182 | + "\n", |
| 183 | + "@app.route('/')\n", |
| 184 | + "def hello_world():\n", |
| 185 | + " return render_template(\"forest_fire1.html\")\n", |
| 186 | + "\n", |
| 187 | + "@app.route('/predict',methods=['POST','GET'])\n", |
| 188 | + "def predict():\n", |
| 189 | + " # receive the values send by user in three text boxes thru request object -> requesst.form.values()\n", |
| 190 | + " int_features=[int(x) for x in request.form.values()]\n", |
| 191 | + " final=[np.array(int_features)]\n", |
| 192 | + " print(int_features)\n", |
| 193 | + " print(final)\n", |
| 194 | + " prediction=model.predict_proba(final)\n", |
| 195 | + " output='{0:.{1}f}'.format(prediction[0][1], 2)\n", |
| 196 | + "\n", |
| 197 | + " if output>str(0.5):\n", |
| 198 | + " return render_template('forest_fire1.html',pred='Your Forest is in Danger.\\nProbability of fire occuring is {}'.format(output))\n", |
| 199 | + " else:\n", |
| 200 | + " return render_template('forest_fire1.html',pred='Your Forest is safe.\\n Probability of fire occuring is {}'.format(output))\n", |
| 201 | + "\n", |
| 202 | + "if __name__ == '__main__':\n", |
| 203 | + " app.run(debug=False)\n" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": null, |
| 209 | + "metadata": { |
| 210 | + "collapsed": true |
| 211 | + }, |
| 212 | + "outputs": [], |
| 213 | + "source": [ |
| 214 | + " \n", |
| 215 | + " \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 | + "cell_type": "code", |
| 229 | + "execution_count": null, |
| 230 | + "metadata": { |
| 231 | + "collapsed": true |
| 232 | + }, |
| 233 | + "outputs": [], |
| 234 | + "source": [] |
| 235 | + } |
| 236 | + ], |
| 237 | + "metadata": { |
| 238 | + "kernelspec": { |
| 239 | + "display_name": "Python 3", |
| 240 | + "language": "python", |
| 241 | + "name": "python3" |
| 242 | + }, |
| 243 | + "language_info": { |
| 244 | + "codemirror_mode": { |
| 245 | + "name": "ipython", |
| 246 | + "version": 3 |
| 247 | + }, |
| 248 | + "file_extension": ".py", |
| 249 | + "mimetype": "text/x-python", |
| 250 | + "name": "python", |
| 251 | + "nbconvert_exporter": "python", |
| 252 | + "pygments_lexer": "ipython3", |
| 253 | + "version": "3.6.3" |
| 254 | + } |
| 255 | + }, |
| 256 | + "nbformat": 4, |
| 257 | + "nbformat_minor": 2 |
| 258 | +} |
0 commit comments