Beispiel #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)
Beispiel #2
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)
Beispiel #3
0
def test_grid_search():
    cv = ShuffleSplit(n_iter=5, random_state=0)
    mf = ExplicitMF(n_components=3, max_iter=10, random_state=0)
    param_grid = {"alpha": [0.1, 1.0, 10]}
    gcv = GridSearchCV(mf, param_grid, cv)
    gcv.fit(X)

    assert_equal(gcv.best_estimator_.alpha, 0.1)
    assert_equal(gcv.best_params_, {"alpha": 0.1})

    mf = ExplicitMF(alpha=0.1, n_components=3, max_iter=10, random_state=0)
    mf.fit(X)

    assert_almost_equal(mf.score(X), gcv.score(X))
Beispiel #4
0
def test_grid_search():
    cv = ShuffleSplit(n_iter=5, random_state=0)
    mf = ExplicitMF(n_components=3, max_iter=10, random_state=0)
    param_grid = {"alpha": [0.1, 1.0, 10]}
    gcv = GridSearchCV(mf, param_grid, cv)
    gcv.fit(X)

    assert_equal(gcv.best_estimator_.alpha, 0.1)
    assert_equal(gcv.best_params_, {"alpha": 0.1})

    mf = ExplicitMF(alpha=0.1, n_components=3, max_iter=10, random_state=0)
    mf.fit(X)

    assert_almost_equal(mf.score(X), gcv.score(X))
Beispiel #5
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)
Beispiel #6
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)