MCPcopy Create free account
hub / github.com/rushter/MLAlgorithms / BasicRegression

Class BasicRegression

mla/linear_models.py:14–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13
14class BasicRegression(BaseEstimator):
15 def __init__(
16 self, lr=0.001, penalty="None", C=0.01, tolerance=0.0001, max_iters=1000
17 ):
18 """Basic class for implementing continuous regression estimators which
19 are trained with gradient descent optimization on their particular loss
20 function.
21
22 Parameters
23 ----------
24 lr : float, default 0.001
25 Learning rate.
26 penalty : str, {'l1', 'l2', None'}, default None
27 Regularization function name.
28 C : float, default 0.01
29 The regularization coefficient.
30 tolerance : float, default 0.0001
31 If the gradient descent updates are smaller than `tolerance`, then
32 stop optimization process.
33 max_iters : int, default 10000
34 The maximum number of iterations.
35 """
36 self.C = C
37 self.penalty = penalty
38 self.tolerance = tolerance
39 self.lr = lr
40 self.max_iters = max_iters
41 self.errors = []
42 self.theta = []
43 self.n_samples, self.n_features = None, None
44 self.cost_func = None
45
46 def _loss(self, w):
47 raise NotImplementedError()
48
49 def init_cost(self):
50 raise NotImplementedError()
51
52 def _add_penalty(self, loss, w):
53 """Apply regularization to the loss."""
54 if self.penalty == "l1":
55 loss += self.C * np.abs(w[1:]).sum()
56 elif self.penalty == "l2":
57 loss += (0.5 * self.C) * (w[1:] ** 2).sum()
58 return loss
59
60 def _cost(self, X, y, theta):
61 prediction = X.dot(theta)
62 error = self.cost_func(y, prediction)
63 return error
64
65 def fit(self, X, y=None):
66 self._setup_input(X, y)
67 self.init_cost()
68 self.n_samples, self.n_features = X.shape
69
70 # Initialize weights + bias term
71 self.theta = np.random.normal(size=(self.n_features + 1), scale=0.5)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected