8000 DOC Use notebook style for remaining notebooks from notebook-style meta-issue by lesteve · Pull Request #23365 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

DOC Use notebook style for remaining notebooks from notebook-style meta-issue #23365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions examples/cross_decomposition/plot_compare_cross_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
20 changes: 14 additions & 6 deletions examples/decomposition/plot_ica_blind_source_separation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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()

Expand Down
19 changes: 15 additions & 4 deletions examples/linear_model/plot_multi_task_lasso_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
# 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)

Expand All @@ -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_)
Expand Down
0