-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathElasticNet.py
More file actions
67 lines (56 loc) · 1.92 KB
/
Copy pathElasticNet.py
File metadata and controls
67 lines (56 loc) · 1.92 KB
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
import numpy as np
from numba import jit
from typing import Tuple
class ElasticNetModel:
def __init__(
self,
learning_rate: float = 0.01,
iterations: int = 1000,
l1_ratio: float = 0.5,
alpha: float = 1.0) -> None:
self.learning_rate = learning_rate
self.iterations = iterations
self.l1_ratio = l1_ratio
self.alpha = alpha
self.weights = np.empty(0)
self.bias = 0.0
def fit(
self,
features: np.ndarray,
target: np.ndarray) -> None:
num_samples, num_features = features.shape
self.weights = np.zeros(num_features)
self.bias = 0.0
self.weights, self.bias = self._optimize(
features,
target,
self.weights,
self.bias,
self.learning_rate,
self.iterations,
self.alpha,
self.l1_ratio,
num_samples)
@staticmethod
@jit(nopython=True, nogil=True)
def _optimize(
features: np.ndarray,
target: np.ndarray,
weights: np.ndarray,
bias: float,
learning_rate: float,
iterations: int,
alpha: float,
l1_ratio: float,
num_samples: int) -> Tuple[np.ndarray, float]:
for _ in range(iterations):
predictions = np.dot(features, weights) + bias
errors = predictions - target
l2_gradient = 2 * weights
l1_gradient = np.sign(weights)
weights -= learning_rate * ((1 / num_samples) * np.dot(features.T, errors) + alpha * ((1 - l1_ratio) * l2_gradient + l1_ratio * l1_gradient))
bias -= learning_rate * (1 / num_samples) * np.sum(errors)
return weights, bias
def predict(self, features: np.ndarray) -> np.ndarray:
predictions = np.dot(features, self.weights) + self.bias
return predictions