当前位置: 首页>>代码示例>>Python>>正文


Python LightFM.fit_partial方法代码示例

本文整理汇总了Python中lightfm.lightfm.LightFM.fit_partial方法的典型用法代码示例。如果您正苦于以下问题:Python LightFM.fit_partial方法的具体用法?Python LightFM.fit_partial怎么用?Python LightFM.fit_partial使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lightfm.lightfm.LightFM的用法示例。


在下文中一共展示了LightFM.fit_partial方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_user_supplied_features_accuracy

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_user_supplied_features_accuracy():

    model = LightFM(random_state=SEED)
    model.fit_partial(
        train,
        user_features=train_user_features,
        item_features=train_item_features,
        epochs=10,
    )

    train_predictions = model.predict(
        train.row,
        train.col,
        user_features=train_user_features,
        item_features=train_item_features,
    )
    test_predictions = model.predict(
        test.row,
        test.col,
        user_features=test_user_features,
        item_features=test_item_features,
    )

    assert roc_auc_score(train.data, train_predictions) > 0.84
    assert roc_auc_score(test.data, test_predictions) > 0.76
开发者ID:linggom,项目名称:lightfm,代码行数:27,代码来源:test_movielens.py

示例2: test_recall_at_k

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_recall_at_k():

    no_users, no_items = (10, 100)

    train, test = _generate_data(no_users, no_items)

    model = LightFM(loss="bpr")
    model.fit_partial(test)

    for k in (10, 5, 1):

        # Without omitting train interactions
        recall = evaluation.recall_at_k(model, test, k=k)
        expected_mean_recall = _recall_at_k(model, test, k)

        assert np.allclose(recall.mean(), expected_mean_recall)
        assert len(recall) == (test.getnnz(axis=1) > 0).sum()
        assert (
            len(evaluation.recall_at_k(model, train, preserve_rows=True))
            == test.shape[0]
        )

        # With omitting train interactions
        recall = evaluation.recall_at_k(model, test, k=k, train_interactions=train)
        expected_mean_recall = _recall_at_k(model, test, k, train=train)

        assert np.allclose(recall.mean(), expected_mean_recall)
开发者ID:linggom,项目名称:lightfm,代码行数:29,代码来源:test_evaluation.py

示例3: test_get_representations

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_get_representations():

    model = LightFM(random_state=SEED)
    model.fit_partial(train, epochs=10)

    num_users, num_items = train.shape

    for (item_features, user_features) in (
        (None, None),
        (
            (sp.identity(num_items) + sp.random(num_items, num_items)),
            (sp.identity(num_users) + sp.random(num_users, num_users)),
        ),
    ):

        test_predictions = model.predict(
            test.row, test.col, user_features=user_features, item_features=item_features
        )

        item_biases, item_latent = model.get_item_representations(item_features)
        user_biases, user_latent = model.get_user_representations(user_features)

        assert item_latent.dtype == np.float32
        assert user_latent.dtype == np.float32

        predictions = (
            (user_latent[test.row] * item_latent[test.col]).sum(axis=1)
            + user_biases[test.row]
            + item_biases[test.col]
        )

        assert np.allclose(test_predictions, predictions, atol=0.000001)
开发者ID:linggom,项目名称:lightfm,代码行数:34,代码来源:test_movielens.py

示例4: test_matrix_types

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_matrix_types():

    mattypes = (sp.coo_matrix, sp.lil_matrix, sp.csr_matrix, sp.csc_matrix)

    dtypes = (np.int32, np.int64, np.float32, np.float64)

    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)
            weights = train.tocoo()

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

            model = LightFM()
            model.fit_partial(
                train,
                sample_weight=weights,
                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
            )
开发者ID:linggom,项目名称:lightfm,代码行数:37,代码来源:test_api.py

示例5: test_precision_at_k

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_precision_at_k():

    no_users, no_items = (10, 100)

    train, test = _generate_data(no_users, no_items)

    model = LightFM(loss="bpr")

    # We want a high precision to catch the k=1 case
    model.fit_partial(test)

    for k in (10, 5, 1):

        # Without omitting train interactions
        precision = evaluation.precision_at_k(model, test, k=k)
        expected_mean_precision = _precision_at_k(model, test, k)

        assert np.allclose(precision.mean(), expected_mean_precision)
        assert len(precision) == (test.getnnz(axis=1) > 0).sum()
        assert (
            len(evaluation.precision_at_k(model, train, preserve_rows=True))
            == test.shape[0]
        )

        # With omitting train interactions
        precision = evaluation.precision_at_k(
            model, test, k=k, train_interactions=train
        )
        expected_mean_precision = _precision_at_k(model, test, k, train=train)

        assert np.allclose(precision.mean(), expected_mean_precision)
开发者ID:linggom,项目名称:lightfm,代码行数:33,代码来源:test_evaluation.py

示例6: test_empty_matrix

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_empty_matrix():

    no_users, no_items = (10, 100)

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

    model = LightFM()
    model.fit_partial(train)
开发者ID:linggom,项目名称:lightfm,代码行数:10,代码来源:test_api.py

