Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
def test_fista_regression_trace():
    rng = np.random.RandomState(0)
    def _make_data(n_samples, n_features, n_tasks, n_components):
        W = rng.rand(n_tasks, n_features) - 0.5
        U, S, V = svd(W, full_matrices=True)
        S[n_components:] = 0
        S = diagsvd(S, U.shape[0], V.shape[0])
        W = np.dot(np.dot(U, S), V)
        X = rng.rand(n_samples, n_features) - 0.5
        Y = np.dot(X, W.T)
        return X, Y, W

    X, Y, W = _make_data(200, 50,30, 5)
    reg = FistaRegressor(max_iter=15, verbose=0)
    reg.fit(X, Y)
    Y_pred = reg.predict(X)
    error = (Y_pred - Y).ravel()
    error = np.dot(error, error)
    assert_almost_equal(error, 77.45, 2)
Пример #6
0
    def fit(self, X, y):
        if self.w is None:
            self.w = np.ones(X.shape[1])

        if self.lam is None:
            lam_max, lam_min = _get_lam_max_min(X, y, self.eps)
            self.lambda_path_ = np.logspace(np.log10(lam_max), np.log10(lam_min), self.n_lam)
        else:
            self.lambda_path_ = [self.lam]

        scorer = make_scorer(self.metric)
        self.coef_path_, self.model_path_, self.score_path_ = [], [], []
        for lam_i in self.lambda_path_:
            # Setup model
            per_model_n = len(y) * ((self.cv-1) / self.cv)
            model_i = FistaRegressor(
                C=1/per_model_n,
                penalty=_LassoProjection(self.w),
                alpha=lam_i
            )

            # Get fit data
            scores_i = cross_val_score(
                model_i,
                X, y,
                scoring=scorer,
                cv=self.cv,
                n_jobs=self.n_jobs
            )

            # Fit model
            model_i.fit(X, y)
            self.coef_path_.append(model_i.coef_)
            self.score_path_.append(scores_i.mean())
            self.model_path_.append(model_i)

        self.coef_path_ = np.vstack(self.coef_path_)
        self.best_index = np.argmin(self.score_path_)
Пример #7
0
def test_fista_regression():
    reg = FistaRegressor(max_iter=100, verbose=0)
    reg.fit(bin_dense, bin_target)
    y_pred = np.sign(reg.predict(bin_dense))
    assert_almost_equal(np.mean(bin_target == y_pred), 0.985)
Пример #8
0
def test_fista_regression():
    reg = FistaRegressor(max_iter=100, verbose=0)
    reg.fit(bin_dense, bin_target)
    y_pred = np.sign(reg.predict(bin_dense))
    assert_almost_equal(np.mean(bin_target == y_pred), 0.985)
Пример #9
0
def test_fista_regression(bin_dense_train_data):
    X, y = bin_dense_train_data
    reg = FistaRegressor(max_iter=100, verbose=0)
    reg.fit(X, y)
    y_pred = np.sign(reg.predict(X))
    np.testing.assert_almost_equal(np.mean(y == y_pred), 0.985)