8000 DOC Use notebook style for remaining notebooks (#23365) · scikit-learn/scikit-learn@9cb86c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9cb86c8

Browse files
authored
DOC Use notebook style for remaining notebooks (#23365)
1 parent 84f8409 commit 9cb86c8

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

examples/cross_decomposition/plot_compare_cross_decomposition.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
===================================
55
66
Simple usage of various cross decomposition algorithms:
7+
78
- PLSCanonical
89
- PLSRegression, with multivariate response, a.k.a. PLS2
910
- PLSRegression, with univariate response, a.k.a. PLS1
@@ -20,12 +21,11 @@
2021
2122
"""
2223

23-
import numpy as np
24-
import matplotlib.pyplot as plt
25-
from sklearn.cross_decomposition import PLSCanonical, PLSRegression, CCA
26-
27-
# #############################################################################
24+
# %%
2825
# Dataset based latent variables model
26+
# ------------------------------------
27+
28+
import numpy as np
2929

3030
n = 500
3131
# 2 latents vars:
@@ -46,19 +46,27 @@
4646
print("Corr(Y)")
4747
print(np.round(np.corrcoef(Y.T), 2))
4848

49-
# #############################################################################
49+
# %%
5050
# Canonical (symmetric) PLS
51-
51+
# -------------------------
52+
#
5253
# Transform data
5354
# ~~~~~~~~~~~~~~
55+
56+
from sklearn.cross_decomposition import PLSCanonical
57+
5458
plsca = PLSCanonical(n_components=2)
5559
plsca.fit(X_train, Y_train)
5660
X_train_r, Y_train_r = plsca.transform(X_train, Y_train)
5761
X_test_r, Y_test_r = plsca.transform(X_test, Y_test)
5862

63+
# %%
5964
# Scatter plot of scores
6065
# ~~~~~~~~~~~~~~~~~~~~~~
61-
# 1) On diagonal plot X vs Y scores on each components
66+
67+
import matplotlib.pyplot as plt
68+
69+
# On diagonal plot X vs Y scores on each components
6270
plt.figure(figsize=(12, 8))
6371
plt.subplot(221)
6472
plt.scatter(X_train_r[:, 0], Y_train_r[:, 0], label="train", marker="o", s=25)
@@ -86,7 +94,7 @@
8694
plt.yticks(())
8795
plt.legend(loc="best")
8896

89-
# 2) Off diagonal plot components 1 vs 2 for X and Y
97+
# Off diagonal plot components 1 vs 2 for X and Y
9098
plt.subplot(222)
9199
plt.scatter(X_train_r[:, 0], X_train_r[:, 1], label="train", marker="*", s=50)
92100
plt.scatter(X_test_r[:, 0], X_test_r[:, 1], label="test", marker="*", s=50)
@@ -114,8 +122,11 @@
114122
plt.yticks(())
115123
plt.show()
116124

117-
# #############################################################################
125+
# %%
118126
# PLS regression, with multivariate response, a.k.a. PLS2
127+
# -------------------------------------------------------
128+
129+
from sklearn.cross_decomposition import PLSRegression
119130

120131
n = 1000
121132
q = 3
@@ -134,7 +145,9 @@
134145
print(np.round(pls2.coef_, 1))
135146
pls2.predict(X)
136147

148+
# %%
137149
# PLS regression, with univariate response, a.k.a. PLS1
150+
# -----------------------------------------------------
138151

139152
n = 1000
140153
p = 10
@@ -146,8 +159,11 @@
146159
print("Estimated betas")
147160
print(np.round(pls1.coef_, 1))
148161

149-
# #############################################################################
162+
# %%
150163
# CCA (PLS mode B with symmetric deflation)
164+
# -----------------------------------------
165+
166+
from sklearn.cross_decomposition import CCA
151167

152168
cca = CCA(n_components=2)
153169
cca.fit(X_train, Y_train)

examples/decomposition/plot_ica_blind_source_separation.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
1515
"""
1616

17+
# %%
18+
# Generate sample data
19+
# --------------------
20+
1721
import numpy as np
18-
import matplotlib.pyplot as plt
1922
from scipy import signal
2023

21-
from sklearn.decomposition import FastICA, PCA
22-
23-
# #############################################################################
24-
# Generate sample data
2524
np.random.seed(0)
2625
n_samples = 2000
2726
time = np.linspace(0, 8, n_samples)
@@ -38,6 +37,12 @@
3837
A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]]) # Mixing matrix
3938
X = np.dot(S, A.T) # Generate observations
4039

40+
# %%
41+
# Fit ICA and PCA models
42+
# ----------------------
43+
44+
from sklearn.decomposition import FastICA, PCA
45+
4146
# Compute ICA
4247
ica = FastICA(n_components=3)
4348
S_ = ica.fit_transform(X) # Reconstruct signals
@@ -50,8 +55,11 @@
5055
pca = PCA(n_components=3)
5156
H = pca.fit_transform(X) # Reconstruct signals based on orthogonal components
5257

53-
# #############################################################################
58+
# %%
5459
# Plot results
60+
# ------------
61+
62+
import matplotlib.pyplot as plt
5563

5664
plt.figure()
5765

examples/linear_model/plot_multi_task_lasso_support.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
1717
# License: BSD 3 clause
1818

19-
import matplotlib.pyplot as plt
20-
import numpy as np
19+
# %%
20+
# Generate data
21+
# -------------
2122

22-
from sklearn.linear_model import MultiTaskLasso, Lasso
23+
import numpy as np
2324

2425
rng = np.random.RandomState(42)
2526

@@ -34,11 +35,21 @@
3435
X = rng.randn(n_samples, n_features)
3536
Y = np.dot(X, coef.T) + rng.randn(n_samples, n_tasks)
3637

38+
# %%
39+
# Fit models
40+
# ----------
41+
42+
from sklearn.linear_model import MultiTaskLasso, Lasso
43+
3744
coef_lasso_ = np.array([Lasso(alpha=0.5).fit(X, y).coef_ for y in Y.T])
3845
coef_multi_task_lasso_ = MultiTaskLasso(alpha=1.0).fit(X, Y).coef_
3946

40-
# #############################################################################
47+
# %%
4148
# Plot support and time series
49+
# ----------------------------
50+
51+
import matplotlib.pyplot as plt
52+
4253
fig = plt.figure(figsize=(8, 5))
4354
plt.subplot(1, 2, 1)
4455
plt.spy(coef_lasso_)

0 commit comments

Comments
 (0)
0