Exemplo n.º 1
0
def test_dict_fact_normalize():
    # Generate some toy data.
    rng = np.random.RandomState(0)
    U = rng.rand(50, 3)
    V = rng.rand(3, 20)
    X = np.dot(U, V)

    mf = DictMF(n_components=3,
                n_epochs=1,
                alpha=1e-3,
                random_state=0,
                verbose=0,
                normalize=True)

    mf.fit(X)

    Y = np.dot(mf.P_.T, mf.Q_)
    Y += mf.col_mean_[np.newaxis, :]
    Y += mf.row_mean_[:, np.newaxis]
    Y2 = mf.predict(X).toarray()

    assert_array_almost_equal(Y, Y2)

    rmse = np.sqrt(np.mean((X - Y)**2))
    rmse2 = mf.score(X)

    assert_almost_equal(rmse, rmse2)
Exemplo n.º 2
0
def test_dummy_axis1():
    est = Dummy()
    est.fit(X)
    X_predicted = est.predict(X).toarray()
    X_expected = [[2, 0, 0, 2], [3.5, 0, 3.5, 0], [0, 3.5, 3.5, 0],
                  [0, 0, 2, 0], [1, 0, 0, 0]]
    assert_array_almost_equal(X_predicted, X_expected)
Exemplo n.º 3
0
def test_matrix_fact_cd():
    # Generate some toy data.
    rng = np.random.RandomState(0)
    U = rng.rand(50, 3)
    V = rng.rand(3, 20)
    X = np.dot(U, V)

    mf = DictMF(n_components=3,
                n_epochs=3,
                alpha=1e-3,
                random_state=0,
                verbose=0,
                normalize=False)

    mf.fit(X)

    Y = np.dot(mf.P_.T, mf.Q_)
    Y2 = mf.predict(X).toarray()

    assert_array_almost_equal(Y, Y2)

    rmse = np.sqrt(np.mean((X - Y)**2))
    rmse2 = mf.score(X)

    assert_almost_equal(rmse, rmse2)
Exemplo n.º 4
0
def test_dummy_axis0():
    est = Dummy(axis=0)
    est.fit(X)
    X_predicted = est.predict(X).toarray()
    X_expected = [[2, 0, 0, 1], [2, 0, 10. / 3, 0], [0, 4, 10. / 3, 0],
                  [0, 0, 10. / 3, 0], [2, 0, 0, 0]]
    assert_array_almost_equal(X_predicted, X_expected)
Exemplo n.º 5
0
def test_dummy_axis1():
    est = Dummy()
    est.fit(X)
    X_predicted = est.predict(X).toarray()
    X_expected = [[2, 0, 0, 2],
                  [3.5, 0, 3.5, 0],
                  [0, 3.5, 3.5, 0],
                  [0, 0, 2, 0],
                  [1, 0, 0, 0]]
    assert_array_almost_equal(X_predicted, X_expected)
Exemplo n.º 6
0
def test_dummy_axis0():
    est = Dummy(axis=0)
    est.fit(X)
    X_predicted = est.predict(X).toarray()
    X_expected = [[2, 0, 0, 1],
                  [2, 0, 10./3, 0],
                  [0, 4, 10./3, 0],
                  [0, 0, 10./3, 0],
                  [2, 0, 0, 0]]
    assert_array_almost_equal(X_predicted, X_expected)
Exemplo n.º 7
0
def test_center_std_axis0():
    X_t = X.copy()

    mean = _mean_axis0(X)
    std = _std_axis0(X)

    for i in xrange(X.shape[0]):
        for j in xrange(X.shape[1]):
            if X[i, j] == 0:
                continue
            X_t[i, j] -= mean[j]
            X_t[i, j] /= std[j]

    scaler = StandardScaler(axis=0, with_std=True)
    X_t2 = scaler.fit_transform(X)

    assert_array_almost_equal(X_t, X_t2.toarray())

    X2 = scaler.inverse_transform(X_t2)
    assert_array_almost_equal(X, X2.toarray())
Exemplo n.º 8
0
def test_center_std_axis0():
    X_t = X.copy()

    mean = _mean_axis0(X)
    std = _std_axis0(X)

    for i in xrange(X.shape[0]):
        for j in xrange(X.shape[1]):
            if X[i, j] == 0:
                continue
            X_t[i, j] -= mean[j]
            X_t[i, j] /= std[j]

    scaler = StandardScaler(axis=0, with_std=True)
    X_t2 = scaler.fit_transform(X)

    assert_array_almost_equal(X_t, X_t2.toarray())

    X2 = scaler.inverse_transform(X_t2)
    assert_array_almost_equal(X, X2.toarray())
Exemplo n.º 9
0
def test_matrix_fact_cd():
    # Generate some toy data.
    rng = np.random.RandomState(0)
    U = rng.rand(50, 3)
    V = rng.rand(3, 20)
    X = np.dot(U, V)

    mf = ExplicitMF(n_components=3, max_iter=10, alpha=1e-3, random_state=0,
                    verbose=0)

    mf.fit(X)

    Y = np.dot(mf.P_, mf.Q_)
    Y2 = mf.predict(X).toarray()

    assert_array_almost_equal(Y, Y2)

    rmse = np.sqrt(np.mean((X - Y) ** 2))
    rmse2 = mf.score(X)

    assert_almost_equal(rmse, rmse2)
Exemplo n.º 10
0
def test_dict_fact_normalize():
    # Generate some toy data.
    rng = np.random.RandomState(0)
    U = rng.rand(50, 3)
    V = rng.rand(3, 20)
    X = np.dot(U, V)

    mf = DictMF(n_components=3, n_epochs=1, alpha=1e-3, random_state=0,
                verbose=0, normalize=True)

    mf.fit(X)

    Y = np.dot(mf.P_.T, mf.Q_)
    Y += mf.col_mean_[np.newaxis, :]
    Y += mf.row_mean_[:, np.newaxis]
    Y2 = mf.predict(X).toarray()

    assert_array_almost_equal(Y, Y2)

    rmse = np.sqrt(np.mean((X - Y) ** 2))
    rmse2 = mf.score(X)

    assert_almost_equal(rmse, rmse2)
Exemplo n.º 11
0
def test_mean_axis0():
    expected = [2, 4, 10. / 3, 1]
    for func in (sp.csr_matrix, sp.csc_matrix, sp.coo_matrix):
        means = mean(func(X, dtype=np.float64), axis=0)
    assert_array_almost_equal(means, expected)
Exemplo n.º 12
0
def test_mean_axis1():
    expected = [2, 3.5, 3.5, 2, 1]
    for func in (sp.csr_matrix, sp.csc_matrix, sp.coo_matrix):
        means = mean(func(X, dtype=np.float64))
        assert_array_almost_equal(means, expected)
Exemplo n.º 13
0
def test_mean_axis0():
    expected = [2, 4, 10./3, 1]
    for func in (sp.csr_matrix, sp.csc_matrix, sp.coo_matrix):
        means = mean(func(X, dtype=np.float64), axis=0)
    assert_array_almost_equal(means, expected)
Exemplo n.º 14
0
def test_mean_axis1():
    expected = [2, 3.5, 3.5, 2, 1]
    for func in (sp.csr_matrix, sp.csc_matrix, sp.coo_matrix):
        means = mean(func(X, dtype=np.float64))
        assert_array_almost_equal(means, expected)