コード例 #1
0
ファイル: test_mtw.py プロジェクト: vishalbelsare/mutar
def test_mtw_vs_lasso(gaussian_data, beta, positive):
    X, y = gaussian_data
    n_samples = y.shape[1]
    Xty = np.array([xx.T.dot(yy) for xx, yy in zip(X, y)])
    beta_max = abs(Xty).max()
    beta *= beta_max / n_samples
    remtw = ReMTW(alpha=0., beta=beta, concomitant=False, positive=positive)
    remtw.fit(X, y)
    relasso = IndRewLasso(alpha=len(X) * [beta], positive=positive)
    relasso.fit(X, y)

    loss1 = remtw.loss_
    loss2 = relasso.loss_
    n = min(len(loss1), len(loss2))
    assert_allclose(remtw.coef_, relasso.coef_, atol=1e-2)
    assert_allclose(loss1[:n], loss2[:n], atol=1e-2)

    mtw = MTW(alpha=0., beta=beta, concomitant=False, positive=positive)
    mtw.fit(X, y)

    lasso = IndLasso(alpha=len(X) * [beta], positive=positive)
    lasso.fit(X, y)

    if positive:
        assert remtw.coef_.min() >= 0.
        assert relasso.coef_.min() >= 0.
        assert mtw.coef_.min() >= 0.
        assert lasso.coef_.min() >= 0.

    assert_allclose(mtw.coef_, lasso.coef_, atol=1e-2)
コード例 #2
0
def test_not_converged_reweighting(gaussian_data):
    X, y = gaussian_data
    m = 2
    est0 = IndRewLasso(alpha=len(X) * [0.1], max_iter_reweighting=m)
    est1 = ReMTW(alpha=0., beta=0.1, max_iter_reweighting=m)

    models = [est0, est1]
    for model in models:
        with pytest.warns(UserWarning, match='reweighting'):
            model.fit(X, y)
コード例 #3
0
def test_log_domain(identity_data):
    X, y = identity_data
    _, n_features = X[0].shape

    # catch log-domain stabilization with tiny epsilon
    est1 = ReMTW(alpha=0.01, beta=0.01, max_iter=20, gamma=1., epsilon=1e-5)

    models = [est1]
    for model in models:
        with pytest.warns(UserWarning, match='log-domain'):
            model.fit(X, y)
コード例 #4
0
def test_zero_alpha(gaussian_data):
    X, y = gaussian_data
    est0 = IndRewLasso(alpha=len(X) * [0.])
    est1 = IndLasso(alpha=len(X) * [0.])
    est2 = GroupLasso(alpha=0.)
    est3 = DirtyModel(alpha=0., beta=0.)
    est4 = MultiLevelLasso(alpha=0.)
    est5 = ReMTW(alpha=0., beta=0.)
    est6 = MTW(alpha=0., beta=0.)

    models = [est0, est1, est2, est3, est4, est5, est6]
    for model in models:
        with pytest.warns(UserWarning, match='LinearRegression'):
            model.fit(X, y)
コード例 #5
0
def test_slow_convergence(gaussian_data):
    X, y = gaussian_data
    max_iter = 1
    est0 = IndRewLasso(alpha=len(X) * [0.01], max_iter=max_iter)
    est1 = IndLasso(alpha=len(X) * [0.01], max_iter=max_iter)
    est2 = GroupLasso(alpha=0.01, max_iter=max_iter)
    est3 = DirtyModel(alpha=0.01, beta=0.01, max_iter=max_iter)
    est4 = MultiLevelLasso(alpha=0.01, max_iter=max_iter)
    est5 = ReMTW(alpha=0.01, beta=0.01, max_iter=max_iter)
    est6 = MTW(alpha=0.01, beta=0.01, max_iter=max_iter)

    models = [est0, est1, est2, est3, est4, est5, est6]
    for model in models:
        with pytest.warns(ConvergenceWarning, match='number of iterations'):
            model.fit(X, y)
コード例 #6
0
ファイル: test_mtw.py プロジェクト: vishalbelsare/mutar
def test_mtw_convolutions(identity_data, max_iter_reweighting):

    X, y = identity_data
    n_samples, n_features = X[0].shape
    width = int(n_features ** 0.5)
    Xty = np.array([xx.T.dot(yy) for xx, yy in zip(X, y)])
    beta_max = abs(Xty).max()
    beta = 0.2 * beta_max / n_samples
    M2d = utils.groundmetric2d(width, p=2, normed=True)
    Mimg = utils.groundmetric_img(width, p=2, normed=True)

    est_img = ReMTW(alpha=0.5, beta=beta, M=Mimg, gamma=1)
    est_img.fit(X, y)
    coef_img = est_img.coef_.flatten()

    est = ReMTW(alpha=0.5, beta=beta, M=M2d, gamma=1)
    est.fit(X, y)
    coef = est.coef_.flatten()

    assert_allclose(coef, coef_img, atol=1e-2)

    assert hasattr(est_img, 'is_fitted_')
コード例 #7
0
ファイル: plot_digits.py プロジェクト: vishalbelsare/mutar
largecoef[:, 4:19][:, :, 4:20] = c

f, axes = plt.subplots(1, n_tasks)
for ax, coef in zip(axes.T, largecoef):
    ax.imshow(np.log(coef.T + 0.1), cmap="hot")
    ax.set_xticks([])
    ax.set_yticks([])
plt.title("MTW")
plt.show()

##############################################################################
# Do the same thing with Reweighted MTW

mtw = ReMTW(M=M_,
            alpha=alpha,
            beta=beta,
            epsilon=epsilon,
            gamma=gamma,
            tol_reweighting=1e-6)
mtw.fit(Xcv, ycv)
coefs_ = mtw.coef_.copy()

ypred = np.argmax(Xvalid.dot(coefs_), axis=1)
errors = (ypred != yvalid).reshape(n_tasks, -1).mean(axis=1)

print(f"Classification error for predicting digits {tasks}:")
print(errors)

###############################################################################
# Imshow coefficients

largecoef = np.zeros((n_tasks, 24, 24))
コード例 #8
0
ファイル: test_mtw.py プロジェクト: vishalbelsare/mutar
def test_mtw_stabilization(gaussian_data, positive):

    X, y = gaussian_data
    n_samples, n_features = X[0].shape
    width = int(n_features ** 0.5)
    Xty = np.array([xx.T.dot(yy) for xx, yy in zip(X, y)])
    beta_max = abs(Xty).max()
    beta = 0.15 * beta_max / n_samples
    M2d = utils.groundmetric2d(width, p=2, normed=True)
    Mimg = utils.groundmetric_img(width, p=2, normed=True)

    est = ReMTW(alpha=0.2, beta=beta, M=Mimg, gamma=0.1, stable=True,
                positive=positive)
    est.fit(X, y)
    coef_img = est.coef_.flatten()

    est = ReMTW(alpha=0.2, beta=beta, M=M2d, gamma=0.1, positive=positive)
    est.fit(X, y)
    coef = est.coef_.flatten()

    est = ReMTW(alpha=0.2, beta=beta, M=M2d, gamma=0.1, stable=True,
                positive=positive)
    est.fit(X, y)
    coef_stable = est.coef_.flatten()

    assert_allclose(coef, coef_img, atol=1e-2)
    assert_allclose(coef_stable, coef_img, atol=1e-2)