|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "# The Iris Setosa\n", |
| 10 | + "from IPython.display import Image\n", |
| 11 | + "url = 'http://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'\n", |
| 12 | + "Image(url,width=400, height=300)" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": null, |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "# The Iris Versicolor\n", |
| 22 | + "from IPython.display import Image\n", |
| 23 | + "url = 'http://upload.wikimedia.org/wikipedia/commons/4/41/Iris_versicolor_3.jpg'\n", |
| 24 | + "Image(url,width=400, height=300)" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "# The Iris Virginica\n", |
| 34 | + "from IPython.display import Image\n", |
35 | + "url = 'http://upload.wikimedia.org/wikipedia/commons/9/9f/Iris_virginica.jpg'\n", |
| 36 | + "Image(url,width=400, height=300)" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": null, |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "import seaborn as sns\n", |
| 46 | + "iris = sns.load_dataset('iris')\n", |
| 47 | + "iris.head(5)" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "\n", |
| 55 | + "## Exploratory Data Analysis\n" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": null, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "import pandas as pd\n", |
| 65 | + "import matplotlib.pyplot as plt\n", |
| 66 | + "%matplotlib inline" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "# Setosa is the most separable. \n", |
| 76 | + "sns.pairplot(iris,hue='species',palette='Dark2')" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "**Create a kde plot of sepal_length versus sepal width for setosa species of flower.**" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "setosa = iris[iris['species']=='setosa']\n", |
| 93 | + "sns.kdeplot( setosa['sepal_width'], setosa['sepal_length'],\n", |
| 94 | + " cmap=\"Spectral\", shade=True, shade_lowest=False)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "# Train Test Split\n", |
| 102 | + "\n", |
| 103 | + "** Split your data into a training set and a testing set.**" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "from sklearn.model_selection import train_test_split" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [ |
| 121 | + "X = iris.drop('species',axis=1)\n", |
| 122 | + "y = iris['species']\n", |
| 123 | + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "# Train a Model\n", |
| 131 | + "\n", |
| 132 | + "Now its time to train a Support Vector Machine Classifier. \n", |
| 133 | + "\n", |
| 134 | + "**Call the SVC() model from sklearn and fit the model to the training data.**" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
| 142 | + "source": [ |
| 143 | + "from sklearn.svm import SVC" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [ |
| 152 | + "svc_model = SVC()" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [], |
| 160 | + "source": [ |
| 161 | + "svc_model.fit(X_train,y_train)" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": { |
| 167 | + "collapsed": true |
| 168 | + }, |
| 169 | + "source": [ |
| 170 | + "## Model Evaluation\n", |
| 171 | + "\n", |
| 172 | + "**Now get predictions from the model and create a confusion matrix and a classification report.**" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "predictions = svc_model.predict(X_test)" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "code", |
| 186 | + "execution_count": null, |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [], |
| 189 | + "source": [ |
| 190 | + "from sklearn.metrics import classification_report,confusion_matrix" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [ |
| 199 | + "print(confusion_matrix(y_test,predictions))" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": null, |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [], |
| 207 | + "source": [ |
| 208 | + "print(classification_report(y_test,predictions))" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "markdown", |
| 213 | + "m
F42D
etadata": {}, |
| 214 | + "source": [ |
| 215 | + "## Gridsearch Practice\n", |
| 216 | + "\n", |
| 217 | + "** Import GridsearchCV from SciKit Learn.**" |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "code", |
| 222 | + "execution_count": null, |
| 223 | + "metadata": {}, |
| 224 | + "outputs": [], |
| 225 | + "source": [ |
| 226 | + "from sklearn.model_selection import GridSearchCV" |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "markdown", |
| 231 | + "metadata": {}, |
| 232 | + "source": [ |
| 233 | + "**Create a dictionary called param_grid and fill out some parameters for C and gamma.**" |
| 234 | + ] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "code", |
| 238 | + "execution_count": null, |
| 239 | + "metadata": {}, |
| 240 | + "outputs": [], |
| 241 | + "source": [ |
| 242 | + "param_grid = {'C': [0.1,1, 10, 100], 'gamma': [1,0.1,0.01,0.001]} " |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "markdown", |
| 247 | + "metadata": {}, |
| 248 | + "source": [ |
| 249 | + "** Create a GridSearchCV object and fit it to the training data.**" |
| 250 | + ] |
| 251 | + }, |
| 252 | + { |
| 253 | + "cell_type": "code", |
| 254 | + "execution_count": null, |
| 255 | + "metadata": {}, |
| 256 | + "outputs": [], |
| 257 | + "source": [ |
| 258 | + "grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=2)\n", |
| 259 | + "grid.fit(X_train,y_train)" |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "markdown", |
| 264 | + "metadata": {}, |
| 265 | + "source": [ |
| 266 | + "** Now take that grid model and create some predictions using the test set and create classification reports and confusion matrices for them. Were you able to improve?**" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": null, |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [], |
| 274 | + "source": [ |
| 275 | + "grid_predictions = grid.predict(X_test)" |
| 276 | + ] |
| 277 | + }, |
| 278 | + { |
| 279 | + "cell_type": "code", |
| 280 | + "execution_count": null, |
| 281 | + "metadata": {}, |
| 282 | + "outputs": [], |
| 283 | + "source": [ |
| 284 | + "print(confusion_matrix(y_test,grid_predictions))" |
| 285 | + ] |
| 286 | + }, |
| 287 | + { |
| 288 | + "cell_type": "code", |
| 289 | + "execution_count": null, |
| 290 | + "metadata": {}, |
| 291 | + "outputs": [], |
| 292 | + "source": [ |
| 293 | + "print(classification_report(y_test,grid_predictions))" |
| 294 | + ] |
| 295 | + } |
| 296 | + ], |
| 297 | + "metadata": { |
| 298 | + "kernelspec": { |
| 299 | + "display_name": "Python 3", |
| 300 | + "language": "python", |
| 301 | + "name": "python3" |
| 302 | + }, |
| 303 | + "language_info": { |
| 304 | + "codemirror_mode": { |
| 305 | + "name": "ipython", |
| 306 | + "version": 3 |
| 307 | + }, |
| 308 | + "file_extension": ".py", |
| 309 | + "mimetype": "text/x-python", |
| 310 | + "name": "python", |
| 311 | + "nbconvert_exporter": "python", |
| 312 | + "pygments_lexer": "ipython3", |
| 313 | + "version": "3.7.6" |
| 314 | + } |
| 315 | + }, |
| 316 | + "nbformat": 4, |
| 317 | + "nbformat_minor": 1 |
| 318 | +} |
0 commit comments