{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# FastICA on 2D point clouds\n\n\nThis example illustrates visually in the feature space a comparison by\nresults using two different component analysis techniques.\n\n`ICA` vs `PCA`.\n\nRepresenting ICA in the feature space gives the view of 'geometric ICA':\nICA is an algorithm that finds directions in the feature space\ncorresponding to projections with high non-Gaussianity. These directions\nneed not be orthogonal in the original feature space, but they are\northogonal in the whitened feature space, in which all directions\ncorrespond to the same variance.\n\nPCA, on the other hand, finds orthogonal directions in the raw feature\nspace that correspond to directions accounting for maximum variance.\n\nHere we simulate independent sources using a highly non-Gaussian\nprocess, 2 student T with a low number of degrees of freedom (top left\nfigure). We mix them to create observations (top right figure).\nIn this raw observation space, directions identified by PCA are\nrepresented by orange vectors. We represent the signal in the PCA space,\nafter whitening by the variance corresponding to the PCA vectors (lower\nleft). Running ICA corresponds to finding a rotation in this space to\nidentify the directions of largest non-Gaussianity (lower right).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Authors: Alexandre Gramfort, Gael Varoquaux\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.decomposition import PCA, FastICA\n\n# #############################################################################\n# Generate sample data\nrng = np.random.RandomState(42)\nS = rng.standard_t(1.5, size=(20000, 2))\nS[:, 0] *= 2.\n\n# Mix data\nA = np.array([[1, 1], [0, 2]])  # Mixing matrix\n\nX = np.dot(S, A.T)  # Generate observations\n\npca = PCA()\nS_pca_ = pca.fit(X).transform(X)\n\nica = FastICA(random_state=rng)\nS_ica_ = ica.fit(X).transform(X)  # Estimate the sources\n\nS_ica_ /= S_ica_.std(axis=0)\n\n\n# #############################################################################\n# Plot results\n\ndef plot_samples(S, axis_list=None):\n    plt.scatter(S[:, 0], S[:, 1], s=2, marker='o', zorder=10,\n                color='steelblue', alpha=0.5)\n    if axis_list is not None:\n        colors = ['orange', 'red']\n        for color, axis in zip(colors, axis_list):\n            axis /= axis.std()\n            x_axis, y_axis = axis\n            # Trick to get legend to work\n            plt.plot(0.1 * x_axis, 0.1 * y_axis, linewidth=2, color=color)\n            plt.quiver(0, 0, x_axis, y_axis, zorder=11, width=0.01, scale=6,\n                       color=color)\n\n    plt.hlines(0, -3, 3)\n    plt.vlines(0, -3, 3)\n    plt.xlim(-3, 3)\n    plt.ylim(-3, 3)\n    plt.xlabel('x')\n    plt.ylabel('y')\n\nplt.figure()\nplt.subplot(2, 2, 1)\nplot_samples(S / S.std())\nplt.title('True Independent Sources')\n\naxis_list = [pca.components_.T, ica.mixing_]\nplt.subplot(2, 2, 2)\nplot_samples(X / np.std(X), axis_list=axis_list)\nlegend = plt.legend(['PCA', 'ICA'], loc='upper right')\nlegend.set_zorder(100)\n\nplt.title('Observations')\n\nplt.subplot(2, 2, 3)\nplot_samples(S_pca_ / np.std(S_pca_, axis=0))\nplt.title('PCA recovered signals')\n\nplt.subplot(2, 2, 4)\nplot_samples(S_ica_ / np.std(S_ica_))\nplt.title('ICA recovered signals')\n\nplt.subplots_adjust(0.09, 0.04, 0.94, 0.94, 0.26, 0.36)\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
}