-
Notifications
You must be signed in to change notification settings - Fork 0
/
regression_models.py
47 lines (37 loc) · 1.23 KB
/
regression_models.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
from MLPR_toolbox.functions import sigmoid, binary_cross_entropy
from scipy.optimize import minimize
import numpy as np
class LogisticRegression:
"""
Binary class logistic regression model.
N - number of observations
D - number of features
Parameters:
===========
X (array): (NxD) design matrix of features
y (array): (Nx1) columns vector of binary labels
"""
def __init__(self, X, y):
self.X, self.y = X, y
def fit(self, eps=0):
"""
Fit logistic regression model.
Parameters:
===========
eps (int): robustness parameter. Default: eps=0
Returns:
========
w_opt (array): a column vector of fitted weights
"""
X, y = self.X, self.y
def fun(w, eps):
y_pred = (1-eps)*sigmoid(X@w) + eps/2
cost = binary_cross_entropy(y, y_pred)
return cost
self.w_opt = minimize(lambda w: fun(w.reshape(-1,1), eps ), x0=np.zeros(2), method='BFGS')['x'].reshape(-1,1)
return self.w_opt
def predict(self, X):
return sigmoid(X@self.w_opt)
def fit_predict(self, X, eps):
self.fit(eps)
return self.predict(X)