From 0d02df3745ca99a7b0e459acbef6d4ceb4e19f60 Mon Sep 17 00:00:00 2001
From: GILSON Matthieu <matthieu.gilson@univ-amu.fr>
Date: Thu, 11 May 2023 12:52:53 +0000
Subject: [PATCH] Upload New File

---
 ...kitlearn_ica_blind_source_separation.ipynb | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 nb_unsupervised_learning/scikitlearn_ica_blind_source_separation.ipynb

diff --git a/nb_unsupervised_learning/scikitlearn_ica_blind_source_separation.ipynb b/nb_unsupervised_learning/scikitlearn_ica_blind_source_separation.ipynb
new file mode 100644
index 0000000..ca16a4e
--- /dev/null
+++ b/nb_unsupervised_learning/scikitlearn_ica_blind_source_separation.ipynb
@@ -0,0 +1,97 @@
+{
+  "cells": [
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "collapsed": false
+      },
+      "outputs": [],
+      "source": [
+        "%matplotlib inline"
+      ]
+    },
+    {
+      "cell_type": "markdown",
+      "metadata": {},
+      "source": [
+        "\n# Blind source separation using FastICA\n\nAn example of estimating sources from noisy data.\n\n`ICA` is used to estimate sources given noisy measurements.\nImagine 3 instruments playing simultaneously and 3 microphones\nrecording the mixed signals. ICA is used to recover the sources\nie. what is played by each instrument. Importantly, PCA fails\nat recovering our `instruments` since the related signals reflect\nnon-Gaussian processes.\n"
+      ]
+    },
+    {
+      "cell_type": "markdown",
+      "metadata": {},
+      "source": [
+        "## Generate sample data\n\n"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "collapsed": false
+      },
+      "outputs": [],
+      "source": [
+        "import numpy as np\nfrom scipy import signal\n\nnp.random.seed(0)\nn_samples = 2000\ntime = np.linspace(0, 8, n_samples)\n\ns1 = np.sin(2 * time)  # Signal 1 : sinusoidal signal\ns2 = np.sign(np.sin(3 * time))  # Signal 2 : square signal\ns3 = signal.sawtooth(2 * np.pi * time)  # Signal 3: saw tooth signal\n\nS = np.c_[s1, s2, s3]\nS += 0.2 * np.random.normal(size=S.shape)  # Add noise\n\nS /= S.std(axis=0)  # Standardize data\n# Mix data\nA = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]])  # Mixing matrix\nX = np.dot(S, A.T)  # Generate observations"
+      ]
+    },
+    {
+      "cell_type": "markdown",
+      "metadata": {},
+      "source": [
+        "## Fit ICA and PCA models\n\n"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "collapsed": false
+      },
+      "outputs": [],
+      "source": [
+        "from sklearn.decomposition import FastICA, PCA\n\n# Compute ICA\nica = FastICA(n_components=3, whiten=\"arbitrary-variance\")\nS_ = ica.fit_transform(X)  # Reconstruct signals\nA_ = ica.mixing_  # Get estimated mixing matrix\n\n# We can `prove` that the ICA model applies by reverting the unmixing.\nassert np.allclose(X, np.dot(S_, A_.T) + ica.mean_)\n\n# For comparison, compute PCA\npca = PCA(n_components=3)\nH = pca.fit_transform(X)  # Reconstruct signals based on orthogonal components"
+      ]
+    },
+    {
+      "cell_type": "markdown",
+      "metadata": {},
+      "source": [
+        "## Plot results\n\n"
+      ]
+    },
+    {
+      "cell_type": "code",
+      "execution_count": null,
+      "metadata": {
+        "collapsed": false
+      },
+      "outputs": [],
+      "source": [
+        "import matplotlib.pyplot as plt\n\nplt.figure()\n\nmodels = [X, S, S_, H]\nnames = [\n    \"Observations (mixed signal)\",\n    \"True Sources\",\n    \"ICA recovered signals\",\n    \"PCA recovered signals\",\n]\ncolors = [\"red\", \"steelblue\", \"orange\"]\n\nfor ii, (model, name) in enumerate(zip(models, names), 1):\n    plt.subplot(4, 1, ii)\n    plt.title(name)\n    for sig, color in zip(model.T, colors):\n        plt.plot(sig, color=color)\n\nplt.tight_layout()\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.9.15"
+    }
+  },
+  "nbformat": 4,
+  "nbformat_minor": 0
+}
\ No newline at end of file
-- 
GitLab