Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "#Resize the data\n",
- "\n",
- "from PIL import Image\n",
- "import numpy as np\n",
- "from matplotlib.pyplot import *\n",
- "import matplotlib.pyplot as plt\n",
- "from PIL import Image\n",
- "import os, sys\n",
- "path = \"/Users/vasaini/MLHACK/orig\"\n",
- "dirs = os.listdir( path )\n",
- "def resize():\n",
- " for item in dirs:\n",
- " if os.path.isfile(path+item):\n",
- " im = Image.open(path+item)\n",
- " f, e = os.path.splitext(path+item)\n",
- " imResize = im.resize ((128,128),\"/Users/vasaini/MLHACK/resized\")\n",
- " \n",
- "resize()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "#Data as np array\n",
- "\n",
- "import os\n",
- "for file in os.listdir(r\"/Users/vasaini/MLHACK/resized\"):\n",
- " img = Image.open(os.path.join(r'/Users/vasaini/MLHACK/resized', file))\n",
- " data = np.array(img)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['axes', 'boots', 'carabiners', 'crampons', 'gloves', 'hardshell_jackets', 'harnesses', 'helmets', 'insulated_jackets', 'pulleys', 'rope', 'tents']\n"
- ]
- }
- ],
- "source": [
- "#Claases in a list\n",
- "\n",
- "root='/Users/vasaini/MLHACK/orig'\n",
- "dirlist = [ item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item)) ]\n",
- "print (dirlist)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "ename": "NameError",
- "evalue": "name 'train_test_split' is not defined",
- "output_type": "error",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
- "\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",
- "\u001b[1;31mNameError\u001b[0m: name 'train_test_split' is not defined"
- ]
- }
- ],
- "source": [
- "\n",
- "# Split our data\n",
- "train, test, train_labels, test_labels = train_test_split(features,\n",
- " labels,\n",
- " test_size=0.33,\n",
- " random_state=42)\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Classification SVM-Multi-class\n",
- "\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "import numpy \n",
- "from PIL import Image\n",
- "import os\n",
- "from sklearn.svm import SVC\n",
- "from sklearn.model_selection import train_test_split\n",
- "for file in os.listdir('C:/Users/vasaini/MLHACK/resized'):\n",
- " img = Image.open(os.path.join('Users/vasaini/Desktop/Gear images',file))\n",
- " data = np.array(img)\n",
- "# fit a SVM model to the data\n",
- "model = SVC()\n",
- "model.fit(dataset.data, dataset.target)\n",
- "print(model)\n",
- "# make predictions\n",
- "predicted = model.predict(dataset.data)\n",
- "# Evaluate accuracy\n",
- "print(accuracy_score(test_labels, predicted))\n",
- "# summarize the fit of the model\n",
- "print(metrics.classification_report(expected, predicted))\n",
- "print(metrics.confusion_matrix(expected, predicted))\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# creating a confusion matrix\n",
- "knn_predictions = knn.predict(X_test) \n",
- "cm = confusion_matrix(y_test, knn_predictions)\n"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment