def test_proj_simplex(size, z): rng = np.random.RandomState(0) v = rng.rand(size) w = project_simplex(v, z=z) w2 = project_simplex_bisection(v, z=z, max_iter=100) np.testing.assert_array_almost_equal(w, w2, 3)
def test_proj_simplex(): rng = np.random.RandomState(0) v = rng.rand(100) w = project_simplex(v, z=10) w2 = project_simplex_bisection(v, z=10, max_iter=100) assert_array_almost_equal(w, w2, 3) v = rng.rand(3) w = project_simplex(v, z=1) w2 = project_simplex_bisection(v, z=1, max_iter=100) assert_array_almost_equal(w, w2, 3) v = rng.rand(2) w = project_simplex(v, z=1) w2 = project_simplex_bisection(v, z=1, max_iter=100) assert_array_almost_equal(w, w2, 3)
def test_fista_regression_simplex(): rng = np.random.RandomState(0) w = project_simplex(rng.rand(10)) X = rng.randn(1000, 10) y = np.dot(X, w) reg = FistaRegressor(penalty="simplex", max_iter=100, verbose=0) reg.fit(X, y) y_pred = reg.predict(X) error = np.sqrt(np.mean((y - y_pred)**2)) assert_almost_equal(error, 0.000, 3) assert_true(np.all(reg.coef_ >= 0)) assert_almost_equal(np.sum(reg.coef_), 1.0, 3)
def test_fista_regression_simplex(): rng = np.random.RandomState(0) w = project_simplex(rng.rand(10)) X = rng.randn(1000, 10) y = np.dot(X, w) reg = FistaRegressor(penalty="simplex", max_iter=100, verbose=0) reg.fit(X, y) y_pred = reg.predict(X) error = np.sqrt(np.mean((y - y_pred) ** 2)) assert_almost_equal(error, 0.000, 3) assert_true(np.all(reg.coef_ >= 0)) assert_almost_equal(np.sum(reg.coef_), 1.0, 3)
def test_fista_regression_l1_ball(): rng = np.random.RandomState(0) alpha = 5.0 w = project_simplex(rng.randn(10), alpha) X = rng.randn(1000, 10) y = np.dot(X, w) reg = FistaRegressor(penalty="l1-ball", alpha=alpha, max_iter=100, verbose=0) reg.fit(X, y) y_pred = reg.predict(X) error = np.sqrt(np.mean((y - y_pred) ** 2)) np.testing.assert_almost_equal(error, 0.000, 3) np.testing.assert_almost_equal(np.sum(np.abs(reg.coef_)), alpha, 3)
def test_fista_regression_l1_ball(): rng = np.random.RandomState(0) alpha = 5.0 w = project_simplex(rng.randn(10), alpha) X = rng.randn(1000, 10) y = np.dot(X, w) reg = FistaRegressor(penalty="l1-ball", alpha=alpha, max_iter=100, verbose=0) reg.fit(X, y) y_pred = reg.predict(X) error = np.sqrt(np.mean((y - y_pred) ** 2)) assert_almost_equal(error, 0.000, 3) assert_almost_equal(np.sum(np.abs(reg.coef_)), alpha, 3)