def test6(self): ridge_sk = sklearn.linear_model.Ridge(alpha=1000) ridge_sk.fit(self.x_train.T, self.y_train.ravel()) preds_ridgesk = ridge_sk.predict(self.x_test.T) r_squared_sk = r_squared(self.y_test, preds_ridgesk) ridge_obj2 = RidgeRegression(degree=1, regParam=1000) ridge_obj2.fit_iterative_optimizer(xtrain=self.x_train, ytrain=self.y_train, num_epochs=200, learn_rate=0.1) preds_ridge = ridge_obj2.predict_linear_regression(self.x_test) r_squared_own = r_squared(self.y_test, preds_ridge) self.assertLessEqual(abs(r_squared_own - r_squared_sk), 0.07)
def test5(self): lasso_sk = sklearn.linear_model.Lasso(alpha=1) lasso_sk.fit(self.x_train.T, self.y_train.ravel()) preds_lassosk = lasso_sk.predict(self.x_test.T) r_squared_sk = r_squared(self.y_test, preds_lassosk) lasso_obj2 = LassoRegression(degree=1, regParam=1) lasso_obj2.fit_iterative_optimizer(xtrain=self.x_train, ytrain=self.y_train, num_epochs=15, learn_rate=0.15) preds_lasso = lasso_obj2.predict_linear_regression(self.x_test) r_squared_own = r_squared(self.y_test, preds_lasso) self.assertLessEqual(abs(r_squared_own - r_squared_sk), 0.08)
def test4(self): lin_reg = sklearn.linear_model.LinearRegression() lin_reg.fit(self.x_train.T, self.y_train.ravel()) preds_linreg = lin_reg.predict(self.x_test.T) r_squared_sk = r_squared(self.y_test, preds_linreg) lin_regOwn = LinearRegression(degree=1) lin_regOwn.fit_iterative_optimizer(xtrain=self.x_train, ytrain=self.y_train, num_epochs=50, learn_rate=0.15) preds_lrOwn = lin_regOwn.predict_linear_regression(self.x_test) r_squared_own = r_squared(self.y_test, preds_lrOwn) self.assertLessEqual(abs(r_squared_own - r_squared_sk), 0.07)
def test9(self): ridge_estimator = RidgeRegression(degree=2, regParam=55) ridge_estimator.fit_iterative_optimizer(xtrain=self.x_train, ytrain=self.y_train, num_epochs=275, learn_rate=0.01) preds = ridge_estimator.predict_linear_regression(self.x_test) r_squared_val = r_squared(self.y_test, preds) self.assertGreaterEqual(r_squared_val, 0.85)
def test7(self): degree_2 = LinearRegression(degree=2) degree_2.fit_iterative_optimizer(xtrain=self.x_train, ytrain=self.y_train, num_epochs=275, learn_rate=0.01, ret_train_loss=True) deg_2 = degree_2.predict_linear_regression(self.x_test) r_squared_val = r_squared(self.y_test, deg_2) self.assertGreaterEqual(r_squared_val, 0.8)
def test3(self): ridge_obj = RidgeRegression(degree=1, regParam=1000) ridge_obj.fit_iterative_optimizer(xtrain=self.x_train, ytrain=self.y_train, xvalid=self.x_valid, yvalid=self.y_valid, num_epochs=100, ret_train_loss=True, learn_rate=0.1) preds2 = ridge_obj.predict_linear_regression(self.x_test) r_squared_val = r_squared(self.y_test, preds2) self.assertGreaterEqual(r_squared_val, 0.5)
def test1(self) -> None: lr_obj = LinearRegression(degree=1) lr_obj.fit_iterative_optimizer(xtrain=self.x_train, ytrain=self.y_train, xvalid=self.x_valid, yvalid=self.y_valid, num_epochs=100, ret_train_loss=True, learn_rate=0.1) preds = lr_obj.predict_linear_regression(self.x_test) r_squared_val = r_squared(self.y_test, preds) self.assertGreaterEqual(r_squared_val, 0.5)