def compute_cost(self):
"""Computes cost based on the current values of the parameters
Returns
-------
cost : float
Cost of the selection of current set of parameters
"""
hypothesis = LogisticRegression.sigmoid(np.dot(self.X, self.theta))
#new ndarray to prevent intercept from theta array to be changed
theta=np.delete(self.theta,0)
#regularization term
reg = (self.lamda/2*self.num_training)*np.sum(np.power(theta,2))
cost = -(np.sum(self.y * np.log(hypothesis) + (1 - self.y) * (np.log(1 - hypothesis)))) / self.num_training
#if regularization is true, add regularization term and return cost
if self.reg:
return cost + reg
return cost
评论列表
文章目录