{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Plot classification probability\n\n\nPlot the classification probability for different classifiers. We use a 3\nclass dataset, and we classify it with a Support Vector classifier, L1\nand L2 penalized logistic regression with either a One-Vs-Rest or multinomial\nsetting, and Gaussian process classification.\n\nThe logistic regression is not a multiclass classifier out of the box. As\na result it can identify only the first class.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(__doc__)\n\n# Author: Alexandre Gramfort \n# License: BSD 3 clause\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.gaussian_process import GaussianProcessClassifier\nfrom sklearn.gaussian_process.kernels import RBF\nfrom sklearn import datasets\n\niris = datasets.load_iris()\nX = iris.data[:, 0:2] # we only take the first two features for visualization\ny = iris.target\n\nn_features = X.shape[1]\n\nC = 1.0\nkernel = 1.0 * RBF([1.0, 1.0]) # for GPC\n\n# Create different classifiers. The logistic regression cannot do\n# multiclass out of the box.\nclassifiers = {'L1 logistic': LogisticRegression(C=C, penalty='l1'),\n 'L2 logistic (OvR)': LogisticRegression(C=C, penalty='l2'),\n 'Linear SVC': SVC(kernel='linear', C=C, probability=True,\n random_state=0),\n 'L2 logistic (Multinomial)': LogisticRegression(\n C=C, solver='lbfgs', multi_class='multinomial'),\n 'GPC': GaussianProcessClassifier(kernel)\n }\n\nn_classifiers = len(classifiers)\n\nplt.figure(figsize=(3 * 2, n_classifiers * 2))\nplt.subplots_adjust(bottom=.2, top=.95)\n\nxx = np.linspace(3, 9, 100)\nyy = np.linspace(1, 5, 100).T\nxx, yy = np.meshgrid(xx, yy)\nXfull = np.c_[xx.ravel(), yy.ravel()]\n\nfor index, (name, classifier) in enumerate(classifiers.items()):\n classifier.fit(X, y)\n\n y_pred = classifier.predict(X)\n classif_rate = np.mean(y_pred.ravel() == y.ravel()) * 100\n print(\"classif_rate for %s : %f \" % (name, classif_rate))\n\n # View probabilities=\n probas = classifier.predict_proba(Xfull)\n n_classes = np.unique(y_pred).size\n for k in range(n_classes):\n plt.subplot(n_classifiers, n_classes, index * n_classes + k + 1)\n plt.title(\"Class %d\" % k)\n if k == 0:\n plt.ylabel(name)\n imshow_handle = plt.imshow(probas[:, k].reshape((100, 100)),\n extent=(3, 9, 1, 5), origin='lower')\n plt.xticks(())\n plt.yticks(())\n idx = (y_pred == k)\n if idx.any():\n plt.scatter(X[idx, 0], X[idx, 1], marker='o', c='k')\n\nax = plt.axes([0.15, 0.04, 0.7, 0.05])\nplt.title(\"Probability\")\nplt.colorbar(imshow_handle, cax=ax, orientation='horizontal')\n\nplt.show()" ] } ], "metadata": { "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.6.6" } }, "nbformat": 4, "nbformat_minor": 0 }