def test_make_sparse_code_signal_warning():
    """Check the message for future deprecation."""
    warn_msg = "The default value of data_transposed will change from True to False"
    with pytest.warns(FutureWarning, match=warn_msg):
        make_sparse_coded_signal(n_samples=1,
                                 n_components=1,
                                 n_features=1,
                                 n_nonzero_coefs=1,
                                 random_state=0)
Пример #2
0
def plot_omp():
    n_components, n_features = 512, 100
    n_nonzero_coefs = 17

    # generate the data

    # y = Xw
    # |x|_0 = n_nonzero_coefs

    y, X, w = make_sparse_coded_signal(n_samples=1,
                                       n_components=n_components,
                                       n_features=n_features,
                                       n_nonzero_coefs=n_nonzero_coefs,
                                       random_state=0)

    idx, = w.nonzero()

    # distort the clean signal
    y_noisy = y + 0.05 * np.random.randn(len(y))

    # plot the sparse signal
    plt.figure(figsize=(7, 7))
    plt.subplot(4, 1, 1)
    plt.xlim(0, 512)
    plt.title("Sparse signal")
    plt.stem(idx, w[idx], use_line_collection=True)

    # plot the noise-free reconstruction
    omp = OrthogonalMatchingPursuit(n_nonzero_coefs=n_nonzero_coefs)
    omp.fit(X, y)
    coef = omp.coef_
    idx_r, = coef.nonzero()
    plt.subplot(4, 1, 2)
    plt.xlim(0, 512)
    plt.title("Recovered signal from noise-free measurements")
    plt.stem(idx_r, coef[idx_r], use_line_collection=True)

    # plot the noisy reconstruction
    omp.fit(X, y_noisy)
    coef = omp.coef_
    idx_r, = coef.nonzero()
    plt.subplot(4, 1, 3)
    plt.xlim(0, 512)
    plt.title("Recovered signal from noisy measurements")
    plt.stem(idx_r, coef[idx_r], use_line_collection=True)

    # plot the noisy reconstruction with number of non-zeros set by CV
    omp_cv = OrthogonalMatchingPursuitCV()
    omp_cv.fit(X, y_noisy)
    coef = omp_cv.coef_
    idx_r, = coef.nonzero()
    plt.subplot(4, 1, 4)
    plt.xlim(0, 512)
    plt.title("Recovered signal from noisy measurements with CV")
    plt.stem(idx_r, coef[idx_r], use_line_collection=True)

    plt.subplots_adjust(0.06, 0.04, 0.94, 0.90, 0.20, 0.38)
    plt.suptitle('Sparse signal recovery with Orthogonal Matching Pursuit',
                 fontsize=16)
    plt.show()
Пример #3
0
def initialize_dict(n_components, n_features):
    n_nonzero_coefs = 20
    w, D, y = make_sparse_coded_signal(n_samples=1,
                                       n_components=n_components,
                                       n_features=n_features,
                                       n_nonzero_coefs=n_nonzero_coefs)
    return D
Пример #4
0
def test_make_sparse_coded_signal():
    Y, D, X = make_sparse_coded_signal(n_samples=5, n_components=8, n_features=10, n_nonzero_coefs=3, random_state=0)
    assert_equal(Y.shape, (10, 5), "Y shape mismatch")
    assert_equal(D.shape, (10, 8), "D shape mismatch")
    assert_equal(X.shape, (8, 5), "X shape mismatch")
    for col in X.T:
        assert_equal(len(np.flatnonzero(col)), 3, "Non-zero coefs mismatch")
    assert_array_equal(np.dot(D, X), Y)
    assert_array_almost_equal(np.sqrt((D ** 2).sum(axis=0)), np.ones(D.shape[1]))
def test_make_sparse_coded_signal():
    Y, D, X = make_sparse_coded_signal(
        n_samples=5, n_components=8, n_features=10, n_nonzero_coefs=3, random_state=0
    )
    assert Y.shape == (10, 5), "Y shape mismatch"
    assert D.shape == (10, 8), "D shape mismatch"
    assert X.shape == (8, 5), "X shape mismatch"
    for col in X.T:
        assert len(np.flatnonzero(col)) == 3, "Non-zero coefs mismatch"
    assert_array_almost_equal(np.dot(D, X), Y)
    assert_array_almost_equal(np.sqrt((D ** 2).sum(axis=0)), np.ones(D.shape[1]))
def test_make_sparse_coded_signal():
    Y, D, X = make_sparse_coded_signal(
        n_samples=5,
        n_components=8,
        n_features=10,
        n_nonzero_coefs=3,
        random_state=0,
        data_transposed=False,
    )
    assert Y.shape == (5, 10), "Y shape mismatch"
    assert D.shape == (8, 10), "D shape mismatch"
    assert X.shape == (5, 8), "X shape mismatch"
    for row in X:
        assert len(np.flatnonzero(row)) == 3, "Non-zero coefs mismatch"
    assert_allclose(Y, X @ D)
    assert_allclose(np.sqrt((D**2).sum(axis=1)), np.ones(D.shape[0]))
def test_make_sparse_coded_signal_transposed():
    Y, D, X = make_sparse_coded_signal(
        n_samples=5,
        n_components=8,
        n_features=10,
        n_nonzero_coefs=3,
        random_state=0,
        data_transposed=True,
    )
    assert Y.shape == (10, 5), "Y shape mismatch"
    assert D.shape == (10, 8), "D shape mismatch"
    assert X.shape == (8, 5), "X shape mismatch"
    for col in X.T:
        assert len(np.flatnonzero(col)) == 3, "Non-zero coefs mismatch"
    assert_allclose(Y, D @ X)
    assert_allclose(np.sqrt((D**2).sum(axis=0)), np.ones(D.shape[1]))
Пример #8
0
def sparse_signal(noisy_data, n_coeffs, learning_ratio):

    n_samples, n_features = noisy_data.shape  # p X n
    n_components = int(learning_ratio * noisy_data.shape[0])  #k
    # x = D*alpha, D = initial_dict
    # alpha = sparse_matrix
    x, init_dict, sparse_matrix = make_sparse_coded_signal(
        n_samples=n_samples,
        n_components=n_components,
        n_features=n_features,
        n_nonzero_coefs=n_coeffs,
        random_state=0)
    indexes = np.random.random_integers(0, noisy_data.shape[1] - 1,
                                        n_components)
    #pdb.set_trace()
    init_dict = noisy_data[indexes, :]

    return init_dict, sparse_matrix
Пример #9
0
def make_sparse_coded_signal(n_samples, n_components, n_features,
                             n_nonzero_coefs, random_state=None,
                             pandas_mode=False):

    y, X, w = ds.make_sparse_coded_signal(n_samples, n_components, n_features,
                                          n_nonzero_coefs, random_state)
    if not pandas_mode:
        return y, X, w
    else:
        comps = pd.Index(['comp-{}'.format(i) for i in range(n_components)],
                         name='components')
        feats = pd.Index(['feat-{}'.format(i) for i in range(n_features)],
                         name='features')
        samps = pd.Index(['sample-{}'.format(i) for i in range(n_samples)],
                         name='samples')

        y = pd.DataFrame(y, index=feats, columns=samps)
        X = pd.DataFrame(X, index=feats, columns=comps)
        w = pd.DataFrame(w, index=comps, columns=samps)
        return y, X, w
Пример #10
0
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import ignore_warnings


from sklearn.linear_model import (orthogonal_mp, orthogonal_mp_gram,
                                  OrthogonalMatchingPursuit,
                                  OrthogonalMatchingPursuitCV,
                                  LinearRegression)
from sklearn.utils.fixes import count_nonzero
from sklearn.utils import check_random_state
from sklearn.datasets import make_sparse_coded_signal

n_samples, n_features, n_nonzero_coefs, n_targets = 20, 30, 5, 3
y, X, gamma = make_sparse_coded_signal(n_targets, n_features, n_samples,
                                       n_nonzero_coefs, random_state=0)
G, Xy = np.dot(X.T, X), np.dot(X.T, y)
# this makes X (n_samples, n_features)
# and y (n_samples, 3)


def test_correct_shapes():
    assert_equal(orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5).shape,
                 (n_features,))
    assert_equal(orthogonal_mp(X, y, n_nonzero_coefs=5).shape,
                 (n_features, 3))


def test_correct_shapes_gram():
    assert_equal(orthogonal_mp_gram(G, Xy[:, 0], n_nonzero_coefs=5).shape,
                 (n_features,))
Пример #11
0
def compute_bench(samples_range, features_range):

    it = 0

    results = dict()
    lars = np.empty((len(features_range), len(samples_range)))
    lars_gram = lars.copy()
    omp = lars.copy()
    omp_gram = lars.copy()

    max_it = len(samples_range) * len(features_range)
    for i_s, n_samples in enumerate(samples_range):
        for i_f, n_features in enumerate(features_range):
            it += 1
            n_informative = n_features / 10
            print('====================')
            print('Iteration %03d of %03d' % (it, max_it))
            print('====================')
            # dataset_kwargs = {
            #     'n_train_samples': n_samples,
            #     'n_test_samples': 2,
            #     'n_features': n_features,
            #     'n_informative': n_informative,
            #     'effective_rank': min(n_samples, n_features) / 10,
            #     #'effective_rank': None,
            #     'bias': 0.0,
            # }
            dataset_kwargs = {
                'n_samples': 1,
                'n_components': n_features,
                'n_features': n_samples,
                'n_nonzero_coefs': n_informative,
                'random_state': 0
            }
            print("n_samples: %d" % n_samples)
            print("n_features: %d" % n_features)
            y, X, _ = make_sparse_coded_signal(**dataset_kwargs)
            X = np.asfortranarray(X)

            gc.collect()
            print("benchmarking lars_path (with Gram):", end='')
            sys.stdout.flush()
            tstart = time()
            G = np.dot(X.T, X)  # precomputed Gram matrix
            Xy = np.dot(X.T, y)
            lars_path_gram(Xy=Xy,
                           Gram=G,
                           n_samples=y.size,
                           max_iter=n_informative)
            delta = time() - tstart
            print("%0.3fs" % delta)
            lars_gram[i_f, i_s] = delta

            gc.collect()
            print("benchmarking lars_path (without Gram):", end='')
            sys.stdout.flush()
            tstart = time()
            lars_path(X, y, Gram=None, max_iter=n_informative)
            delta = time() - tstart
            print("%0.3fs" % delta)
            lars[i_f, i_s] = delta

            gc.collect()
            print("benchmarking orthogonal_mp (with Gram):", end='')
            sys.stdout.flush()
            tstart = time()
            orthogonal_mp(X, y, precompute=True, n_nonzero_coefs=n_informative)
            delta = time() - tstart
            print("%0.3fs" % delta)
            omp_gram[i_f, i_s] = delta

            gc.collect()
            print("benchmarking orthogonal_mp (without Gram):", end='')
            sys.stdout.flush()
            tstart = time()
            orthogonal_mp(X,
                          y,
                          precompute=False,
                          n_nonzero_coefs=n_informative)
            delta = time() - tstart
            print("%0.3fs" % delta)
            omp[i_f, i_s] = delta

    results['time(LARS) / time(OMP)\n (w/ Gram)'] = (lars_gram / omp_gram)
    results['time(LARS) / time(OMP)\n (w/o Gram)'] = (lars / omp)
    return results
Пример #12
0
from sklearn.linear_model import OrthogonalMatchingPursuit
from sklearn.linear_model import OrthogonalMatchingPursuitCV
from sklearn.datasets import make_sparse_coded_signal

n_components, n_features = 512, 100
n_nonzero_coefs = 17

# generate the data
###################

# y = Xw
# |x|_0 = n_nonzero_coefs

y, X, w = make_sparse_coded_signal(n_samples=1,
                                   n_components=n_components,
                                   n_features=n_features,
                                   n_nonzero_coefs=n_nonzero_coefs,
                                   random_state=0)

idx, = w.nonzero()

