{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Experiments with the one vs rest multiclass classification scheme" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "from sklearn import datasets\n", "from sklearn.multiclass import OneVsRestClassifier as OvR\n", "from sklearn.svm import LinearSVC\n", "import numpy as np\n", "import copy\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "digits = datasets.load_digits()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use the \"Optical Recognition of Handwritten Digits Data Set\" from UCI (included in scikit learn and already loaded on the previous line). Let us plot the first 10 images in the dataset." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWYAAADRCAYAAAD/nhhvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnX2MHNWZr58XDOHD2RkjJ/ESW4wTFIJCZOMQhBITD4Hs\nEpTEIOVjI927dlYKXGl1l49dYlZXVxhppYB2JZv7x0pkEzAo2SAgawg3urmBDUNgI0MMHgLhKzae\nXBsYwMHjXUK8wuG9f3TN0NNVdU51VddMTffvkVrT3W9X9elnzjldfc5bp8zdEUII0RyOmu8CCCGE\nmI06ZiGEaBjqmIUQomGoYxZCiIahjlkIIRqGOmYhhGgY0Y7ZzC40s2fN7NdmtmkuCrUQkJc0cpJG\nTrKRlwjunnsDjgZ2AyPAMcA4cHrHa3xQbvIiJ914kRPVlaJOOm+xI+azgd3uPuHubwG3A+s7X9S+\nw2uvvTa3o++M3XHHHbNuX/ziF2fur1y5ctZteHh45v6ll14667ZmzZpZj19//fWZ2ze+8Y1Zj4uW\ntTNexUs37xOLrVu3buZ2yimnzHq8atWqmdv73ve+WY+3b98+c/vKV74y63HZ8sxlXQnFHnjggZnb\nhg0bZj0eGhqadXvXu94163HI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], "source": [ "for index, image in enumerate(digits.images[:10]):\n", " plt.subplot(2, 5, index + 1)\n", " plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us create a training set using the first 1000 images and a test set using the rest of the data." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 0. 5. 13. 9. 1. 0. 0. 0. 0. 13. 15. 10. 15. 5.\n", " 0. 0. 3. 15. 2. 0. 11. 8. 0. 0. 4. 12. 0. 0. 8.\n", " 8. 0. 0. 5. 8. 0. 0. 9. 8. 0. 0. 4. 11. 0. 1.\n", " 12. 7. 0. 0. 2. 14. 5. 10. 12. 0. 0. 0. 0. 6. 13.\n", " 10. 0. 0. 0.]\n" ] } ], "source": [ "X,y = digits.data[0:1000], digits.target[0:1000]\n", "X_test, y_test = digits.data[1000:], digits.target[1000:]\n", "print(X[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "scikit-learn provide us with an One-Vs-Rest classifier that we already imported with name `OvR`. Let us use that classifier to fit the training set and to make predictions over the test set:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "binaryLearner = LinearSVC(random_state=0)\n", "\n", "oneVrestLearningAlgorithm = OvR(binaryLearner)\n", "oneVrestLearningAlgorithm.fit(X,y)\n", "predicted_labels = oneVrestLearningAlgorithm.predict(X_test)\n", "\n", "# n.b.: the above is equivalent to:\n", "# predicted_labels = OvR(LinearSVC(random_state=0)).fit(X,y).predict(X_test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print \"Accuracy: %s\" % (1.0 - np.count_nonzero(y_test - predicted_labels) / float(len(predicted_labels)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reimplement the OvR classifier by completing the methods in the following class [[1](#hint1)]:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class OneVsRestClassifier:\n", " def __init__(self, learner):\n", " #... to be done ...\n", " return 1\n", "\n", " def fit(self, data, labels):\n", " #... to be done ...\n", "\n", " return self\n", "\n", " def predict(self, data):\n", " #... to be done ...\n", " return 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the accuracy of your solution using the following code [[2](#hint2)]:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ovr = OneVsRestClassifier(LinearSVC(random_state=0))\n", "predicted_labels = ovr.fit(X,y).predict(X_test)\n", "print \"Accuracy: %s\" % (1.0-np.count_nonzero(predicted_labels-y_test)/float(len(y_test)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hint 1: Feel free to organize your code as you like (add as many methods as you believe are necessary).\n", "\n", "Hint 2: The scheme provided by scikit-learn is a little different from the one we have seen in the lessons. It is normal if your accuracy is not as good as the one obtained above (expect the accuracy to be between 0.8 and 0.9)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 1 }