def fit(self, x, y, alpha, iterations, add_intercept=False): if add_intercept: x = add_intercept_term(x) m, n = x.shape self.theta = np.zeros(n) cost_history = np.zeros(iterations) for i in range(iterations): self.theta = self.theta - alpha * self.gradient(x, y, self.theta) cost_history[i] = self.cost(x, y, self.theta) return cost_history
def predict(self, x, add_intercept=True): """ Function that predicts the target based on the sample and the model's theta vector :param x: the sample value :param add_intercept: indicates whether it is needed to add the intercept term to features x :return: the predicted/target value """ if add_intercept: x = add_intercept_term(x) return self._hypothesis(x, self.theta)
def fit(self, x, y, alpha, iterations, add_intercept=False): """ Fits the training samples (x) to the targets (y) using linear regression training algorithm :param x: the matrix(m,n) with the training samples, where m = #samples and n = #features :param y: the vector(m) with the target values :param alpha: the learning rate used by the algorithm :param iterations: the number of iterations to perform :return: returns the history of the cost function calculated for each iteration. The theta coefficients are stored as part of the model's state """ if add_intercept: x = add_intercept_term(x) m, n = x.shape self.theta = np.zeros(n) cost_history = np.zeros(iterations) for i in range(iterations): self.theta = self.theta - alpha * (1 / m) * ( x.transpose().dot(self._hypothesis(x, self.theta) - y)) cost_history[i] = self.cost(x, y, self.theta) return cost_history
def test_linear_regression_cost_theta_non_zero(samples1, linear_regression_model): x, y = samples1 x = add_intercept_term(x) assert np.round(linear_regression_model.cost(x, y, [-1, 2]), 2) == 54.24
def test_predict_value(linear_regression_model): x = add_intercept_term(np.array([1, 2, 3]).reshape(3, 1)) y = np.array([4, 8, 12]) linear_regression_model.fit(x, y, alpha=0.01, iterations=10) prediction = linear_regression_model.predict(np.array([4])) assert prediction == np.array([1, 4]).dot(linear_regression_model.theta)
def test_fit_should_decrease_cost_function_after_each_iteration(linear_regression_model): x = add_intercept_term(np.array([1, 2, 3]).reshape(3, 1)) y = np.array([4, 8, 12]) J_history = linear_regression_model.fit(x, y, alpha=0.01, iterations=10) assert np.size(J_history) == 10 assert all(J_history[i] > J_history[i + 1] for i in range(len(J_history) - 1))
def test_fit_cost_function_history(linear_regression_model): x = add_intercept_term(np.array([1, 1, 1]).reshape(3, 1)) y = np.array([1, 2, 3]) J_history = linear_regression_model.fit(x, y, alpha=0.01, iterations=10) assert J_history is not None assert np.size(J_history) == 10
def test_cost_function_with_theta_zeros(linear_regression_model): x = add_intercept_term(np.array([1, 1, 1]).reshape(3, 1)) y = np.array([1, 2, 3]) theta = np.array([0, 0]) assert np.round(linear_regression_model.cost(x, y, theta), decimals=2) == 2.33
def test_fit_calculates_theta_vector(linear_regression_model): x = add_intercept_term(np.array([1, 1, 1]).reshape(3, 1)) y = np.array([1, 2, 3]) linear_regression_model.fit(x, y, alpha=0.01, iterations=10) assert linear_regression_model.theta is not None assert np.size(linear_regression_model.theta) == 2
def test_cost_function_with_multi_variables_returns_zero_when_x_and_y_fit(linear_regression_model): x = add_intercept_term(np.array([[1, 2], [2, 4], [2, 3]])) y = np.array([4, 7, 6]) theta = np.array([1, 1, 1]) assert linear_regression_model.cost(x, y, theta) == 0
def test_cost_function_with_multiple_variables(linear_regression_model): x = add_intercept_term(np.array([[1, 2], [2, 4], [2, 3]])) y = np.array([4, 7, 7]) theta = np.array([0, 1, 1]) assert linear_regression_model.cost(x, y, theta) == 1
def test_cost_function_returns_zero_when_x_and_y_fit(linear_regression_model): x = add_intercept_term(np.array([1, 2, 3]).reshape(3, 1)) y = np.array([1, 2, 3]) theta = np.array([0, 1]) assert linear_regression_model.cost(x, y, theta) == 0
def test_logistic_regression_cost_theta_non_zero(samples1, logistic_regression_model): x, y = samples1 x = add_intercept_term(x) assert np.round(logistic_regression_model.cost(x, y, [-24, 0.2, 0.2]), 3) == 0.218
def test_cost_function_with_zero_decision_boundary(logistic_regression_model): x = add_intercept_term(np.array([1, 1, 1]).reshape(3, 1)) y = np.array([0, 0, 0]) theta = np.array([0, 0]) cost = (1 / 3) * (-np.log(1 / 2) * 3) assert logistic_regression_model.cost(x, y, theta) == cost
def test_cost_function_with_good_decision_boundary(logistic_regression_model): x = add_intercept_term(np.array([1, 2, 3, 7, 8, 9]).reshape(6, 1)) y = np.array([0, 0, 0, 1, 1, 1]) theta = np.array([-4, 1]) assert logistic_regression_model.cost(x, y, theta) < 0.1