示例7: test_intersections_check

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_intersections_check():

    no_users, no_items = (10, 100)

    train, test = _generate_data(no_users, no_items)

    model = LightFM(loss="bpr")
    model.fit_partial(train)

    # check error is raised when train and test have interactions in common
    with pytest.raises(ValueError):
        evaluation.auc_score(
            model, train, train_interactions=train, check_intersections=True
        )

    with pytest.raises(ValueError):
        evaluation.recall_at_k(
            model, train, train_interactions=train, check_intersections=True
        )

    with pytest.raises(ValueError):
        evaluation.precision_at_k(
            model, train, train_interactions=train, check_intersections=True
        )

    with pytest.raises(ValueError):
        evaluation.reciprocal_rank(
            model, train, train_interactions=train, check_intersections=True
        )

    # check no errors raised when train and test have no interactions in common
    evaluation.auc_score(
        model, test, train_interactions=train, check_intersections=True
    )
    evaluation.recall_at_k(
        model, test, train_interactions=train, check_intersections=True
    )
    evaluation.precision_at_k(
        model, test, train_interactions=train, check_intersections=True
    )
    evaluation.reciprocal_rank(
        model, test, train_interactions=train, check_intersections=True
    )

    # check no error is raised when there are intersections but flag is False
    evaluation.auc_score(
        model, train, train_interactions=train, check_intersections=False
    )
    evaluation.recall_at_k(
        model, train, train_interactions=train, check_intersections=False
    )
    evaluation.precision_at_k(
        model, train, train_interactions=train, check_intersections=False
    )
    evaluation.reciprocal_rank(
        model, train, train_interactions=train, check_intersections=False
    )
开发者ID:linggom,项目名称:lightfm,代码行数:59,代码来源:test_evaluation.py

示例8: test_movielens_accuracy

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_movielens_accuracy():

    model = LightFM(random_state=SEED)
    model.fit_partial(train, epochs=10)

    train_predictions = model.predict(train.row, train.col)
    test_predictions = model.predict(test.row, test.col)

    assert roc_auc_score(train.data, train_predictions) > 0.84
    assert roc_auc_score(test.data, test_predictions) > 0.76
开发者ID:linggom,项目名称:lightfm,代码行数:12,代码来源:test_movielens.py

示例9: test_hogwild_accuracy

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_hogwild_accuracy():

    # Should get comparable accuracy with 2 threads
    model = LightFM(random_state=SEED)
    model.fit_partial(train, epochs=10, num_threads=2)

    train_predictions = model.predict(train.row, train.col, num_threads=2)
    test_predictions = model.predict(test.row, test.col, num_threads=2)

    assert roc_auc_score(train.data, train_predictions) > 0.84
    assert roc_auc_score(test.data, test_predictions) > 0.76
开发者ID:linggom,项目名称:lightfm,代码行数:13,代码来源:test_movielens.py

示例10: test_warp_stability

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_warp_stability():

    learning_rates = (0.05, 0.1, 0.5)

    for lrate in learning_rates:

        model = LightFM(learning_rate=lrate, loss="warp", random_state=SEED)
        model.fit_partial(train, epochs=10)

        assert not np.isnan(model.user_embeddings).any()
        assert not np.isnan(model.item_embeddings).any()
开发者ID:linggom,项目名称:lightfm,代码行数:13,代码来源:test_movielens.py

示例11: test_random_state_fixing

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_random_state_fixing():

    model = LightFM(learning_rate=0.05, loss="warp", random_state=SEED)

    model.fit_partial(train, epochs=2)

    model_2 = LightFM(learning_rate=0.05, loss="warp", random_state=SEED)

    model_2.fit_partial(train, epochs=2)

    assert np.all(model.user_embeddings == model_2.user_embeddings)
    assert np.all(model.item_embeddings == model_2.item_embeddings)
开发者ID:linggom,项目名称:lightfm,代码行数:14,代码来源:test_movielens.py

示例12: test_zeros_negative_accuracy

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_zeros_negative_accuracy():

    # Should get the same accuracy when zeros are used to
    # denote negative interactions
    train.data[train.data == -1] = 0
    model = LightFM(random_state=SEED)
    model.fit_partial(train, epochs=10)

    train_predictions = model.predict(train.row, train.col)
    test_predictions = model.predict(test.row, test.col)

    assert roc_auc_score(train.data, train_predictions) > 0.84
    assert roc_auc_score(test.data, test_predictions) > 0.76
开发者ID:linggom,项目名称:lightfm,代码行数:15,代码来源:test_movielens.py

示例13: test_overfitting

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_overfitting():

    # Let's massivly overfit
    model = LightFM(no_components=50, random_state=SEED)
    model.fit_partial(train, epochs=30)

    train_predictions = model.predict(train.row, train.col)
    test_predictions = model.predict(test.row, test.col)
    overfit_train = roc_auc_score(train.data, train_predictions)
    overfit_test = roc_auc_score(test.data, test_predictions)

    assert overfit_train > 0.99
    assert overfit_test < 0.75
开发者ID:linggom,项目名称:lightfm,代码行数:15,代码来源:test_movielens.py

示例14: test_regularization

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
def test_regularization():

    # Let's regularize
    model = LightFM(
        no_components=50, item_alpha=0.0001, user_alpha=0.0001, random_state=SEED
    )
    model.fit_partial(train, epochs=30)

    train_predictions = model.predict(train.row, train.col)
    test_predictions = model.predict(test.row, test.col)

    assert roc_auc_score(train.data, train_predictions) > 0.80
    assert roc_auc_score(test.data, test_predictions) > 0.75
开发者ID:linggom,项目名称:lightfm,代码行数:15,代码来源:test_movielens.py

示例15: test_predict

# 需要导入模块: from lightfm.lightfm import LightFM [as 别名]
# 或者: from lightfm.lightfm.LightFM import fit_partial [as 别名]
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)
开发者ID:linggom,项目名称:lightfm,代码行数:15,代码来源:test_api.py


注:本文中的lightfm.lightfm.LightFM.fit_partial方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。