|
4 | 4 | ===================================
|
5 | 5 |
|
6 | 6 | Simple usage of various cross decomposition algorithms:
|
| 7 | +
|
7 | 8 | - PLSCanonical
|
8 | 9 | - PLSRegression, with multivariate response, a.k.a. PLS2
|
9 | 10 | - PLSRegression, with univariate response, a.k.a. PLS1
|
|
20 | 21 |
|
21 | 22 | """
|
22 | 23 |
|
23 |
| -import numpy as np |
24 |
| -import matplotlib.pyplot as plt |
25 |
| -from sklearn.cross_decomposition import PLSCanonical, PLSRegression, CCA |
26 |
| - |
27 |
| -# ############################################################################# |
| 24 | +# %% |
28 | 25 | # Dataset based latent variables model
|
| 26 | +# ------------------------------------ |
| 27 | + |
| 28 | +import numpy as np |
29 | 29 |
|
30 | 30 | n = 500
|
31 | 31 | # 2 latents vars:
|
|
46 | 46 | print("Corr(Y)")
|
47 | 47 | print(np.round(np.corrcoef(Y.T), 2))
|
48 | 48 |
|
49 |
| -# ############################################################################# |
| 49 | +# %% |
50 | 50 | # Canonical (symmetric) PLS
|
51 |
| - |
| 51 | +# ------------------------- |
| 52 | +# |
52 | 53 | # Transform data
|
53 | 54 | # ~~~~~~~~~~~~~~
|
| 55 | + |
| 56 | +from sklearn.cross_decomposition import PLSCanonical |
| 57 | + |
54 | 58 | plsca = PLSCanonical(n_components=2)
|
55 | 59 | plsca.fit(X_train, Y_train)
|
56 | 60 | X_train_r, Y_train_r = plsca.transform(X_train, Y_train)
|
57 | 61 | X_test_r, Y_test_r = plsca.transform(X_test, Y_test)
|
58 | 62 |
|
| 63 | +# %% |
59 | 64 | # Scatter plot of scores
|
60 | 65 | # ~~~~~~~~~~~~~~~~~~~~~~
|
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 |
62 | 70 | plt.figure(figsize=(12, 8))
|
63 | 71 | plt.subplot(221)
|
64 | 72 | plt.scatter(X_train_r[:, 0], Y_train_r[:, 0], label="train", marker="o", s=25)
|
|
86 | 94 | plt.yticks(())
|
87 | 95 | plt.legend(loc="best")
|
88 | 96 |
|
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 |
90 | 98 | plt.subplot(222)
|
91 | 99 | plt.scatter(X_train_r[:, 0], X_train_r[:, 1], label="train", marker="*", s=50)
|
92 | 100 | plt.scatter(X_test_r[:, 0], X_test_r[:, 1], label="test", marker="*", s=50)
|
|
114 | 122 | plt.yticks(())
|
115 | 123 | plt.show()
|
116 | 124 |
|
117 |
| -# ############################################################################# |
| 125 | +# %% |
118 | 126 | # PLS regression, with multivariate response, a.k.a. PLS2
|
| 127 | +# ------------------------------------------------------- |
| 128 | + |
| 129 | +from sklearn.cross_decomposition import PLSRegression |
119 | 130 |
|
120 | 131 | n = 1000
|
121 | 132 | q = 3
|
|
134 | 145 | print(np.round(pls2.coef_, 1))
|
135 | 146 | pls2.predict(X)
|
136 | 147 |
|
| 148 | +# %% |
137 | 149 | # PLS regression, with univariate response, a.k.a. PLS1
|
| 150 | +# ----------------------------------------------------- |
138 | 151 |
|
139 | 152 | n = 1000
|
140 | 153 | p = 10
|
|
146 | 159 | print("Estimated betas")
|
147 | 160 | print(np.round(pls1.coef_, 1))
|
148 | 161 |
|
149 |
| -# ############################################################################# |
| 162 | +# %% |
150 | 163 | # CCA (PLS mode B with symmetric deflation)
|
| 164 | +# ----------------------------------------- |
| 165 | + |
| 166 | +from sklearn.cross_decomposition import CCA |
151 | 167 |
|
152 | 168 | cca = CCA(n_components=2)
|
153 | 169 | cca.fit(X_train, Y_train)
|
|
0 commit comments