-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathestimators.py
241 lines (202 loc) · 7.51 KB
/
estimators.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
Models following scikit-learn's estimator API.
"""
import warnings
from sklearn.base import BaseEstimator
from . import algorithms
from . import families
from .utils import (
sigmoid, dot, add_intercept, mean_squared_error, accuracy_score, exp,
poisson_deviance
)
msg = ("The 'dask_glm.estimators' module is deprecated in favor of "
"'dask_ml.linear_models'. Please install 'dask-ml' and update "
"your imports.")
warnings.warn(msg, FutureWarning)
class _GLM(BaseEstimator):
""" Base estimator for Generalized Linear Models
You should not use this class directly, you should use one of its subclasses
instead.
This class should be subclassed and paired with a GLM Family object like
Logistic, Linear, Poisson, etc. to form an estimator.
See Also
--------
LinearRegression
LogisticRegression
PoissonRegression
"""
@property
def family(self):
""" The family for which this is the estimator """
def __init__(self, fit_intercept=True, solver='admm', regularizer='l2',
max_iter=100, tol=1e-4, lamduh=1.0, rho=1,
over_relax=1, abstol=1e-4, reltol=1e-2):
self.fit_intercept = fit_intercept
self.solver = solver
self.regularizer = regularizer
self.max_iter = max_iter
self.tol = tol
self.lamduh = lamduh
self.rho = rho
self.over_relax = over_relax
self.abstol = abstol
self.reltol = reltol
self.coef_ = None
self.intercept_ = None
self._coef = None # coef, maybe with intercept
fit_kwargs = {'max_iter', 'tol', 'family'}
if solver == 'admm':
fit_kwargs.discard('tol')
fit_kwargs.update({
'regularizer', 'lamduh', 'rho', 'over_relax', 'abstol',
'reltol'
})
elif solver == 'proximal_grad' or solver == 'lbfgs':
fit_kwargs.update({'regularizer', 'lamduh'})
self._fit_kwargs = {k: getattr(self, k) for k in fit_kwargs}
def fit(self, X, y=None):
X_ = self._maybe_add_intercept(X)
self._coef = algorithms._solvers[self.solver](X_, y, **self._fit_kwargs)
if self.fit_intercept:
self.coef_ = self._coef[:-1]
self.intercept_ = self._coef[-1]
else:
self.coef_ = self._coef
return self
def _maybe_add_intercept(self, X):
if self.fit_intercept:
return add_intercept(X)
else:
return X
class LogisticRegression(_GLM):
"""
Estimator for logistic regression.
Parameters
----------
fit_intercept : bool, default True
Specifies if a constant (a.k.a. bias or intercept) should be
added to the decision function.
solver : {'admm', 'gradient_descent', 'newton', 'lbfgs', 'proximal_grad'}
Solver to use. See :ref:`api.algorithms` for details
regularizer : {'l1', 'l2'}
Regularizer to use. See :ref:`api.regularizers` for details.
Only used with ``admm``, ``lbfgs``, and ``proximal_grad`` solvers.
max_iter : int, default 100
Maximum number of iterations taken for the solvers to converge
tol : float, default 1e-4
Tolerance for stopping criteria. Ignored for ``admm`` solver
lambduh : float, default 1.0
Only used with ``admm``, ``lbfgs`` and ``proximal_grad`` solvers.
rho, over_relax, abstol, reltol : float
Only used with the ``admm`` solver.
Attributes
----------
coef_ : array, shape (n_classes, n_features)
The learned value for the model's coefficients
intercept_ : float of None
The learned value for the intercept, if one was added
to the model
Examples
--------
>>> from dask_glm.datasets import make_classification
>>> X, y = make_classification()
>>> est = LogisticRegression()
>>> est.fit(X, y)
>>> est.predict(X)
>>> est.predict_proba(X)
>>> est.score(X, y)
"""
family = families.Logistic
def predict(self, X):
return self.predict_proba(X) > .5 # TODO: verify, multiclass broken
def predict_proba(self, X):
X_ = self._maybe_add_intercept(X)
return sigmoid(dot(X_, self._coef))
def score(self, X, y):
return accuracy_score(y, self.predict(X))
class LinearRegression(_GLM):
"""
Estimator for a linear model using Ordinary Least Squares.
Parameters
----------
fit_intercept : bool, default True
Specifies if a constant (a.k.a. bias or intercept) should be
added to the decision function.
solver : {'admm', 'gradient_descent', 'newton', 'lbfgs', 'proximal_grad'}
Solver to use. See :ref:`api.algorithms` for details
regularizer : {'l1', 'l2'}
Regularizer to use. See :ref:`api.regularizers` for details.
Only used with ``admm`` and ``proximal_grad`` solvers.
max_iter : int, default 100
Maximum number of iterations taken for the solvers to converge
tol : float, default 1e-4
Tolerance for stopping criteria. Ignored for ``admm`` solver
lambduh : float, default 1.0
Only used with ``admm`` and ``proximal_grad`` solvers
rho, over_relax, abstol, reltol : float
Only used with the ``admm`` solver.
Attributes
----------
coef_ : array, shape (n_classes, n_features)
The learned value for the model's coefficients
intercept_ : float of None
The learned value for the intercept, if one was added
to the model
Examples
--------
>>> from dask_glm.datasets import make_regression
>>> X, y = make_regression()
>>> est = LinearRegression()
>>> est.fit(X, y)
>>> est.predict(X)
>>> est.score(X, y)
"""
family = families.Normal
def predict(self, X):
X_ = self._maybe_add_intercept(X)
return dot(X_, self._coef)
def score(self, X, y):
return mean_squared_error(y, self.predict(X))
class PoissonRegression(_GLM):
"""
Estimator for Poisson Regression.
Parameters
----------
fit_intercept : bool, default True
Specifies if a constant (a.k.a. bias or intercept) should be
added to the decision function.
solver : {'admm', 'gradient_descent', 'newton', 'lbfgs', 'proximal_grad'}
Solver to use. See :ref:`api.algorithms` for details
regularizer : {'l1', 'l2'}
Regularizer to use. See :ref:`api.regularizers` for details.
Only used with ``admm``, ``lbfgs``, and ``proximal_grad`` solvers.
max_iter : int, default 100
Maximum number of iterations taken for the solvers to converge
tol : float, default 1e-4
Tolerance for stopping criteria. Ignored for ``admm`` solver
lambduh : float, default 1.0
Only used with ``admm``, ``lbfgs`` and ``proximal_grad`` solvers.
rho, over_relax, abstol, reltol : float
Only used with the ``admm`` solver.
Attributes
----------
coef_ : array, shape (n_classes, n_features)
The learned value for the model's coefficients
intercept_ : float of None
The learned value for the intercept, if one was added
to the model
Examples
--------
>>> from dask_glm.datasets import make_poisson
>>> X, y = make_poisson()
>>> est = PoissonRegression()
>>> est.fit(X, y)
>>> est.predict(X)
>>> est.get_deviance(X, y)
"""
family = families.Poisson
def predict(self, X):
X_ = self._maybe_add_intercept(X)
return exp(dot(X_, self._coef))
def get_deviance(self, X, y):
return poisson_deviance(y, self.predict(X))