コード例 #1
0
def test_matrix_types():
    no_users, no_items = 10, 100
    no_features = 20

    for mattype in mattypes:
        for dtype in dtypes:
            train = mattype((no_users, no_items), dtype=dtype)

            user_features = mattype((no_users, no_features), dtype=dtype)
            item_features = mattype((no_items, no_features), dtype=dtype)

            model = LightFM()
            model.fit_partial(train,
                              user_features=user_features,
                              item_features=item_features)

            model.predict(
                np.random.randint(0, no_users, 10).astype(np.int32),
                np.random.randint(0, no_items, 10).astype(np.int32),
                user_features=user_features,
                item_features=item_features,
            )

            model.predict_rank(train,
                               user_features=user_features,
                               item_features=item_features)
コード例 #2
0
def test_predict():
    no_users, no_items = 10, 100

    train = sp.coo_matrix((no_users, no_items), dtype=np.int32)

    model = LightFM()
    model.fit_partial(train)

    for uid in range(no_users):
        scores_arr = model.predict(np.repeat(uid, no_items),
                                   np.arange(no_items))
        scores_int = model.predict(uid, np.arange(no_items))
        assert np.allclose(scores_arr, scores_int)
コード例 #3
0
def test_predict_not_fitted():
    model = LightFM()

    with pytest.raises(ValueError):
        model.predict(np.arange(10), np.arange(10))

    with pytest.raises(ValueError):
        model.predict_rank(1)

    with pytest.raises(ValueError):
        model.get_user_representations()

    with pytest.raises(ValueError):
        model.get_item_representations()
コード例 #4
0
def test_batch_predict_with_items():
    no_components = 2
    ds = RandomDataset(density=1.0)

    model = LightFM(no_components=no_components)
    model.fit_partial(ds.train,
                      user_features=ds.user_features,
                      item_features=ds.item_features)
    model.batch_setup(item_chunks={0: ds.item_ids},
                      user_features=ds.user_features,
                      item_features=ds.item_features)
    n_items = 10
    item_ids = np.random.choice(ds.item_ids, n_items)

    for uid in range(ds.no_users):

        original_scores = model.predict(
            np.repeat(uid, n_items),
            item_ids=item_ids,
            user_features=ds.user_features,
            item_features=ds.item_features,
        )

        # Check scores
        _, batch_predicted_scores = model.predict_for_user(user_id=uid,
                                                           item_ids=item_ids,
                                                           top_k=0)
        assert_array_almost_equal(original_scores, batch_predicted_scores)

        # Check ids
        original_ids = item_ids[np.argsort(-original_scores)[:5]]
        batch_ids, _ = model.predict_for_user(user_id=uid,
                                              item_ids=item_ids,
                                              top_k=5)
        assert_array_equal(original_ids, batch_ids)
コード例 #5
0
def test_feature_inference_fails():
    # On predict if we try to use feature inference and supply
    # higher ids than the number of features that were supplied to fit
    # we should complain

    no_users, no_items = 10, 100
    no_features = 20

    train = sp.coo_matrix((no_users, no_items), dtype=np.int32)

    user_features = sp.csr_matrix((no_users, no_features), dtype=np.int32)
    item_features = sp.csr_matrix((no_items, no_features), dtype=np.int32)
    model = LightFM()
    model.fit_partial(train,
                      user_features=user_features,
                      item_features=item_features)

    with pytest.raises(ValueError):
        model.predict(np.array([no_features], dtype=np.int32),
                      np.array([no_features], dtype=np.int32))
コード例 #6
0
def test_batch_predict():
    no_components = 2
    ds = RandomDataset(density=1.0)

    model = LightFM(no_components=no_components)
    model.fit_partial(ds.train,
                      user_features=ds.user_features,
                      item_features=ds.item_features)

    model.batch_setup(
        item_chunks={0: ds.item_ids},
        user_features=ds.user_features,
        item_features=ds.item_features,
    )
    user_repr = inference._user_repr
    item_repr = inference._item_repr
    assert np.sum(user_repr)
    assert user_repr.shape == (ds.no_users, no_components)
    assert np.sum(item_repr)
    assert item_repr.shape == (no_components, ds.no_items)

    zeros = 0

    for uid in range(ds.no_users):

        original_scores = model.predict(
            np.repeat(uid, ds.no_items),
            np.arange(ds.no_items),
            user_features=ds.user_features,
            item_features=ds.item_features,
        )

        # Check scores
        _, batch_predicted_scores = model.predict_for_user(
            user_id=uid, top_k=0, item_ids=ds.item_ids)
        assert_array_almost_equal(original_scores, batch_predicted_scores)

        # Check ids
        original_ids = np.argsort(-original_scores)[:5]
        batch_ids, _ = model.predict_for_user(user_id=uid,
                                              top_k=5,
                                              item_ids=ds.item_ids)
        assert np.array_equal(original_ids, batch_ids)

        if np.sum(batch_predicted_scores) == 0:
            zeros += 1
    assert zeros < ds.no_users, 'predictions seems to be all zeros'
コード例 #7
0
def test_regression_full_batch_predict():
    no_components = 2
    np.random.seed(42)
    ds = RandomDataset(no_items=5, density=1)

    model = LightFM(no_components=no_components)
    model.fit(ds.train,
              user_features=ds.user_features,
              item_features=ds.item_features)

    # Set non zero biases
    model.item_biases += 0.2
    model.user_biases += 0.5
    user_ids = [0, 1, 2]

    model.batch_setup(item_chunks={0: ds.item_ids},
                      item_features=ds.item_features,
                      user_features=ds.user_features)
    recoms = model.batch_predict(
        user_ids=user_ids,
        chunk_id=0,
        top_k=0,  # Score all items
    )
    zeros = 0
    for user_id in user_ids:
        scores = model.predict(
            user_ids=user_id,
            item_ids=ds.item_ids,
            item_features=ds.item_features,
            user_features=ds.user_features,
            num_threads=1,
        )
        if sum(scores) != 0:
            zeros += 1
        assert_array_almost_equal(recoms[user_id][1], scores)
    assert zeros != 0