def test_simple_lasso(self): X, y, X_test, y_test = build_dataset() # check if FusedLasso generates same result of LassoAdmm when fused_coef is zero clf = FusedLassoADMM(alpha=0.05, sparse_coef=1, fused_coef=0, tol=1e-8).fit(X, y) self.assertGreater(clf.score(X_test, y_test), 0.99) self.assertLess(clf.n_iter_, 150)
def test_fused_lasso_alpha(self): X = np.random.normal(0.0, 1.0, (8, 4)) beta = np.array([4, 4, 0, 0]) y = X.dot(beta) T = np.array([[5., 6., 7., 8.], [9., 10., 11., 12.], [13., 14., 15., 16.]]) # test sample # small regularization parameter clf = FusedLassoADMM(alpha=1e-8).fit(X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.997, 3.998, -0.037, 0.005], decimal=3) assert_array_almost_equal(actual, [43.774, 75.626, 107.478], decimal=3) self.assertLess(clf.n_iter_, 150) # default clf = FusedLassoADMM(alpha=1).fit(X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.197, 1.599, 0.799, 0.4], decimal=3) assert_array_almost_equal(actual, [34.691, 58.673, 82.654], decimal=3) self.assertLess(clf.n_iter_, 150) # all coefs will be zero clf = FusedLassoADMM(alpha=10).fit(X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [0, 0, 0, 0], decimal=3) assert_array_almost_equal(actual, [3.73, 3.733, 3.736], decimal=3) print(T, clf.coef_, actual) self.assertLess(clf.n_iter_, 20)
def test_fused_lasso_alpha(self): beta = np.array([4, 4, 0, 0]) y = self.X.dot(beta) T = np.array([[5., 6., 7., 8.], [9., 10., 11., 12.], [13., 14., 15., 16.]]) # test sample # small regularization parameter clf = FusedLassoADMM(alpha=1e-8).fit(self.X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.998, 3.998, -0.022, 0.003], decimal=3) assert_array_almost_equal(actual, [43.862, 75.773, 107.683], decimal=3) self.assertLess(clf.n_iter_, 100) # default clf = FusedLassoADMM(alpha=1).fit(self.X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [2.428, 1.506, 0., 0.], decimal=3) assert_array_almost_equal(actual, [22.69, 38.427, 54.164], decimal=3) self.assertLess(clf.n_iter_, 100) # all coefs will be zero clf = FusedLassoADMM(alpha=10).fit(self.X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [0, 0, 0, 0], decimal=3) assert_array_almost_equal(actual, [3.724, 3.722, 3.72], decimal=3) self.assertLess(clf.n_iter_, 20)
def test_fused_lasso_coef(self): X = np.random.normal(0.0, 1.0, (8, 4)) beta = np.array([4, 4, 0, 0]) y = X.dot(beta) T = np.array([[5., 6., 7., 8.], [9., 10., 11., 12.], [13., 14., 15., 16.]]) # test sample # small fused_coef clf = FusedLassoADMM(alpha=1e-8, fused_coef=1e-4).fit(X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.999e+00, 3.999e+00, -7.296e-03, 9.860e-04], decimal=3) assert_array_almost_equal(actual, [43.95, 75.916, 107.883], decimal=3) self.assertLess(clf.n_iter_, 100) # large fused_coef clf = FusedLassoADMM(alpha=1e-8, fused_coef=10).fit(X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.916, 3.669, -0.107, 0.196], decimal=3) assert_array_almost_equal(actual, [42.499, 73.198, 103.896], decimal=3) self.assertLess(clf.n_iter_, clf.max_iter) # small sparse_coef clf = FusedLassoADMM(alpha=1e-8, sparse_coef=1e-4).fit(X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.999e+00, 3.999e+00, -1.100e-02, 2.126e-03], decimal=3) assert_array_almost_equal(actual, [43.931, 75.885, 107.839], decimal=3) self.assertLess(clf.n_iter_, 100) # large fused_coef clf = FusedLassoADMM(alpha=1e-8, sparse_coef=10).fit(X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.912, 3.826, -0.336, 0.124], decimal=3) assert_array_almost_equal(actual, [41.367, 71.471, 101.575], decimal=3) self.assertLess(clf.n_iter_, clf.max_iter)
def test_fused_lasso_coef(self): beta = np.array([4, 4, 0, 0]) y = self.X.dot(beta) T = np.array([[5., 6., 7., 8.], [9., 10., 11., 12.], [13., 14., 15., 16.]]) # test sample # small trend_coef clf = FusedLassoADMM(alpha=1e-8, trend_coef=1e-4).fit(self.X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.999, 3.999, -0.007, 0.001], decimal=3) assert_array_almost_equal(actual, [43.95, 75.916, 107.883], decimal=3) self.assertLess(clf.n_iter_, 100) # large trend_coef clf = FusedLassoADMM(alpha=1e-8, trend_coef=10).fit(self.X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.938, 3.755, -0.079, 0.141], decimal=3) assert_array_almost_equal(actual, [42.862, 73.885, 104.908], decimal=3) self.assertLess(clf.n_iter_, clf.max_iter) # small sparse_coef clf = FusedLassoADMM(alpha=1e-8, sparse_coef=1e-4).fit(self.X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.999, 3.999, -0.011, 0.002], decimal=3) assert_array_almost_equal(actual, [43.931, 75.885, 107.839], decimal=3) self.assertLess(clf.n_iter_, 100) # large sparse_coef clf = FusedLassoADMM(alpha=1e-8, sparse_coef=10).fit(self.X, y) actual = clf.predict(T) assert_array_almost_equal(clf.coef_, [3.913, 3.837, -0.349, 0.113], decimal=3) assert_array_almost_equal(actual, [41.265, 71.32, 101.374], decimal=3) self.assertLess(clf.n_iter_, clf.max_iter)