Пример #1
0
    def test_information(self):

        import numpy as np
        import parsimony.estimators as estimators
        import parsimony.algorithms.gradient as gradient
        from parsimony.algorithms.utils import Info

        np.random.seed(1337)

        n = 100
        p = 160
        X = np.random.rand(n, p)
        y = np.random.rand(n, 1)

        info = [Info.converged, Info.num_iter, Info.time, Info.fvalue,
                Info.func_val]
        # Note: Info.fvalue and Info.func_val are both returned for now. But
        # recall that Info.fvalue is deprecated!
        lr = estimators.LinearRegression(algorithm=gradient.GradientDescent(),
                                         algorithm_params=dict(max_iter=7000,
                                                               info=info),
                                         mean=False)

        error = lr.fit(X, y).score(X, y)
#        print "error:", error
        assert_less(error, 5e-2, "The found regression vector is not correct.")

        ret_info = lr.get_info()
#        print "ret_info:", sorted(ret_info.keys())
#        print "info:", sorted(info)
        assert sorted(ret_info.keys()) == sorted(info)

#        print len(ret_info[Info.time])
        assert len(ret_info[Info.time]) == ret_info[Info.num_iter]
#        print len(ret_info[Info.fvalue])
        assert len(ret_info[Info.fvalue]) == ret_info[Info.num_iter]

#        print "converged:", ret_info[Info.converged]
        assert not ret_info[Info.converged]
def mapper(key, output_collector):

    import mapreduce as GLOBAL
    Xtr = GLOBAL.DATA_RESAMPLED["X"][0]
    Xte = GLOBAL.DATA_RESAMPLED["X"][1]
    ytr = GLOBAL.DATA_RESAMPLED["y"][0]
    yte = GLOBAL.DATA_RESAMPLED["y"][1]

    # key = 'enettv_0.01_0.1_0.2'.split("_")
    algo, alpha, l1l2ratio, tvratio = key[0], float(key[1]), float(
        key[2]), float(key[3])

    tv = alpha * tvratio
    l1 = alpha * float(1 - tv) * l1l2ratio
    l2 = alpha * float(1 - tv) * (1 - l1l2ratio)

    print(key, algo, alpha, l1, l2, tv)

    scaler = preprocessing.StandardScaler().fit(Xtr[:, 1:])
    Xtr[:, 1:] = scaler.transform(Xtr[:, 1:])
    Xte[:, 1:] = scaler.transform(Xte[:, 1:])

    if algo == 'enettv':
        conesta = algorithms.proximal.CONESTA(max_iter=10000)
        mod = estimators.LinearRegressionL1L2TV(l1,
                                                l2,
                                                tv,
                                                GLOBAL.Atv,
                                                algorithm=conesta,
                                                penalty_start=penalty_start)
        mod.fit(Xtr, ytr.ravel())
        beta = mod.beta

    elif algo == 'enetgn':
        fista = algorithms.proximal.FISTA(max_iter=5000)
        mod = estimators.LinearRegressionL1L2GraphNet(
            l1,
            l2,
            tv,
            GLOBAL.Agn,
            algorithm=fista,
            penalty_start=penalty_start)
        mod.fit(Xtr, ytr.ravel())
        beta = mod.beta

    elif algo == 'enet':
        fista = algorithms.proximal.FISTA(max_iter=5000)
        mod = estimators.ElasticNet(l1l2ratio,
                                    algorithm=fista,
                                    penalty_start=penalty_start)
        mod.fit(Xtr, ytr.ravel())
        beta = mod.beta

    elif algo == 'Ridge':
        mod = estimators.RidgeRegression(l1l2ratio,
                                         penalty_start=penalty_start)
        mod.fit(Xtr, ytr.ravel())
        beta = mod.beta

    elif algo == 'RidgeAGD':
        mod = estimators.RidgeRegression(l1l2ratio,\
        algorithm=gradient.GradientDescent(max_iter=1000),penalty_start = penalty_start )
        mod.fit(Xtr, ytr.ravel())
        beta = mod.beta

    elif algo == 'linearSklearn':
        mod = linear_model.LinearRegression(fit_intercept=False)
        mod.fit(Xtr, ytr.ravel())
        beta = mod.coef_
        beta = beta.reshape(beta.shape[0], 1)

    elif algo == 'SkRidge':
        mod = linear_model.Ridge(alpha=l1l2ratio, fit_intercept=False)
        mod.fit(Xtr, ytr.ravel())
        beta = mod.coef_
        beta = beta.reshape(beta.shape[0], 1)

    elif algo == 'SkRidgeInt':
        mod = linear_model.Ridge(alpha=l1l2ratio, fit_intercept=True)
        mod.fit(Xtr, ytr.ravel())
        beta = mod.coef_
        beta = beta.reshape(beta.shape[0], 1)
    else:
        raise Exception('Algo%s not handled' % algo)

    y_pred = mod.predict(Xte)
    ret = dict(y_pred=y_pred, y_true=yte, beta=beta)
    if output_collector:
        output_collector.collect(key, ret)
    else:
        return ret