Exemplo n.º 1
0
def test_nmf_regularization():
    # Test the effect of L1 and L2 regularizations
    n_samples = 6
    n_features = 5
    n_components = 3
    rng = np.random.mtrand.RandomState(42)
    X = np.abs(rng.randn(n_samples, n_features))

    # L1 regularization should increase the number of zeros
    l1_ratio = 1.
    for solver in ['cd', 'mu']:
        regul = nmf.NMF(n_components=n_components,
                        solver=solver,
                        alpha=0.5,
                        l1_ratio=l1_ratio,
                        random_state=42)
        model = nmf.NMF(n_components=n_components,
                        solver=solver,
                        alpha=0.,
                        l1_ratio=l1_ratio,
                        random_state=42)

        W_regul = regul.fit_transform(X)
        W_model = model.fit_transform(X)

        H_regul = regul.components_
        H_model = model.components_

        W_regul_n_zeros = W_regul[W_regul == 0].size
        W_model_n_zeros = W_model[W_model == 0].size
        H_regul_n_zeros = H_regul[H_regul == 0].size
        H_model_n_zeros = H_model[H_model == 0].size

        assert W_regul_n_zeros > W_model_n_zeros
        assert H_regul_n_zeros > H_model_n_zeros

    # L2 regularization should decrease the mean of the coefficients
    l1_ratio = 0.
    for solver in ['cd', 'mu']:
        regul = nmf.NMF(n_components=n_components,
                        solver=solver,
                        alpha=0.5,
                        l1_ratio=l1_ratio,
                        random_state=42)
        model = nmf.NMF(n_components=n_components,
                        solver=solver,
                        alpha=0.,
                        l1_ratio=l1_ratio,
                        random_state=42)

        W_regul = regul.fit_transform(X)
        W_model = model.fit_transform(X)

        H_regul = regul.components_
        H_model = model.components_

        assert W_model.mean() > W_regul.mean()
        assert H_model.mean() > H_regul.mean()
Exemplo n.º 2
0
def test_nmf_regularization(solver):
    # Test the effect of L1 and L2 regularizations
    n_samples = 6
    n_features = 5
    n_components = 3
    rng = np.random.mtrand.RandomState(42)
    X = np.abs(rng.randn(n_samples, n_features))

    # FIXME : should be removed in 1.1
    init = "nndsvda"
    # L1 regularization should increase the number of zeros
    l1_ratio = 1.0

    regul = nmf.NMF(
        n_components=n_components,
        solver=solver,
        alpha_W=0.5,
        l1_ratio=l1_ratio,
        random_state=42,
        init=init,
    )
    model = nmf.NMF(
        n_components=n_components,
        solver=solver,
        alpha_W=0.0,
        l1_ratio=l1_ratio,
        random_state=42,
        init=init,
    )

    W_regul = regul.fit_transform(X)
    W_model = model.fit_transform(X)

    H_regul = regul.components_
    H_model = model.components_

    W_regul_n_zeros = W_regul[W_regul == 0].size
    W_model_n_zeros = W_model[W_model == 0].size
    H_regul_n_zeros = H_regul[H_regul == 0].size
    H_model_n_zeros = H_model[H_model == 0].size

    assert W_regul_n_zeros > W_model_n_zeros
    assert H_regul_n_zeros > H_model_n_zeros

    # L2 regularization should decrease the mean of the coefficients
    l1_ratio = 0.0

    regul = nmf.NMF(
        n_components=n_components,
        solver=solver,
        alpha_W=0.5,
        l1_ratio=l1_ratio,
        random_state=42,
        init=init,
    )
    model = nmf.NMF(
        n_components=n_components,
        solver=solver,
        alpha_W=0.0,
        l1_ratio=l1_ratio,
        random_state=42,
        init=init,
    )

    W_regul = regul.fit_transform(X)
    W_model = model.fit_transform(X)

    H_regul = regul.components_
    H_model = model.components_

    assert (linalg.norm(W_model))**2.0 + (linalg.norm(H_model))**2.0 > (
        linalg.norm(W_regul))**2.0 + (linalg.norm(H_regul))**2.0