arnaucube
arnaucube/objectImageIdentifierAIPublic Self-hosted repository
objectImageIdentifierAI/serverPredictor/HotdogClassifierChooser.ipynb
serverPredictor/HotdogClassifierChooser.ipynbRaw
  1{
  2 "cells": [
  3  {
  4   "cell_type": "code",
  5   "execution_count": 1,
  6   "metadata": {},
  7   "outputs": [
  8    {
  9     "name": "stderr",
 10     "output_type": "stream",
 11     "text": [
 12      "/home/asus/anaconda3/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
 13      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
 14     ]
 15    }
 16   ],
 17   "source": [
 18    "from PIL import Image, ImageOps\n",
 19    "import numpy, os\n",
 20    "from sklearn.cross_validation import cross_val_score\n",
 21    "import numpy as np\n",
 22    "import pandas as pd"
 23   ]
 24  },
 25  {
 26   "cell_type": "code",
 27   "execution_count": 2,
 28   "metadata": {
 29    "collapsed": true
 30   },
 31   "outputs": [],
 32   "source": [
 33    "path=\"dataset/\"\n",
 34    "Xlist=[]\n",
 35    "Ylist=[]\n",
 36    "size = 100, 100"
 37   ]
 38  },
 39  {
 40   "cell_type": "code",
 41   "execution_count": 3,
 42   "metadata": {},
 43   "outputs": [
 44    {
 45     "name": "stdout",
 46     "output_type": "stream",
 47     "text": [
 48      "reading dataset images files\n"
 49     ]
 50    }
 51   ],
 52   "source": [
 53    "print(\"reading dataset images files\")\n",
 54    "for directory in os.listdir(path):\n",
 55    "    for file in os.listdir(path+directory):\n",
 56    "        #print(path+directory+\"/\"+file)\n",
 57    "        img=Image.open(path+directory+\"/\"+file)\n",
 58    "        #resize\n",
 59    "        thumb = ImageOps.fit(img, size, Image.ANTIALIAS)\n",
 60    "        image_data = np.array(thumb).flatten()[:100]\n",
 61    "        #image_data=numpy.array(img).flatten()[:50] #in my case the images dont have the same dimensions, so [:50] only takes the first 50 values\n",
 62    "        Xlist.append(image_data)\n",
 63    "        Ylist.append(directory)"
 64   ]
 65  },
 66  {
 67   "cell_type": "code",
 68   "execution_count": 4,
 69   "metadata": {
 70    "collapsed": true
 71   },
 72   "outputs": [],
 73   "source": [
 74    "from sklearn.model_selection import train_test_split\n",
 75    "X_train, X_test, y_train, y_test = train_test_split(Xlist, Ylist, test_size=0.2)"
 76   ]
 77  },
 78  {
 79   "cell_type": "markdown",
 80   "metadata": {},
 81   "source": [
 82    "### AdaBoostClassifier"
 83   ]
 84  },
 85  {
 86   "cell_type": "code",
 87   "execution_count": 5,
 88   "metadata": {},
 89   "outputs": [
 90    {
 91     "name": "stdout",
 92     "output_type": "stream",
 93     "text": [
 94      "0.762399355878\n"
 95     ]
 96    }
 97   ],
 98   "source": [
 99    "from sklearn.ensemble import AdaBoostClassifier\n",
100    "clf=AdaBoostClassifier(n_estimators=100)\n",
101    "scores = cross_val_score(clf, X_train, y_train, cv=3)\n",
102    "print(scores.mean())"
103   ]
104  },
105  {
106   "cell_type": "markdown",
107   "metadata": {},
108   "source": [
109    "### GaussianNB"
110   ]
111  },
112  {
113   "cell_type": "code",
114   "execution_count": 6,
115   "metadata": {},
116   "outputs": [
117    {
118     "name": "stdout",
119     "output_type": "stream",
120     "text": [
121      "0.721908939014\n"
122     ]
123    }
124   ],
125   "source": [
126    "from sklearn.naive_bayes import GaussianNB\n",
127    "clf = GaussianNB()\n",
128    "scores = cross_val_score(clf, Xlist, Ylist)\n",
129    "print(scores.mean())"
130   ]
131  },
132  {
133   "cell_type": "markdown",
134   "metadata": {},
135   "source": [
136    "### KNeighborsClassifier"
137   ]
138  },
139  {
140   "cell_type": "code",
141   "execution_count": 7,
142   "metadata": {},
143   "outputs": [
144    {
145     "name": "stdout",
146     "output_type": "stream",
147     "text": [
148      "0.751357560568\n"
149     ]
150    }
151   ],
152   "source": [
153    "from sklearn.neighbors import KNeighborsClassifier\n",
154    "clf = KNeighborsClassifier(n_neighbors=10)\n",
155    "scores = cross_val_score(clf, Xlist, Ylist)\n",
156    "print(scores.mean())"
157   ]
158  },
159  {
160   "cell_type": "markdown",
161   "metadata": {},
162   "source": [
163    "### LinearSVC"
164   ]
165  },
166  {
167   "cell_type": "code",
168   "execution_count": 8,
169   "metadata": {},
170   "outputs": [
171    {
172     "name": "stdout",
173     "output_type": "stream",
174     "text": [
175      "0.66238512949\n"
176     ]
177    }
178   ],
179   "source": [
180    "from sklearn.svm import LinearSVC\n",
181    "clf = LinearSVC()\n",
182    "scores = cross_val_score(clf, Xlist, Ylist)\n",
183    "print(scores.mean())"
184   ]
185  },
186  {
187   "cell_type": "markdown",
188   "metadata": {},
189   "source": [
190    "### SVC"
191   ]
192  },
193  {
194   "cell_type": "code",
195   "execution_count": 9,
196   "metadata": {},
197   "outputs": [
198    {
199     "name": "stdout",
200     "output_type": "stream",
201     "text": [
202      "0.668650793651\n"
203     ]
204    }
205   ],
206   "source": [
207    "from sklearn.svm import SVC\n",
208    "clf = SVC()\n",
209    "scores = cross_val_score(clf, Xlist, Ylist)\n",
210    "print(scores.mean())"
211   ]
212  },
213  {
214   "cell_type": "markdown",
215   "metadata": {},
216   "source": [
217    "### GaussianProcessClassifier"
218   ]
219  },
220  {
221   "cell_type": "code",
222   "execution_count": 10,
223   "metadata": {},
224   "outputs": [
225    {
226     "name": "stdout",
227     "output_type": "stream",
228     "text": [
229      "0.491228070175\n"
230     ]
231    }
232   ],
233   "source": [
234    "from sklearn.gaussian_process import GaussianProcessClassifier\n",
235    "clf = GaussianProcessClassifier()\n",
236    "scores = cross_val_score(clf, Xlist, Ylist)\n",
237    "print(scores.mean())"
238   ]
239  },
240  {
241   "cell_type": "markdown",
242   "metadata": {},
243   "source": [
244    "### RandomForestClassifier"
245   ]
246  },
247  {
248   "cell_type": "code",
249   "execution_count": 11,
250   "metadata": {},
251   "outputs": [
252    {
253     "name": "stdout",
254     "output_type": "stream",
255     "text": [
256      "0.775793650794\n"
257     ]
258    }
259   ],
260   "source": [
261    "from sklearn.ensemble import RandomForestClassifier\n",
262    "clf = RandomForestClassifier()\n",
263    "scores = cross_val_score(clf, Xlist, Ylist)\n",
264    "print(scores.mean())"
265   ]
266  },
267  {
268   "cell_type": "markdown",
269   "metadata": {},
270   "source": [
271    "# Hyperparameters Tuning using sklearn pipeline and gridsearch"
272   ]
273  },
274  {
275   "cell_type": "code",
276   "execution_count": 12,
277   "metadata": {
278    "collapsed": true
279   },
280   "outputs": [],
281   "source": [
282    "from sklearn.pipeline import Pipeline\n",
283    "from sklearn.model_selection import GridSearchCV\n",
284    "from sklearn.model_selection import RandomizedSearchCV"
285   ]
286  },
287  {
288   "cell_type": "code",
289   "execution_count": 13,
290   "metadata": {
291    "collapsed": true
292   },
293   "outputs": [],
294   "source": [
295    "pipe = [Pipeline([\n",
296    "        ('clf', RandomForestClassifier()),\n",
297    "    ]),\n",
298    "    Pipeline([\n",
299    "        ('clf', KNeighborsClassifier()),\n",
300    "    ]),\n",
301    "    Pipeline([\n",
302    "        ('clf', GaussianProcessClassifier()),\n",
303    "    ]),\n",
304    "    Pipeline([\n",
305    "        ('clf', AdaBoostClassifier()),\n",
306    "    ]),\n",
307    "    Pipeline([\n",
308    "        ('clf', SVC()),\n",
309    "    ]),\n",
310    "]"
311   ]
312  },
313  {
314   "cell_type": "code",
315   "execution_count": 14,
316   "metadata": {
317    "collapsed": true
318   },
319   "outputs": [],
320   "source": [
321    "param_grid = [dict(clf__n_estimators=[3, 10, 100]),\n",
322    "              dict(clf__n_neighbors=[3,10]),\n",
323    "              dict(clf__n_restarts_optimizer=[0,1]),\n",
324    "              dict(clf__n_estimators=[3, 10, 100]),\n",
325    "              dict(clf__C=[3, 10, 100]),\n",
326    "             ]"
327   ]
328  },
329  {
330   "cell_type": "code",
331   "execution_count": 15,
332   "metadata": {
333    "collapsed": true
334   },
335   "outputs": [],
336   "source": [
337    "grid_search = GridSearchCV(pipe, param_grid=param_grid, n_jobs=-1, verbose=1, cv=3)"
338   ]
339  },
340  {
341   "cell_type": "code",
342   "execution_count": 16,
343   "metadata": {
344    "collapsed": true
345   },
346   "outputs": [],
347   "source": [
348    "# Utility function to report best scores\n",
349    "def report(results, n_top=10):\n",
350    "    for i in range(1, n_top + 1):\n",
351    "        candidates = np.flatnonzero(results['rank_test_score'] == i)\n",
352    "        for candidate in candidates:\n",
353    "            print(\"Model with rank: {0}\".format(i))\n",
354    "            print(\"Mean validation score: {0:.3f} (std: {1:.3f})\".format(\n",
355    "                  results['mean_test_score'][candidate],\n",
356    "                  results['std_test_score'][candidate]))\n",
357    "            print(\"Parameters: {0}\".format(results['params'][candidate]))\n",
358    "            print(\"\")"
359   ]
360  },
361  {
362   "cell_type": "code",
363   "execution_count": 17,
364   "metadata": {
365    "scrolled": false
366   },
367   "outputs": [
368    {
369     "name": "stdout",
370     "output_type": "stream",
371     "text": [
372      "-----\n",
373      "classifier:\n",
374      "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
375      "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
376      "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
377      "            min_samples_leaf=1, min_samples_split=2,\n",
378      "            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,\n",
379      "            oob_score=False, random_state=None, verbose=0,\n",
380      "            warm_start=False)\n",
381      "Fitting 3 folds for each of 3 candidates, totalling 9 fits\n"
382     ]
383    },
384    {
385     "name": "stderr",
386     "output_type": "stream",
387     "text": [
388      "[Parallel(n_jobs=-1)]: Done   9 out of   9 | elapsed:    0.8s finished\n"
389     ]
390    },
391    {
392     "name": "stdout",
393     "output_type": "stream",
394     "text": [
395      "GridSearchCV took 1.45 seconds for 3 candidate parameter settings.\n",
396      "finished GridSearch\n",
397      "Model with rank: 1\n",
398      "Mean validation score: 0.800 (std: 0.085)\n",
399      "Parameters: {'clf__n_estimators': 100}\n",
400      "\n",
401      "Model with rank: 2\n",
402      "Mean validation score: 0.778 (std: 0.035)\n",
403      "Parameters: {'clf__n_estimators': 3}\n",
404      "\n",
405      "Model with rank: 3\n",
406      "Mean validation score: 0.741 (std: 0.046)\n",
407      "Parameters: {'clf__n_estimators': 10}\n",
408      "\n",
409      "-----\n",
410      "classifier:\n",
411      "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
412      "           metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n",
413      "           weights='uniform')\n",
414      "Fitting 3 folds for each of 2 candidates, totalling 6 fits\n",
415      "GridSearchCV took 0.35 seconds for 2 candidate parameter settings.\n",
416      "finished GridSearch\n",
417      "Model with rank: 1\n",
418      "Mean validation score: 0.756 (std: 0.056)\n",
419      "Parameters: {'clf__n_neighbors': 10}\n",
420      "\n",
421      "Model with rank: 2\n",
422      "Mean validation score: 0.748 (std: 0.111)\n",
423      "Parameters: {'clf__n_neighbors': 3}\n",
424      "\n",
425      "-----\n",
426      "classifier:\n",
427      "GaussianProcessClassifier(copy_X_train=True, kernel=None,\n",
428      "             max_iter_predict=100, multi_class='one_vs_rest', n_jobs=1,\n",
429      "             n_restarts_optimizer=0, optimizer='fmin_l_bfgs_b',\n",
430      "             random_state=None, warm_start=False)\n",
431      "Fitting 3 folds for each of 2 candidates, totalling 6 fits\n"
432     ]
433    },
434    {
435     "name": "stderr",
436     "output_type": "stream",
437     "text": [
438      "[Parallel(n_jobs=-1)]: Done   6 out of   6 | elapsed:    0.1s remaining:    0.0s\n",
439      "[Parallel(n_jobs=-1)]: Done   6 out of   6 | elapsed:    0.1s finished\n"
440     ]
441    },
442    {
443     "name": "stdout",
444     "output_type": "stream",
445     "text": [
446      "GridSearchCV took 0.47 seconds for 2 candidate parameter settings.\n",
447      "finished GridSearch\n",
448      "Model with rank: 1\n",
449      "Mean validation score: 0.496 (std: 0.005)\n",
450      "Parameters: {'clf__n_restarts_optimizer': 0}\n",
451      "\n",
452      "Model with rank: 1\n",
453      "Mean validation score: 0.496 (std: 0.005)\n",
454      "Parameters: {'clf__n_restarts_optimizer': 1}\n",
455      "\n",
456      "-----\n",
457      "classifier:\n",
458      "AdaBoostClassifier(algorithm='SAMME.R', base_estimator=None,\n",
459      "          learning_rate=1.0, n_estimators=50, random_state=None)\n",
460      "Fitting 3 folds for each of 3 candidates, totalling 9 fits\n"
461     ]
462    },
463    {
464     "name": "stderr",
465     "output_type": "stream",
466     "text": [
467      "[Parallel(n_jobs=-1)]: Done   6 out of   6 | elapsed:    0.3s remaining:    0.0s\n",
468      "[Parallel(n_jobs=-1)]: Done   6 out of   6 | elapsed:    0.3s finished\n"
469     ]
470    },
471    {
472     "name": "stdout",
473     "output_type": "stream",
474     "text": [
475      "GridSearchCV took 1.06 seconds for 3 candidate parameter settings.\n",
476      "finished GridSearch\n",
477      "Model with rank: 1\n",
478      "Mean validation score: 0.793 (std: 0.088)\n",
479      "Parameters: {'clf__n_estimators': 3}\n",
480      "\n",
481      "Model with rank: 2\n",
482      "Mean validation score: 0.785 (std: 0.084)\n",
483      "Parameters: {'clf__n_estimators': 10}\n",
484      "\n",
485      "Model with rank: 3\n",
486      "Mean validation score: 0.763 (std: 0.048)\n",
487      "Parameters: {'clf__n_estimators': 100}\n",
488      "\n",
489      "-----\n",
490      "classifier:\n",
491      "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
492      "  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',\n",
493      "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
494      "  tol=0.001, verbose=False)\n",
495      "Fitting 3 folds for each of 3 candidates, totalling 9 fits\n"
496     ]
497    },
498    {
499     "name": "stderr",
500     "output_type": "stream",
501     "text": [
502      "[Parallel(n_jobs=-1)]: Done   9 out of   9 | elapsed:    0.9s finished\n"
503     ]
504    },
505    {
506     "name": "stdout",
507     "output_type": "stream",
508     "text": [
509      "GridSearchCV took 0.36 seconds for 3 candidate parameter settings.\n",
510      "finished GridSearch\n",
511      "Model with rank: 1\n",
512      "Mean validation score: 0.689 (std: 0.067)\n",
513      "Parameters: {'clf__C': 3}\n",
514      "\n",
515      "Model with rank: 1\n",
516      "Mean validation score: 0.689 (std: 0.067)\n",
517      "Parameters: {'clf__C': 10}\n",
518      "\n",
519      "Model with rank: 1\n",
520      "Mean validation score: 0.689 (std: 0.067)\n",
521      "Parameters: {'clf__C': 100}\n",
522      "\n"
523     ]
524    },
525    {
526     "name": "stderr",
527     "output_type": "stream",
528     "text": [
529      "[Parallel(n_jobs=-1)]: Done   9 out of   9 | elapsed:    0.1s finished\n"
530     ]
531    }
532   ],
533   "source": [
534    "from time import time\n",
535    "\n",
536    "for i in range(len(pipe)):\n",
537    "        start = time()\n",
538    "        print(\"-----\")\n",
539    "        print(\"classifier:\")\n",
540    "        print(pipe[i].named_steps['clf'])\n",
541    "        grid_search = GridSearchCV(pipe[i], param_grid[i], n_jobs=-1, verbose=1, cv=3)\n",
542    "        grid_search.fit(X_train, y_train)\n",
543    "        print(\"GridSearchCV took %.2f seconds for %d candidate parameter settings.\"\n",
544    "              % (time() - start, len(grid_search.cv_results_['params'])))\n",
545    "        print(\"finished GridSearch\")\n",
546    "        report(grid_search.cv_results_)"
547   ]
548  },
549  {
550   "cell_type": "code",
551   "execution_count": null,
552   "metadata": {
553    "collapsed": true
554   },
555   "outputs": [],
556   "source": []
557  }
558 ],
559 "metadata": {
560  "kernelspec": {
561   "display_name": "Python 3",
562   "language": "python",
563   "name": "python3"
564  },
565  "language_info": {
566   "codemirror_mode": {
567    "name": "ipython",
568    "version": 3
569   },
570   "file_extension": ".py",
571   "mimetype": "text/x-python",
572   "name": "python",
573   "nbconvert_exporter": "python",
574   "pygments_lexer": "ipython3",
575   "version": "3.6.3"
576  }
577 },
578 "nbformat": 4,
579 "nbformat_minor": 2
580}