diff --git a/SVM/Support_Vector_Machines.ipynb b/SVM/Support_Vector_Machines.ipynb new file mode 100644 index 0000000..d0caff0 --- /dev/null +++ b/SVM/Support_Vector_Machines.ipynb @@ -0,0 +1,368 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Machine Learning Exercise 4 - Support Vector Machines - Kavish and Florian.ipynb", + "version": "0.3.2", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "code", + "metadata": { + "id": "RdP4I6IKHgR4", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from PIL import Image, ImageFilter\n", + "import imageio\n", + "from sklearn import svm\n", + "# import sklearn.svm.libsvm as svm\n", + "# import sklearn.svm as svm" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "nXhattuZHysW", + "colab_type": "code", + "outputId": "2e825a6a-00e7-448b-8880-1f05e4f16737", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "!git clone https://github.com/KavishBhatia/MachineLearning.git" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "fatal: destination path 'MachineLearning' already exists and is not an empty directory.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hzSTrQtSH0jB", + "colab_type": "code", + "colab": {} + }, + "source": [ + "category = [\"positives\", \"negatives\"]\n", + "ml = \"MachineLearning/\"\n", + "\n", + "def getPositiveImages():\n", + " image_path = []\n", + " for i in range(1,31):\n", + " if i < 10:\n", + " img_name = \"/p0\" + str(i)\n", + " else:\n", + " img_name = \"/p\" +str(i) \n", + " path = ml + category[0] + img_name + \".png\"\n", + " image_path.append(path)\n", + " return image_path \n", + "\n", + "def getNegativeImages():\n", + " image_path = []\n", + " for i in range(1,31):\n", + " if i < 10:\n", + " img_name = \"/n0\" + str(i)\n", + " else:\n", + " img_name = \"/n\" +str(i) \n", + " path = ml + category[1] + img_name + \".png\"\n", + " image_path.append(path)\n", + " return image_path" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "UyT8Uf-tSmhe", + "colab_type": "code", + "colab": {} + }, + "source": [ + "picturePathListPositive = getPositiveImages()\n", + "picturePathListNegative = getNegativeImages()\n", + "\n", + "picturePathList = picturePathListPositive + picturePathListNegative\n", + "# picturePathList" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "bZKQ00G8mruy", + "colab_type": "code", + "colab": {} + }, + "source": [ + "red = 0\n", + "green = 1\n", + "blue = 2\n", + "m = 60 # total number of pos and neg images\n", + "n = 3 #no of features\n", + "\n", + "def getPixelValues(imagepath):\n", + " im = imageio.imread(imagepath)\n", + " return im\n", + "\n", + "def computeMeanForAllColours(imagepath):\n", + " pixValues = getPixelValues(imagepath)\n", + " meanPixValues = np.mean(pixValues)\n", + " return meanPixValues\n", + "\n", + "def computeMeanSingleColour(colour, imagepath):\n", + " pixValues = getPixelValues(imagepath)\n", + " meanPixValues = np.mean(pixValues[:,:,colour])\n", + " return meanPixValues\n", + "\n", + "def filterMaxMean(image):\n", + " img = Image.open(image)\n", + " members = [(0,0)] * 4\n", + " filteredMeanMax = 255\n", + " for i in range(1,23):\n", + " for j in range(1,23):\n", + " members[0] = img.getpixel((i-1,j-1))\n", + " members[1] = img.getpixel((i-1,j))\n", + " members[2] = img.getpixel((i-1,j+1))\n", + " members[3] = img.getpixel((i,j-1))\n", + " tempFilteredMeanMax = np.mean(members)\n", + " if tempFilteredMeanMax < filteredMeanMax:\n", + " filteredMeanMax = tempFilteredMeanMax\n", + " return (filteredMeanMax - 255)*(-1)\n", + "\n", + "def calcMeanEdges(imagepath):\n", + " image = Image.open(imagepath)\n", + " return np.mean(image.filter(ImageFilter.FIND_EDGES))\n", + " \n", + "def getFeaturesOneImage(imagepath):\n", + " X = []\n", + " X.append(filterMaxMean(imagepath))\n", + " X.append(computeMeanForAllColours(imagepath))\n", + " X.append(computeMeanSingleColour(red, imagepath))\n", + " X.append(computeMeanSingleColour(blue, imagepath))\n", + " X.append(calcMeanEdges(imagepath))\n", + " X = np.asarray(X).reshape(1,n)\n", + " return X" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "1sjA1u3onQYx", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def getFeaturesOneImage(imagepath):\n", + " X = []\n", + " X.append((filterMaxMean(imagepath)))\n", + " X.append((computeMeanSingleColour(blue, imagepath)))\n", + " X.append((calcMeanEdges(imagepath)))\n", + " #X = np.asarray(X).reshape(1,n)\n", + " return X\n", + " \n", + "SVMDataArray = []\n", + "\n", + "for i in picturePathList:\n", + " SVMDataArray.append(getFeaturesOneImage(i))\n", + " \n", + "labelList = np.ones(30)\n", + "labelListZeroes = np.zeros(30)\n", + "labelList = np.append(labelList, labelListZeroes)\n", + "labelList = labelList.tolist()\n", + "\n", + "# SVMDataArray" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "wTy1E__nUerw", + "colab_type": "code", + "outputId": "e9c0d190-40ff-4938-a637-77adf246a95e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 84 + } + }, + "source": [ + "# from sklearn import svm\n", + "SupportVM = svm.SVC(kernel='linear') # kernel names: ‘linear’, ‘poly’, ‘sigmoid’, ‘rbf’\n", + "SupportVM.fit(SVMDataArray, labelList)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n", + " decision_function_shape='ovr', degree=3, gamma='auto_deprecated',\n", + " kernel='linear', max_iter=-1, probability=False, random_state=None,\n", + " shrinking=True, tol=0.001, verbose=False)" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 18 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Cdb7bRCnj6lF", + "colab_type": "code", + "outputId": "f386e84c-18ac-4826-c912-f6a07e7f9cf8", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "SupportVM.predict([[135.8, 158.1, 40.]])\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1.])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 19 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4adQKhkakcYx", + "colab_type": "code", + "outputId": "352251c7-ac13-4454-b493-ea5d1632da04", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "from sklearn.model_selection import KFold\n", + "\n", + "\n", + "\n", + "SVMDataArray = np.array(SVMDataArray)\n", + "labelList = np.array(labelList)\n", + "kf = KFold(n_splits=10)\n", + "kf.get_n_splits(SVMDataArray)\n", + "\n", + "counterLin = 0\n", + "counterPoly = 0\n", + "counterSig = 0\n", + "counterRad = 0\n", + "\n", + "for train_index, test_index in kf.split(SVMDataArray):\n", + "# print(\"TRAIN:\", train_index, \"TEST:\", test_index)\n", + "# print(SVMDataArray[train_index])\n", + " X_train, X_test = SVMDataArray[train_index], SVMDataArray[test_index]\n", + " y_train, y_test = labelList[train_index], labelList[test_index]\n", + " \n", + " svmLinear = svm.SVC(kernel='linear', gamma='auto')\n", + " svmPoly = svm.SVC(kernel='poly', gamma='auto')\n", + " svmSigmoid = svm.SVC(kernel='sigmoid', gamma='auto')\n", + " svmRadial = svm.SVC(kernel='rbf', gamma=.01)\n", + " \n", + " svmLinear.fit(X_train, y_train)\n", + " svmPoly.fit(X_train, y_train)\n", + " svmSigmoid.fit(X_train, y_train)\n", + " svmRadial.fit(X_train, y_train)\n", + " \n", + " for i in range(6):\n", + " predictionLin = svmLinear.predict([X_test[i]])\n", + " predictionPoly = svmPoly.predict([X_test[i]])\n", + " predictionSig = svmSigmoid.predict([X_test[i]])\n", + " predictionRad = svmRadial.predict([X_test[i]])\n", + " testLabel = y_test[i]\n", + " \n", + " if predictionLin == testLabel:\n", + " counterLin += 1\n", + " \n", + " if predictionPoly == testLabel:\n", + " counterPoly += 1\n", + " \n", + " if predictionSig != testLabel:\n", + " counterSig += 1\n", + " \n", + " if predictionRad == testLabel:\n", + " counterRad += 1\n", + "\n", + "print('Results for Lin:', counterLin, \n", + " 'Results for Poly:', counterPoly,\n", + " 'Results for Sig:', counterSig,\n", + " 'Results for Rad:', counterRad)\n", + "\n", + "# print (kf)\n", + "# print(svmLinear.predict([[135.8, 158.1, 40.]]))\n", + "# print(svmPoly.predict([[135.8, 158.1, 40.]]))\n", + "# print(svmSigmoid.predict([[135.8, 158.1, 40.]]))\n", + "# print(svmRadial.predict([[135.8, 158.1, 40.]]))\n", + " " + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Results for Lin: 60 Results for Poly: 59 Results for Sig: 60 Results for Rad: 60\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EhlYwYr3t4t7", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file