def test_bread_cp():
    """
    Test CANDECOMP/PARFAC decomposition using bread dataset.
    """
    X, meta = load_bread()
    assert_raises(ValueError, cp, X)
    U1 = cp(X, 2, init_type="hosvd")
    U2 = _cp3(X, 2, tol=1E-4, max_iter=500, init_type="hosvd")
    for n, i in enumerate(U1):
        assert_almost_equal(U1[n], U2[n])
def test_bread_cp():
    """
    Test CANDECOMP/PARFAC decomposition using bread dataset.
    """
    X, meta = load_bread()
    assert_raises(ValueError, cp, X)
    U1 = cp(X, 2, init_type="hosvd")
    U2 = _cp3(X, 2, tol=1E-4, max_iter=500, init_type="hosvd")
    for n, i in enumerate(U1):
        assert_almost_equal(U1[n], U2[n])
def test_generated_cp():
    """
    Test CANDECOMP/PARFAC decomposition. Problem from
    http://issnla2010.ba.cnr.it/DecompositionsI.pdf
    """
    rs = np.random.RandomState(1999)
    X = .7 * rs.rand(2, 4, 3) + .25 * rs.rand(2, 4, 3)
    assert_raises(ValueError, cp, X)
    U1 = cp(X, 2, init_type="hosvd")
    U2 = _cp3(X, 2, tol=1E-4, max_iter=500, init_type="hosvd")
    for n, i in enumerate(U1):
        assert_almost_equal(U1[n], U2[n])
def test_generated_cp():
    """
    Test CANDECOMP/PARFAC decomposition. Problem from
    http://issnla2010.ba.cnr.it/DecompositionsI.pdf
    """
    rs = np.random.RandomState(1999)
    X = .7 * rs.rand(2, 4, 3) + .25 * rs.rand(2, 4, 3)
    assert_raises(ValueError, cp, X)
    U1 = cp(X, 2, init_type="hosvd")
    U2 = _cp3(X, 2, tol=1E-4, max_iter=500, init_type="hosvd")
    for n, i in enumerate(U1):
        assert_almost_equal(U1[n], U2[n])
Example #5
0
======================================================
CANDECOMP/PARAFAC decomposition for analyzing MEG data
======================================================

MEG data is notriously complex. An SVD over the flattened matrix does not give
any useful or interpretable results. Using the extra structural information of
the dataset, a CANDECOMP/PARAFAC decomposition is able to pull out interesting
basis functions from this signal.

The waves at 50Hz represent the power line interference (recordings were done
in the UK), while the longer 10Hz waves appear to represent the "alpha wave"
of the brain.

The aperiodic signals represent the response of the brain to the presentation of
an image of a face, or an image of a scrambled face.

"""
print(__doc__)
from tensorlib.datasets import fetch_decmeg
from tensorlib.decomposition import cp
import matplotlib.pyplot as plt
import time

X, meta = fetch_decmeg()
X = X[:, :, 125:250]
t0 = time.time()
U0, U1, U2 = cp(X, n_components=10, init_type="hosvd")
plt.plot(U2)
plt.title("Rank 10 decomposition of MEG data, time axis")
plt.show()
Example #6
0
"""An example of using tensorlib and scipy to decompose a builtin dataset."""
from tensorlib.datasets import load_bread
from tensorlib.decomposition import cp
from scipy import linalg
import numpy as np
import matplotlib.pyplot as plt

X, meta = load_bread()
U0, U1, U2 = cp(X, n_components=2, random_state=1999)
X0_flat = X.reshape(X.shape[0], -1)
U, S, V = linalg.svd(X0_flat, full_matrices=False)
svd_proj = V[:, :2]
t1 = np.dot(X0_flat.T, U0).T
t2 = np.dot(X0_flat.T, svd_proj).T
plt.figure()
plt.title("Tensor decomposition of bread data")
plt.scatter(t1[0, :], t1[1, :], color="darkred")
plt.figure()
plt.title("SVD of bread data")
plt.scatter(t2[0, :], t2[1, :], color="steelblue")
plt.show()