Guest User

Untitled

a guest
May 23rd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 3,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "#Resize the data\n",
  10. "\n",
  11. "from PIL import Image\n",
  12. "import numpy as np\n",
  13. "from matplotlib.pyplot import *\n",
  14. "import matplotlib.pyplot as plt\n",
  15. "from PIL import Image\n",
  16. "import os, sys\n",
  17. "path = \"/Users/vasaini/MLHACK/orig\"\n",
  18. "dirs = os.listdir( path )\n",
  19. "def resize():\n",
  20. " for item in dirs:\n",
  21. " if os.path.isfile(path+item):\n",
  22. " im = Image.open(path+item)\n",
  23. " f, e = os.path.splitext(path+item)\n",
  24. " imResize = im.resize ((128,128),\"/Users/vasaini/MLHACK/resized\")\n",
  25. " \n",
  26. "resize()"
  27. ]
  28. },
  29. {
  30. "cell_type": "code",
  31. "execution_count": 4,
  32. "metadata": {},
  33. "outputs": [],
  34. "source": [
  35. "#Data as np array\n",
  36. "\n",
  37. "import os\n",
  38. "for file in os.listdir(r\"/Users/vasaini/MLHACK/resized\"):\n",
  39. " img = Image.open(os.path.join(r'/Users/vasaini/MLHACK/resized', file))\n",
  40. " data = np.array(img)"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": 5,
  46. "metadata": {},
  47. "outputs": [
  48. {
  49. "name": "stdout",
  50. "output_type": "stream",
  51. "text": [
  52. "['axes', 'boots', 'carabiners', 'crampons', 'gloves', 'hardshell_jackets', 'harnesses', 'helmets', 'insulated_jackets', 'pulleys', 'rope', 'tents']\n"
  53. ]
  54. }
  55. ],
  56. "source": [
  57. "#Claases in a list\n",
  58. "\n",
  59. "root='/Users/vasaini/MLHACK/orig'\n",
  60. "dirlist = [ item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item)) ]\n",
  61. "print (dirlist)"
  62. ]
  63. },
  64. {
  65. "cell_type": "code",
  66. "execution_count": 8,
  67. "metadata": {},
  68. "outputs": [
  69. {
  70. "ename": "NameError",
  71. "evalue": "name 'train_test_split' is not defined",
  72. "output_type": "error",
  73. "traceback": [
  74. "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
  75. "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
  76. "\u001b[1;32m<ipython-input-8-9b2434c22d06>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m# Split our data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m train, test, train_labels, test_labels = train_test_split(features,\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mlabels\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mtest_size\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.33\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
  77. "\u001b[1;31mNameError\u001b[0m: name 'train_test_split' is not defined"
  78. ]
  79. }
  80. ],
  81. "source": [
  82. "\n",
  83. "# Split our data\n",
  84. "train, test, train_labels, test_labels = train_test_split(features,\n",
  85. " labels,\n",
  86. " test_size=0.33,\n",
  87. " random_state=42)\n",
  88. "\n"
  89. ]
  90. },
  91. {
  92. "cell_type": "markdown",
  93. "metadata": {},
  94. "source": [
  95. "Classification SVM-Multi-class\n",
  96. "\n"
  97. ]
  98. },
  99. {
  100. "cell_type": "code",
  101. "execution_count": null,
  102. "metadata": {},
  103. "outputs": [],
  104. "source": [
  105. "import numpy \n",
  106. "from PIL import Image\n",
  107. "import os\n",
  108. "from sklearn.svm import SVC\n",
  109. "from sklearn.model_selection import train_test_split\n",
  110. "for file in os.listdir('C:/Users/vasaini/MLHACK/resized'):\n",
  111. " img = Image.open(os.path.join('Users/vasaini/Desktop/Gear images',file))\n",
  112. " data = np.array(img)\n",
  113. "# fit a SVM model to the data\n",
  114. "model = SVC()\n",
  115. "model.fit(dataset.data, dataset.target)\n",
  116. "print(model)\n",
  117. "# make predictions\n",
  118. "predicted = model.predict(dataset.data)\n",
  119. "# Evaluate accuracy\n",
  120. "print(accuracy_score(test_labels, predicted))\n",
  121. "# summarize the fit of the model\n",
  122. "print(metrics.classification_report(expected, predicted))\n",
  123. "print(metrics.confusion_matrix(expected, predicted))\n"
  124. ]
  125. },
  126. {
  127. "cell_type": "code",
  128. "execution_count": null,
  129. "metadata": {},
  130. "outputs": [],
  131. "source": [
  132. "# creating a confusion matrix\n",
  133. "knn_predictions = knn.predict(X_test) \n",
  134. "cm = confusion_matrix(y_test, knn_predictions)\n"
  135. ]
  136. }
  137. ],
  138. "metadata": {
  139. "kernelspec": {
  140. "display_name": "Python 3",
  141. "language": "python",
  142. "name": "python3"
  143. }
  144. },
  145. "nbformat": 4,
  146. "nbformat_minor": 2
  147. }
Add Comment
Please, Sign In to add comment