From 8db879de002e9e78eb7ede826cab2060343750b5 Mon Sep 17 00:00:00 2001 From: Cristian Tatu Date: Wed, 10 Aug 2022 14:40:58 +0200 Subject: [PATCH 1/2] Reduce Lasso test time --- tests/test_lasso.py | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/tests/test_lasso.py b/tests/test_lasso.py index 37c7f40b..39a7901a 100644 --- a/tests/test_lasso.py +++ b/tests/test_lasso.py @@ -32,18 +32,18 @@ def test_fit_predict(self): lasso = Lasso(lmbd=0.1, max_iter=50) - lasso.fit(ds.array(X_train, (5, 100)), ds.array(y_train, (5, 1))) + lasso.fit(ds.array(X_train, (25, 100)), ds.array(y_train, (25, 1))) y_pred_lasso = lasso.predict(ds.array(X_test, (25, 100))) r2_score_lasso = r2_score(y_test, y_pred_lasso.collect()) - self.assertAlmostEqual(r2_score_lasso, 0.9481746925431124) + self.assertAlmostEqual(r2_score_lasso, 0.961471905600488) def test_save_load(self): """ Tests load and save methods """ np.random.seed(42) - n_samples, n_features = 50, 100 + n_samples, n_features = 10, 20 X = np.random.randn(n_samples, n_features) # Decreasing coef w. alternated signs for visualization @@ -57,43 +57,30 @@ def test_save_load(self): n_samples = X.shape[0] X_train, y_train = X[:n_samples // 2], y[:n_samples // 2] - X_test, y_test = X[n_samples // 2:], y[n_samples // 2:] - lasso = Lasso(lmbd=0.1, max_iter=50) + lasso = Lasso(lmbd=0.1, max_iter=1) - lasso.fit(ds.array(X_train, (5, 100)), ds.array(y_train, (5, 1))) + lasso.fit(ds.array(X_train, (5, 20)), ds.array(y_train, (5, 1))) lasso.save_model("./lasso_model") - lasso2 = Lasso() + lasso2 = Lasso(max_iter=1) lasso2.load_model("./lasso_model") - y_pred_lasso = lasso2.predict(ds.array(X_test, (25, 100))) - r2_score_lasso = r2_score(y_test, y_pred_lasso.collect()) - - self.assertAlmostEqual(r2_score_lasso, 0.9481746925431124) lasso.save_model("./lasso_model", save_format="cbor") - lasso2 = Lasso() + lasso2 = Lasso(max_iter=1) lasso2.load_model("./lasso_model", load_format="cbor") - y_pred_lasso = lasso2.predict(ds.array(X_test, (25, 100))) - r2_score_lasso = r2_score(y_test, y_pred_lasso.collect()) - - self.assertAlmostEqual(r2_score_lasso, 0.9481746925431124) lasso.save_model("./lasso_model", save_format="pickle") - lasso2 = Lasso() + lasso2 = Lasso(max_iter=1) lasso2.load_model("./lasso_model", load_format="pickle") - y_pred_lasso = lasso2.predict(ds.array(X_test, (25, 100))) - r2_score_lasso = r2_score(y_test, y_pred_lasso.collect()) - - self.assertAlmostEqual(r2_score_lasso, 0.9481746925431124) with self.assertRaises(ValueError): lasso.save_model("./lasso_model", save_format="txt") with self.assertRaises(ValueError): - lasso2 = Lasso() + lasso2 = Lasso(max_iter=1) lasso2.load_model("./lasso_model", load_format="txt") y2 = np.dot(X, coef) @@ -102,17 +89,13 @@ def test_save_load(self): n_samples = X.shape[0] X_train, y_train = X[:n_samples // 2], y2[:n_samples // 2] - lasso = Lasso(lmbd=0.1, max_iter=50) + lasso = Lasso(lmbd=0.1, max_iter=1) - lasso.fit(ds.array(X_train, (5, 100)), ds.array(y_train, (5, 1))) + lasso.fit(ds.array(X_train, (5, 20)), ds.array(y_train, (5, 1))) lasso.save_model("./lasso_model", overwrite=False) - lasso2 = Lasso() + lasso2 = Lasso(max_iter=1) lasso2.load_model("./lasso_model", load_format="pickle") - y_pred_lasso = lasso2.predict(ds.array(X_test, (25, 100))) - r2_score_lasso = r2_score(y_test, y_pred_lasso.collect()) - - self.assertAlmostEqual(r2_score_lasso, 0.9481746925431124) cbor2_module = utilmodel.cbor2 utilmodel.cbor2 = None From 44912a74f0c98ea35f5ecd88e297c64c7439cff8 Mon Sep 17 00:00:00 2001 From: Cristian Tatu Date: Thu, 18 Aug 2022 11:22:22 +0200 Subject: [PATCH 2/2] Update test_lasso.py --- tests/test_lasso.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_lasso.py b/tests/test_lasso.py index 39a7901a..1f86a0c6 100644 --- a/tests/test_lasso.py +++ b/tests/test_lasso.py @@ -32,11 +32,11 @@ def test_fit_predict(self): lasso = Lasso(lmbd=0.1, max_iter=50) - lasso.fit(ds.array(X_train, (25, 100)), ds.array(y_train, (25, 1))) + lasso.fit(ds.array(X_train, (5, 100)), ds.array(y_train, (5, 1))) y_pred_lasso = lasso.predict(ds.array(X_test, (25, 100))) r2_score_lasso = r2_score(y_test, y_pred_lasso.collect()) - self.assertAlmostEqual(r2_score_lasso, 0.961471905600488) + self.assertAlmostEqual(r2_score_lasso, 0.9481746925431124) def test_save_load(self): """ Tests load and save methods """