def test_cost(self): """ Tests the cost function. """ X, y, betas, pred_y = self.solve_iris_regression() exp_mse = mean_squared_error(y, pred_y) # use cost function predict method cost_func = cost_functions.LinearCostFunction(X, y) cost = cost_func.cost(betas) self.assertAlmostEqual(cost, exp_mse)
def test_error(self): """ Tests the mean-squared error implementation """ X, y, _, pred_y = self.solve_iris_regression() # use cost function predict method cost = cost_functions.LinearCostFunction(X, y) obs_mse = cost._mse(y, pred_y) exp_mse = mean_squared_error(y, pred_y) self.assertAlmostEqual(obs_mse, exp_mse)
def test_predict(self): """ Tests the predict method using known model params """ X, y, betas, exp_y = self.solve_iris_regression() # use cost function predict method cost = cost_functions.LinearCostFunction(X, y) obs_y = cost.predict(X, betas) mse = mean_squared_error(obs_y, exp_y) self.assertEqual(obs_y.shape, y.shape) self.assertAlmostEqual(mse, 0.0)
def test_optim(self): """ Test optimizing the cost function. """ X, y, betas, normal_pred_y = self.solve_iris_regression() exp_mse = mean_squared_error(y, normal_pred_y) cost_func = cost_functions.LinearCostFunction(X, y) optimizer = optim.Optimizer(0.01, 10000, 1e-4, 1e-4) initial_params = np.zeros(betas.shape) optim_params, n_iters = optimizer.optimize(cost_func, initial_params) print(n_iters) print(betas) print(optim_params) gd_pred_y = cost_func._predict(X, optim_params) obs_mse = mean_squared_error(y, gd_pred_y) self.assertAlmostEqual(exp_mse, obs_mse, 2)