-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosts.py
33 lines (27 loc) · 805 Bytes
/
costs.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
import numpy as np
from hw_helpers import sigmoid
def compute_mse(y, tx, w):
"""
Compute the mean square error.
:param y: labels
:param tx: features
:param w: weights
:return: the mean square error
"""
e = y - tx.dot(w)
mse = e.T.dot(e) / (2 * len(e))
return mse
def calculate_loss(y, tx, w, lambda_=0):
"""
Compute the negative log likelihood
:param y: labels
:param tx: features
:param w: weights
:param lambda_: regularization
:return: the negative log likelihood
"""
# ***************************************************
# INSERT YOUR CODE HERE
# TODO
# ***************************************************
return (((-1)*(y*np.log(sigmoid(tx@w))+(1-y)*np.log(1-sigmoid(tx@w)))) + lambda_/2*w.T@w).mean()