Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Lasso test time #415

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
37 changes: 10 additions & 27 deletions tests/test_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_save_load(self):

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
Expand All @@ -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)
Expand All @@ -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
Expand Down