Repository navigation
Expand file tree
/
Copy pathlogistic_regression.py
More file actions
170 lines (133 loc) · 5.54 KB
/
Copy pathlogistic_regression.py
File metadata and controls
170 lines (133 loc) · 5.54 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
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
from typing import NoReturn
import numpy as np
from base_estimator import BaseEstimator
from gradient_descent import GradientDescent
from modules import L1, L2 ,LogisticModule, RegularizedModule
class LogisticRegression(BaseEstimator):
"""
Logistic Regression Classifier
Attributes
----------
solver_: GradientDescent, default=GradientDescent()
Descent method solver to use for the logistic regression objective optimization
penalty_: str, default="none"
Type of regularization term to add to logistic regression objective. Supported values
are "none", "l1", "l2"
lam_: float, default=1
Regularization parameter to be used in case `self.penalty_` is not "none"
alpha_: float, default=0.5
Threshold value by which to convert class probability to class value
include_intercept_: bool, default=True
Should fitted model include an intercept or not
coefs_: ndarray of shape (n_features,) or (n_features+1,)
Coefficients vector fitted by linear regression. To be set in
`LogisticRegression.fit` function.
"""
def __init__(self,
include_intercept: bool = True,
solver: GradientDescent = GradientDescent(),
penalty: str = "none",
lam: float = 1,
alpha: float = .5):
"""
Instantiate a linear regression estimator
Parameters
----------
solver: GradientDescent, default=GradientDescent()
Descent method solver to use for the logistic regression objective optimization
penalty: str, default="none"
Type of regularization term to add to logistic regression objective. Supported values
are "none", "l1", "l2"
lam: float, default=1
Regularization parameter to be used in case `self.penalty_` is not "none"
alpha: float, default=0.5
Threshold value by which to convert class probability to class value
include_intercept: bool, default=True
Should fitted model include an intercept or not
"""
super().__init__()
self.include_intercept_ = include_intercept
self.solver_ = solver
self.lam_ = lam
self.penalty_ = penalty
self.alpha_ = alpha
if penalty not in ["none", "l1", "l2"]:
raise ValueError("Supported penalty types are: none, l1, l2")
self.coefs_ = None
def _fit(self, X: np.ndarray, y: np.ndarray) -> NoReturn:
"""
Fit Logistic regression model to given samples
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to fit an estimator for
y : ndarray of shape (n_samples, )
Responses of input data to fit to
Notes
-----
Fits model using specified `self.solver_` passed when instantiating class and includes an intercept
if specified by `self.include_intercept_
"""
if self.include_intercept_:
X = np.c_[np.ones(X.shape[0]), X]
num_features = X.shape[1]
initial_weights = np.random.randn(num_features) / np.sqrt(num_features)
if self.penalty_ == "none":
objective_function = LogisticModule(weights=initial_weights)
else:
regularization_module = {"l1": L1(), "l2": L2()}[self.penalty_]
objective_function = RegularizedModule(
fidelity_module=LogisticModule(),
regularization_module=regularization_module,
lam=self.lam_,
include_intercept=self.include_intercept_,
weights=initial_weights)
self.coefs_ = self.solver_.fit(objective_function, X, y)
def _predict(self, X: np.ndarray) -> np.ndarray:
"""
Predict responses for given samples using fitted estimator
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to predict responses for
Returns
-------
responses : ndarray of shape (n_samples, )
Predicted responses of given samples
"""
probabilities = self.predict_proba(X)
return (probabilities > self.alpha_)
def predict_proba(self, X: np.ndarray) -> np.ndarray:
"""
Predict probabilities of samples being classified as `1` according to sigmoid(Xw)
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Input data to predict probability for
Returns
-------
probabilities: ndarray of shape (n_samples,)
Probability of each sample being classified as `1` according to the fitted model
"""
if not self.fitted_:
raise ValueError("Must fit before calling 'predict_proba'")
if self.include_intercept_:
X = np.c_[np.ones(len(X)), X]
sigmoid_value = np.exp(X @ self.coefs_)
return sigmoid_value / (1 + sigmoid_value )
def _loss(self, X: np.ndarray, y: np.ndarray) -> float:
"""
Evaluate performance under misclassification error
Parameters
----------
X : ndarray of shape (n_samples, n_features)
Test samples
y : ndarray of shape (n_samples, )
True labels of test samples
Returns
-------
loss : float
Performance under misclassification error
"""
from loss_functions import misclassification_error
return misclassification_error(y_true=y, y_pred=self.predict(X))