Beispiel #1
0
def test_warm_start_path():
    X, y, coef = make_user_item_regression(label_stdev=.4)
    X = sp.csc_matrix(X)

    rank = 4
    seed = 333
    step_size = 1
    n_iter = 10
    l2_reg_w = 0
    l2_reg_V = 0

    fm = als.FMRegression(n_iter=0,
                          l2_reg_w=l2_reg_w,
                          l2_reg_V=l2_reg_V,
                          rank=rank,
                          random_state=seed)

    rmse = []
    for _ in range(1, n_iter):
        fm.fit(X, y, n_more_iter=step_size)
        rmse.append(np.sqrt(mean_squared_error(fm.predict(X), y)))

    print('------- restart ----------')
    rmse_re = []
    for i in range(1, n_iter):
        fm = als.FMRegression(n_iter=i,
                              l2_reg_w=l2_reg_w,
                              l2_reg_V=l2_reg_V,
                              rank=rank,
                              random_state=seed)
        fm.fit(X, y)
        rmse_re.append(np.sqrt(mean_squared_error(fm.predict(X), y)))

    assert len(rmse) == len(rmse_re)
    assert_almost_equal(rmse, rmse_re)
Beispiel #2
0
def test_als_warm_start():
    X, y, coef = make_user_item_regression(label_stdev=0)
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.33,
                                                        random_state=42)
    X_train = sp.csc_matrix(X_train)
    X_test = sp.csc_matrix(X_test)

    fm = als.FMRegression(n_iter=10, l2_reg_w=0, l2_reg_V=0, rank=2)
    fm.fit(X_train, y_train)
    y_pred = fm.predict(X_test)
    error_10_iter = mean_squared_error(y_pred, y_test)

    fm = als.FMRegression(n_iter=5, l2_reg_w=0, l2_reg_V=0, rank=2)
    fm.fit(X_train, y_train)
    print(fm.iter_count)
    y_pred = fm.predict(X_test)
    error_5_iter = mean_squared_error(y_pred, y_test)

    fm.fit(sp.csc_matrix(X_train), y_train, n_more_iter=5)
    print(fm.iter_count)
    y_pred = fm.predict(X_test)
    error_5_iter_plus_5 = mean_squared_error(y_pred, y_test)

    print(error_5_iter, error_5_iter_plus_5, error_10_iter)

    assert error_10_iter == error_5_iter_plus_5
Beispiel #3
0
def test_fm_regression():
    w0, w, V, y, X = get_test_problem()

    fm = als.FMRegression(n_iter=1000, l2_reg_w=0, l2_reg_V=0, rank=2)
    fm.fit(X, y)
    y_pred = fm.predict(X)
    assert_almost_equal(y_pred, y, 3)
    # check different size
    fm = als.FMRegression(n_iter=1000, l2_reg_w=0, l2_reg_V=0, rank=5)
    X_big = sp.hstack([X, X])
    fm.fit(X_big, y)
    y_pred = fm.predict(X_big[:2, ])
Beispiel #4
0
def _test_fm_regression_only_w0():
    X, y = get_small_data()

    fm = als.FMRegression(n_iter=0, l2_reg_w=0, l2_reg_V=0, rank=0)
    fm.ignore_w = True
    fm.w0_ = 2
    fm.fit(X, y)
    assert_almost_equal(fm.w0_, 2, 6)

    fm = als.FMRegression(n_iter=1, l2_reg_w=0, l2_reg_V=0, rank=0)
    fm.ignore_w = True
    fm.w0_ = 2
    fm.fit(X, y)
    assert_almost_equal(fm.w0_, 4466.6666666666661, 6)
Beispiel #5
0
def test_clone():
    from sklearn.base import clone

    a = als.FMRegression()
    b = clone(a)
    assert a.get_params() == b.get_params()

    a = als.FMClassification()
    b = clone(a)
    assert a.get_params() == b.get_params()
Beispiel #6
0
def test_fm_regression_reg_V():
    X, y = get_small_data()

    fm = als.FMRegression(n_iter=10,
                          l2_reg_w=1,
                          l2_reg_V=1,
                          rank=4,
                          random_state=123)
    fm.fit(X, y)
    y_pred_reg1 = fm.predict(X)

    fm = als.FMRegression(n_iter=10,
                          l2_reg_w=1,
                          l2_reg_V=100,
                          rank=4,
                          random_state=123)
    fm.fit(X, y)
    y_pred_reg100 = fm.predict(X)

    # different reg values should result in differnt predictions
    assert np.any(np.not_equal(y_pred_reg1, y_pred_reg100))
Beispiel #7
0
def _test_raise_when_input_is_dense():
    fm = als.FMRegression(n_iter=0, l2_reg_w=0, l2_reg_V=0, rank=0)
    X = np.arange(3, 4, dtype=np.float64)
    y = np.arange(3, dtype=np.float64)
    fm.fit(X, y)