-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPCovR_Regressors.py
143 lines (104 loc) · 3.44 KB
/
PCovR_Regressors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# coding: utf-8
"""
Choosing Different Regressors for PCovR
=======================================
"""
# %%
#
import time
from matplotlib import pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge
from sklearn.preprocessing import StandardScaler
from skmatter.decomposition import PCovR
# %%
#
# For this, we will use the :func:`sklearn.datasets.load_diabetes` dataset from
# ``sklearn``.
mixing = 0.5
X, y = load_diabetes(return_X_y=True)
y = y.reshape(X.shape[0], -1)
X_scaler = StandardScaler()
X_scaled = X_scaler.fit_transform(X)
y_scaler = StandardScaler()
y_scaled = y_scaler.fit_transform(y)
# %%
#
# Use the default regressor in PCovR
# ----------------------------------
#
# When there is no regressor supplied, PCovR uses
# ``sklearn.linear_model.Ridge('alpha':1e-6, 'fit_intercept':False, 'tol':1e-12)``.
pcovr1 = PCovR(mixing=mixing, n_components=2)
t0 = time.perf_counter()
pcovr1.fit(X_scaled, y_scaled)
t1 = time.perf_counter()
print(f"Regressor is {pcovr1.regressor_} and fit took {1e3 * (t1 - t0):0.2} ms.")
# %%
#
# Use a fitted regressor
# ----------------------
#
# You can pass a fitted regressor to ``PCovR`` to rely on the predetermined regression
# parameters. Currently, scikit-matter supports ``scikit-learn`` classes
# class:`LinearModel <sklearn.linear_model.LinearModel>`, :class:`Ridge
# <sklearn.linear_model.Ridge>`, and class:`RidgeCV <sklearn.linear_model.RidgeCV>`,
# with plans to support any regressor with similar architecture in the future.
regressor = Ridge(alpha=1e-6, fit_intercept=False, tol=1e-12)
t0 = time.perf_counter()
regressor.fit(X_scaled, y_scaled)
t1 = time.perf_counter()
print(f"Fit took {1e3 * (t1 - t0):0.2} ms.")
# %%
#
pcovr2 = PCovR(mixing=mixing, n_components=2, regressor=regressor)
t0 = time.perf_counter()
pcovr2.fit(X_scaled, y_scaled)
t1 = time.perf_counter()
print(f"Regressor is {pcovr2.regressor_} and fit took {1e3 * (t1 - t0):0.2} ms.")
# %%
#
# Use a pre-predicted y
# ---------------------
#
# With ``regressor='precomputed'``, you can pass a regression output :math:`\hat{Y}` and
# optional regression weights :math:`W` to PCovR. If ``W=None``, then PCovR will
# determine :math:`W` as the least-squares solution between :math:`X` and
# :math:`\hat{Y}`.
regressor = Ridge(alpha=1e-6, fit_intercept=False, tol=1e-12)
t0 = time.perf_counter()
regressor.fit(X_scaled, y_scaled)
t1 = time.perf_counter()
print(f"Fit took {1e3 * (t1 - t0):0.2} ms.")
W = regressor.coef_
# %%
#
pcovr3 = PCovR(mixing=mixing, n_components=2, regressor="precomputed")
t0 = time.perf_counter()
pcovr3.fit(X_scaled, y_scaled, W=W)
t1 = time.perf_counter()
print(f"Fit took {1e3 * (t1 - t0):0.2} ms.")
# %%
#
# Comparing Results
# -----------------
#
# Because we used the same regressor in all three models, they will yield the same
# result.
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(12, 4), sharex=True, sharey=True)
ax1.scatter(*pcovr1.transform(X_scaled).T, c=y)
ax2.scatter(*pcovr2.transform(X_scaled).T, c=y)
ax3.scatter(*pcovr3.transform(X_scaled).T, c=y)
ax1.set_ylabel("PCov$_2$")
ax1.set_xlabel("PCov$_1$")
ax2.set_xlabel("PCov$_1$")
ax3.set_xlabel("PCov$_1$")
ax1.set_title("Default Regressor")
ax2.set_title("Pre-fit Regressor")
ax3.set_title("Precomputed Regression Result")
fig.show()
# %%
#
# As you can imagine, these three options have different use cases -- if you
# are working with a large dataset, you should always pre-fit to save on time!