-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegularizedLogisticRegression.py
61 lines (43 loc) · 1.73 KB
/
RegularizedLogisticRegression.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
import numpy as np
from gradientForClassification import compute_gradient
def compute_cost_reg(X, y, w, b, lambda_=1):
"""
Computes the cost over all examples
Args:
X : (ndarray Shape (m,n)) data, m examples by n features
y : (ndarray Shape (m,)) target value
w : (ndarray Shape (n,)) values of parameters of the model
b : (scalar) value of bias parameter of the model
lambda_ : (scalar, float) Controls amount of regularization
Returns:
total_cost : (scalar) cost
"""
m, n = X.shape
# Calls the compute_cost function that you implemented above
cost_without_reg = compute_cost(X, y, w, b)
# You need to calculate this value
### START CODE HERE ###
reg_cost = (lambda_ / (2 * m)) * (np.sum(w ** 2))
### END CODE HERE ###
# Add the regularization cost to get the total cost
total_cost = cost_without_reg + reg_cost
return total_cost
def compute_gradient_reg(X, y, w, b, lambda_=1):
"""
Computes the gradient for logistic regression with regularization
Args:
X : (ndarray Shape (m,n)) data, m examples by n features
y : (ndarray Shape (m,)) target value
w : (ndarray Shape (n,)) values of parameters of the model
b : (scalar) value of bias parameter of the model
lambda_ : (scalar,float) regularization constant
Returns
dj_db : (scalar) The gradient of the cost w.r.t. the parameter b.
dj_dw : (ndarray Shape (n,)) The gradient of the cost w.r.t. the parameters w.
"""
m, n = X.shape
dj_db, dj_dw = compute_gradient(X, y, w, b)
### START CODE HERE ###
dj_dw = dj_dw + (lambda_ / m) * w
### END CODE HERE ###
return dj_db, dj_dw