def fit(self, data, labels, update=True): ''' Calculates the gradient and train loss. If the update flag is set to False, gradient is calculated but own weights will not be updated ''' total_delta_w = {} train_loss = 0 for x, label in zip(data, labels): xw = dotproduct(x, self.__w) if self.__misclassification(xw, label): delta_w = self.__gradient(x, label) if update: self.update_weights(delta_w) else: delta_w = self.__regularization_gradient(x) if update: self.update_weights(delta_w) for k, v in delta_w.items(): if k in total_delta_w: total_delta_w[k] += v else: total_delta_w[k] = v train_loss += max(1 - label * xw, 0) train_loss += self.__regularizer(x) return total_delta_w, train_loss / len(labels)
def loss(self, data, labels): ''' Returns the MSE loss of the data with the true labels. ''' total_loss = 0 for x, label in zip(data, labels): xw = dotproduct(x, self.__w) total_loss += max(1 - label * xw, 0) total_loss += self.__regularizer(x) return total_loss / len(labels)
def predict(self, data): ''' Predict the labels of the input data ''' return [sign(dotproduct(x, self.__w)) for x in data]