# distort the clean signal
##########################
y_noisy = y + 0.05 * np.random.randn(len(y))

# plot the sparse signal
########################
pl.figure(figsize=(7, 7))
pl.subplot(4, 1, 1)
pl.xlim(0, 512)
pl.title("Sparse signal")
Пример #13
0
    data -= m;

    
    code = ompcode(D, data, 2).T;
    patches = np.dot(code, D.T);
    patches += m;
    patches = np.array(patches).reshape(len(data), *patch_size)
    ret = face.copy();
    ret[:, width // 2:] = reconstruct_from_patches_2d(patches, (height, width // 2))
    plt.figure()
    plt.imshow(ret, cmap=plt.cm.gray, interpolation='nearest')
    plt.show();

y, X, w = make_sparse_coded_signal(n_samples=100,
                                   n_components=8,
                                   n_features=16,
                                   n_nonzero_coefs=2,
                                   random_state=0)

face = mat(scipy.misc.face(gray=True))
face = face / 255.0;
face = face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2] + face[1::2, 1::2]
face /= 4.0
height, width = face.shape;

if halfNoise:
    face[:, width // 2:] += 0.075 * np.random.randn(height, width // 2)
else:
    face += 0.075 * np.random.randn(height, width);
    
plt.figure()
Пример #14
0
consensusEnabled = 1  # value 0 or 1
td = 50
K = 3
tp = 2
tc = 5

if tc > 1:
    wR = 1 / (tc * podQuantity)  # Weight Ratio - less than 1 and more than 0
else:
    wR = 0.5

# Generate new data
Y20000, D2, X2 = make_sparse_coded_signal(n_samples=Q,
                                          n_components=N,
                                          n_features=M,
                                          n_nonzero_coefs=K,
                                          random_state=0)
'''
# Load saved data
D = []
Y = []

with open('consensusData.json') as json_file:
    jsonData = json.load(json_file)
    for elementD in jsonData['D']:
        D.append(np.matrix(elementD))
    Y = np.array(jsonData['Y'])
'''
'''
# Split Y into parts for each pod
Пример #15
0
from sklearn.linear_model import (
    orthogonal_mp,
    orthogonal_mp_gram,
    OrthogonalMatchingPursuit,
    OrthogonalMatchingPursuitCV,
    LinearRegression,
)
from sklearn.utils import check_random_state
from sklearn.datasets import make_sparse_coded_signal

n_samples, n_features, n_nonzero_coefs, n_targets = 25, 35, 5, 3
y, X, gamma = make_sparse_coded_signal(
    n_samples=n_targets,
    n_components=n_features,
    n_features=n_samples,
    n_nonzero_coefs=n_nonzero_coefs,
    random_state=0,
    data_transposed=True,
)
# Make X not of norm 1 for testing
X *= 10
y *= 10
G, Xy = np.dot(X.T, X), np.dot(X.T, y)
# this makes X (n_samples, n_features)
# and y (n_samples, 3)


# FIXME: 'normalize' to set to False in 1.2 and removed in 1.4
@pytest.mark.parametrize(
    "OmpModel", [OrthogonalMatchingPursuit, OrthogonalMatchingPursuitCV])
@pytest.mark.parametrize("normalize, n_warnings", [(True, 0), (False, 0),
Пример #16
0
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_array_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_warns
from sklearn.utils.testing import ignore_warnings


from sklearn.linear_model import (orthogonal_mp, orthogonal_mp_gram,
                                  OrthogonalMatchingPursuit,
                                  OrthogonalMatchingPursuitCV,
                                  LinearRegression)
from sklearn.utils import check_random_state
from sklearn.datasets import make_sparse_coded_signal

n_samples, n_features, n_nonzero_coefs, n_targets = 20, 30, 5, 3
y, X, gamma = make_sparse_coded_signal(n_targets, n_features, n_samples,
                                       n_nonzero_coefs, random_state=0)
G, Xy = np.dot(X.T, X), np.dot(X.T, y)
# this makes X (n_samples, n_features)
# and y (n_samples, 3)


def test_correct_shapes():
    assert_equal(orthogonal_mp(X, y[:, 0], n_nonzero_coefs=5).shape,
                 (n_features,))
    assert_equal(orthogonal_mp(X, y, n_nonzero_coefs=5).shape,
                 (n_features, 3))


def test_correct_shapes_gram():
    assert_equal(orthogonal_mp_gram(G, Xy[:, 0], n_nonzero_coefs=5).shape,
                 (n_features,))
Пример #17
0
from sklearn.linear_model import OrthogonalMatchingPursuit
from sklearn.linear_model import OrthogonalMatchingPursuitCV
from sklearn.datasets import make_sparse_coded_signal

n_components, n_features = 512, 100
n_nonzero_coefs = 17

# generate the data
###################

# y = Xw
# |x|_0 = n_nonzero_coefs

y, X, w = make_sparse_coded_signal(n_samples=1,
                                   n_components=n_components,
                                   n_features=n_features,
                                   n_nonzero_coefs=n_nonzero_coefs,
                                   random_state=0)

idx, = w.nonzero()

# distort the clean signal
##########################
y_noisy = y + 0.05 * np.random.randn(len(y))

# plot the sparse signal
########################
plt.figure(figsize=(7, 7))
plt.subplot(4, 1, 1)
plt.xlim(0, 512)
plt.title("Sparse signal")
"""OMP.

Orthogonal Matching Pursuit.
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.metrics import r2_score
from sklearn.datasets import make_sparse_coded_signal

if __name__ == "__main__":
    print("Generating data...")
    ncomp, nf, nncoef = 256, 10000, 32
    y, X, w = make_sparse_coded_signal(n_samples=1,
                                       n_components=ncomp,
                                       n_features=nf,
                                       n_nonzero_coefs=nncoef)
    idx, = w.nonzero()
    y = y + 0.02 * np.random.randn(len(y))
    y = y.flatten()
    X_train, X_test = X[nf // 2:], X[:nf // 2]
    y_train, y_test = y[nf // 2:], y[:nf // 2]
    print(X, y)
    print("Fitting model...")
    omp = linear_model.OrthogonalMatchingPursuit(n_nonzero_coefs=nncoef)
    omp.fit(X_train, y_train)
    print("R2 score: {0}".format(r2_score(y_test, omp.predict(X_test))))

    plt.scatter(np.arange(nf // 2), y_train, color="purple")
    plt.scatter(np.arange(nf // 2) + (nf // 2), y_test, color="red")
    plt.plot(np.arange(nf // 2), omp.predict(X_train), color="purple")
Пример #19
0
# TEST
limit_memory(datasize)

cvx_time, prox_time, sspg_time = [], [], []
# atoms = range(300, 301, 1)
atoms = range(80, 500, 20)
fname = '{0}-{1}-{2}-{3}'.format(prefix, atoms.start, atoms.stop, atoms.step)
for n_components in atoms:
    print("----------")
    print("atoms", n_components)
    n_features = round(n_components / 20)
    # Generate sparse signals using a dictionary
    sample, dictionary, codes = make_sparse_coded_signal(
        n_samples=n_samples,
        n_components=n_components,
        n_features=n_features,
        n_nonzero_coefs=n_nonzero_coefs,
        random_state=0)
    dictionary = np.eye(n_features)
    Delta = np.random.standard_normal((n_components, n_features))

    start = timer()
    cvx_x = cosr_cvx(sample, Delta, lam)
    end = timer()
    cvx_time.append(end-start)
    print("CVX time: ", end - start)
    # print("CVX: Sparsity", np.count_nonzero(Delta@cvx_x),
    #         "solution", Delta@cvx_x)

    # x0 = np.ones(dictionary.shape[1])
    # start = timer()
from sklearn import datasets
import matplotlib.pyplot as plt

# make_sparse_coded_signal data
data, dic, code = datasets.make_sparse_coded_signal(n_samples=10,
                                                    n_components=5,
                                                    n_features=3,
                                                    n_nonzero_coefs=1,
                                                    random_state=None)
print('data = ')
print(data)
print('dic = ')
print(dic)
print('code = ')
print(code)