{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Linear and Quadratic Discriminant Analysis with covariance ellipsoid\n\n\nThis example plots the covariance ellipsoids of each class and\ndecision boundary learned by LDA and QDA. The ellipsoids display\nthe double standard deviation for each class. With LDA, the\nstandard deviation is the same for all the classes, while each\nclass has its own standard deviation with QDA.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nfrom scipy import linalg\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nfrom matplotlib import colors\n\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis\nfrom sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis\n\n# #############################################################################\n# Colormap\ncmap = colors.LinearSegmentedColormap(\n    'red_blue_classes',\n    {'red': [(0, 1, 1), (1, 0.7, 0.7)],\n     'green': [(0, 0.7, 0.7), (1, 0.7, 0.7)],\n     'blue': [(0, 0.7, 0.7), (1, 1, 1)]})\nplt.cm.register_cmap(cmap=cmap)\n\n\n# #############################################################################\n# Generate datasets\ndef dataset_fixed_cov():\n    '''Generate 2 Gaussians samples with the same covariance matrix'''\n    n, dim = 300, 2\n    np.random.seed(0)\n    C = np.array([[0., -0.23], [0.83, .23]])\n    X = np.r_[np.dot(np.random.randn(n, dim), C),\n              np.dot(np.random.randn(n, dim), C) + np.array([1, 1])]\n    y = np.hstack((np.zeros(n), np.ones(n)))\n    return X, y\n\n\ndef dataset_cov():\n    '''Generate 2 Gaussians samples with different covariance matrices'''\n    n, dim = 300, 2\n    np.random.seed(0)\n    C = np.array([[0., -1.], [2.5, .7]]) * 2.\n    X = np.r_[np.dot(np.random.randn(n, dim), C),\n              np.dot(np.random.randn(n, dim), C.T) + np.array([1, 4])]\n    y = np.hstack((np.zeros(n), np.ones(n)))\n    return X, y\n\n\n# #############################################################################\n# Plot functions\ndef plot_data(lda, X, y, y_pred, fig_index):\n    splot = plt.subplot(2, 2, fig_index)\n    if fig_index == 1:\n        plt.title('Linear Discriminant Analysis')\n        plt.ylabel('Data with\\n fixed covariance')\n    elif fig_index == 2:\n        plt.title('Quadratic Discriminant Analysis')\n    elif fig_index == 3:\n        plt.ylabel('Data with\\n varying covariances')\n\n    tp = (y == y_pred)  # True Positive\n    tp0, tp1 = tp[y == 0], tp[y == 1]\n    X0, X1 = X[y == 0], X[y == 1]\n    X0_tp, X0_fp = X0[tp0], X0[~tp0]\n    X1_tp, X1_fp = X1[tp1], X1[~tp1]\n\n    alpha = 0.5\n\n    # class 0: dots\n    plt.plot(X0_tp[:, 0], X0_tp[:, 1], 'o', alpha=alpha,\n             color='red', markeredgecolor='k')\n    plt.plot(X0_fp[:, 0], X0_fp[:, 1], '*', alpha=alpha,\n             color='#990000', markeredgecolor='k')  # dark red\n\n    # class 1: dots\n    plt.plot(X1_tp[:, 0], X1_tp[:, 1], 'o', alpha=alpha,\n             color='blue', markeredgecolor='k')\n    plt.plot(X1_fp[:, 0], X1_fp[:, 1], '*', alpha=alpha,\n             color='#000099', markeredgecolor='k')  # dark blue\n\n    # class 0 and 1 : areas\n    nx, ny = 200, 100\n    x_min, x_max = plt.xlim()\n    y_min, y_max = plt.ylim()\n    xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx),\n                         np.linspace(y_min, y_max, ny))\n    Z = lda.predict_proba(np.c_[xx.ravel(), yy.ravel()])\n    Z = Z[:, 1].reshape(xx.shape)\n    plt.pcolormesh(xx, yy, Z, cmap='red_blue_classes',\n                   norm=colors.Normalize(0., 1.))\n    plt.contour(xx, yy, Z, [0.5], linewidths=2., colors='k')\n\n    # means\n    plt.plot(lda.means_[0][0], lda.means_[0][1],\n             'o', color='black', markersize=10, markeredgecolor='k')\n    plt.plot(lda.means_[1][0], lda.means_[1][1],\n             'o', color='black', markersize=10, markeredgecolor='k')\n\n    return splot\n\n\ndef plot_ellipse(splot, mean, cov, color):\n    v, w = linalg.eigh(cov)\n    u = w[0] / linalg.norm(w[0])\n    angle = np.arctan(u[1] / u[0])\n    angle = 180 * angle / np.pi  # convert to degrees\n    # filled Gaussian at 2 standard deviation\n    ell = mpl.patches.Ellipse(mean, 2 * v[0] ** 0.5, 2 * v[1] ** 0.5,\n                              180 + angle, facecolor=color,\n                              edgecolor='yellow',\n                              linewidth=2, zorder=2)\n    ell.set_clip_box(splot.bbox)\n    ell.set_alpha(0.5)\n    splot.add_artist(ell)\n    splot.set_xticks(())\n    splot.set_yticks(())\n\n\ndef plot_lda_cov(lda, splot):\n    plot_ellipse(splot, lda.means_[0], lda.covariance_, 'red')\n    plot_ellipse(splot, lda.means_[1], lda.covariance_, 'blue')\n\n\ndef plot_qda_cov(qda, splot):\n    plot_ellipse(splot, qda.means_[0], qda.covariance_[0], 'red')\n    plot_ellipse(splot, qda.means_[1], qda.covariance_[1], 'blue')\n\nfor i, (X, y) in enumerate([dataset_fixed_cov(), dataset_cov()]):\n    # Linear Discriminant Analysis\n    lda = LinearDiscriminantAnalysis(solver=\"svd\", store_covariance=True)\n    y_pred = lda.fit(X, y).predict(X)\n    splot = plot_data(lda, X, y, y_pred, fig_index=2 * i + 1)\n    plot_lda_cov(lda, splot)\n    plt.axis('tight')\n\n    # Quadratic Discriminant Analysis\n    qda = QuadraticDiscriminantAnalysis(store_covariance=True)\n    y_pred = qda.fit(X, y).predict(X)\n    splot = plot_data(qda, X, y, y_pred, fig_index=2 * i + 2)\n    plot_qda_cov(qda, splot)\n    plt.axis('tight')\nplt.suptitle('Linear Discriminant Analysis vs Quadratic Discriminant'\n             'Analysis')\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.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}