From 233c8f90304a4973ccf37245a76b9059e51ed5ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Fri, 13 May 2022 17:00:35 +0200 Subject: [PATCH] Use notebook style for remaining notebooks --- .../plot_compare_cross_decomposition.py | 38 +++++++++++++------ .../plot_ica_blind_source_separation.py | 20 +++++++--- .../plot_multi_task_lasso_support.py | 19 ++++++++-- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/examples/cross_decomposition/plot_compare_cross_decomposition.py b/examples/cross_decomposition/plot_compare_cross_decomposition.py index deccd7aa1932c..762c42dfdf31c 100644 --- a/examples/cross_decomposition/plot_compare_cross_decomposition.py +++ b/examples/cross_decomposition/plot_compare_cross_decomposition.py @@ -4,6 +4,7 @@ =================================== Simple usage of various cross decomposition algorithms: + - PLSCanonical - PLSRegression, with multivariate response, a.k.a. PLS2 - PLSRegression, with univariate response, a.k.a. PLS1 @@ -20,12 +21,11 @@ """ -import numpy as np -import matplotlib.pyplot as plt -from sklearn.cross_decomposition import PLSCanonical, PLSRegression, CCA - -# ############################################################################# +# %% # Dataset based latent variables model +# ------------------------------------ + +import numpy as np n = 500 # 2 latents vars: @@ -46,19 +46,27 @@ print("Corr(Y)") print(np.round(np.corrcoef(Y.T), 2)) -# ############################################################################# +# %% # Canonical (symmetric) PLS - +# ------------------------- +# # Transform data # ~~~~~~~~~~~~~~ + +from sklearn.cross_decomposition import PLSCanonical + plsca = PLSCanonical(n_components=2) plsca.fit(X_train, Y_train) X_train_r, Y_train_r = plsca.transform(X_train, Y_train) X_test_r, Y_test_r = plsca.transform(X_test, Y_test) +# %% # Scatter plot of scores # ~~~~~~~~~~~~~~~~~~~~~~ -# 1) On diagonal plot X vs Y scores on each components + +import matplotlib.pyplot as plt + +# On diagonal plot X vs Y scores on each components plt.figure(figsize=(12, 8)) plt.subplot(221) plt.scatter(X_train_r[:, 0], Y_train_r[:, 0], label="train", marker="o", s=25) @@ -86,7 +94,7 @@ plt.yticks(()) plt.legend(loc="best") -# 2) Off diagonal plot components 1 vs 2 for X and Y +# Off diagonal plot components 1 vs 2 for X and Y plt.subplot(222) plt.scatter(X_train_r[:, 0], X_train_r[:, 1], label="train", marker="*", s=50) plt.scatter(X_test_r[:, 0], X_test_r[:, 1], label="test", marker="*", s=50) @@ -114,8 +122,11 @@ plt.yticks(()) plt.show() -# ############################################################################# +# %% # PLS regression, with multivariate response, a.k.a. PLS2 +# ------------------------------------------------------- + +from sklearn.cross_decomposition import PLSRegression n = 1000 q = 3 @@ -134,7 +145,9 @@ print(np.round(pls2.coef_, 1)) pls2.predict(X) +# %% # PLS regression, with univariate response, a.k.a. PLS1 +# ----------------------------------------------------- n = 1000 p = 10 @@ -146,8 +159,11 @@ print("Estimated betas") print(np.round(pls1.coef_, 1)) -# ############################################################################# +# %% # CCA (PLS mode B with symmetric deflation) +# ----------------------------------------- + +from sklearn.cross_decomposition import CCA cca = CCA(n_components=2) cca.fit(X_train, Y_train) diff --git a/examples/decomposition/plot_ica_blind_source_separation.py b/examples/decomposition/plot_ica_blind_source_separation.py index 15945e5075ce8..27a2d7cfe105b 100644 --- a/examples/decomposition/plot_ica_blind_source_separation.py +++ b/examples/decomposition/plot_ica_blind_source_separation.py @@ -14,14 +14,13 @@ """ +# %% +# Generate sample data +# -------------------- + import numpy as np -import matplotlib.pyplot as plt from scipy import signal -from sklearn.decomposition import FastICA, PCA - -# ############################################################################# -# Generate sample data np.random.seed(0) n_samples = 2000 time = np.linspace(0, 8, n_samples) @@ -38,6 +37,12 @@ A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]]) # Mixing matrix X = np.dot(S, A.T) # Generate observations +# %% +# Fit ICA and PCA models +# ---------------------- + +from sklearn.decomposition import FastICA, PCA + # Compute ICA ica = FastICA(n_components=3) S_ = ica.fit_transform(X) # Reconstruct signals @@ -50,8 +55,11 @@ pca = PCA(n_components=3) H = pca.fit_transform(X) # Reconstruct signals based on orthogonal components -# ############################################################################# +# %% # Plot results +# ------------ + +import matplotlib.pyplot as plt plt.figure() diff --git a/examples/linear_model/plot_multi_task_lasso_support.py b/examples/linear_model/plot_multi_task_lasso_support.py index b53c78b986acd..a30b51ed7a7fe 100644 --- a/examples/linear_model/plot_multi_task_lasso_support.py +++ b/examples/linear_model/plot_multi_task_lasso_support.py @@ -16,10 +16,11 @@ # Author: Alexandre Gramfort # License: BSD 3 clause -import matplotlib.pyplot as plt -import numpy as np +# %% +# Generate data +# ------------- -from sklearn.linear_model import MultiTaskLasso, Lasso +import numpy as np rng = np.random.RandomState(42) @@ -34,11 +35,21 @@ X = rng.randn(n_samples, n_features) Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks) +# %% +# Fit models +# ---------- + +from sklearn.linear_model import MultiTaskLasso, Lasso + coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T]) coef_multi_task_lasso_ = MultiTaskLasso(alpha=1.0).fit(X, Y).coef_ -# ############################################################################# +# %% # Plot support and time series +# ---------------------------- + +import matplotlib.pyplot as plt + fig = plt.figure(figsize=(8, 5)) plt.subplot(1, 2, 1) plt.spy(coef_lasso_)