{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Le but de ce TP est de mettre en oeuvre le classifieur Bayesien Naif sur les données MNIST. \n", "Pour se familiariser avec ces données voir [cette page](https://perso.limsi.fr/Individu/allauzen/webpages/pmwiki.php?n=Cours.Main#toc6). Elles sont également accessible en lecture et en local: \n", "\n", " /partage/public/allauzen\n", " \n", "Ce corpus rassemble des images (observations) de chiffres isolés et manuscrits ainsi que les classes associées (les labels ou les réponses attendues). Il s'agit donc d'un problème de classification à 10 classes (de 0 à 9). L'objectif est d'assigner le bon chiffre à une image. \n", "\n", "# Chargement des données" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50000 training examples\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import cPickle, gzip, numpy\n", "# Load the dataset\n", "f = gzip.open('/Users/allauzen/data/mnist/mnist.pkl.gz', 'rb')\n", "train_set, valid_set, test_set = cPickle.load(f)\n", "\n", "print str(len(train_set[0]))+\" training examples\"\n", "f.close()\n", "# exemple: \n", "im = train_set[0][0] # the first image\n", "label = train_set[1][0] # its label\n", "\n", "%matplotlib inline\n", "%config InlineBackend.figure_formats=['svg']\n", "%config InlineBackend.figure_format = 'svg'\n", "import matplotlib\n", "\n", "im = train_set[0][0]\n", "matplotlib.pyplot.show()\n", "matplotlib.pyplot.imshow(im.reshape(28,28) , matplotlib.pyplot.cm.gray)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Manipulation des données\n", "Voici quelques exemples de manipulation. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "shape d'images :(50000, 784)\n", "shape de labels :(50000,)\n", "Nombre d'images dont le label est 5 :4506\n", "fives contient toutes les images dont le label est 5, il y en a :(4506, 784)\n" ] } ], "source": [ "# Récupération des images et des labels\n", "images = train_set[0]\n", "labels = train_set[1]\n", "print \"shape d'images :\"+str(images.shape)\n", "print \"shape de labels :\"+str(labels.shape)\n", "# Combien y-a-t-il d'images de 5 \n", "print \"Nombre d'images dont le label est 5 :\"+str((labels==5).sum())\n", "# labels==5 retourne un ndarray de même dimension que labels mais remplit de boolean.\n", "# True, si le label de la case est 5 et False sinon. \n", "# On peut se servir de cet ndarray pour faire du slicing de images\n", "fives=images[labels==5]\n", "print \"fives contient toutes les images dont le label est 5, il y en a :\"+str(fives.shape)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Bayesien Naif (continu)\n", "Les observations sont de nature continue. Chaque image contient 784 pixels dont la valeur est réelle et normalisée entre 0 et 1. On pourrait binariser ce type de données, mais ici nous allons les modéliser ainsi en utilisant des gaussiennes.\n", "\n", "Une image est un vecteur de caractéristiques contenant 784 composantes. Partons de cette simple représentation. L'hypothèse naïve implique que chaque composante est indépendante des autres. Dans le cas présent, chaque pixel est donc modélisé par une gaussienne. La matrice de covariance est donc diagonale. \n", "\n", "## TODO\n", "Implémenter le Bayesien naïf gaussien: \n", "* Calcul des paramètres à partir des données d'apprentissage\n", " * Pour chaque classe calculer le vecteur des moyennes\n", " * La matrice de covariance (Attention elle est diagonale, on peut avoir qu'un vecteur)\n", "* Inférence sur les données de test\n", "* Calcul du taux d'erreur\n", "* Tracer les images \"moyennes\" pour chaque classe\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }