本文整理汇总了Python中lightfm.lightfm.LightFM类的典型用法代码示例。如果您正苦于以下问题:Python LightFM类的具体用法?Python LightFM怎么用?Python LightFM使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LightFM类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_user_supplied_features_accuracy
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
示例2: test_recall_at_k
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)
示例3: test_precision_at_k
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)
示例4: test_empty_matrix
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)
示例5: test_intersections_check
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
)
示例6: test_warp_few_items
def test_warp_few_items():
no_users, no_items = (1000, 2)
train = sp.rand(no_users, no_items, format="csr", random_state=42)
model = LightFM(loss="warp", max_sampled=10)
model.fit(train)
示例7: test_return_self
def test_return_self():
no_users, no_items = (10, 100)
train = sp.coo_matrix((no_users, no_items), dtype=np.int32)
model = LightFM()
assert model.fit_partial(train) is model
assert model.fit(train) is model
示例8: test_movielens_accuracy
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
示例9: test_exception_on_divergence
def test_exception_on_divergence():
no_users, no_items = (1000, 1000)
train = sp.rand(no_users, no_items, format="csr", random_state=42)
model = LightFM(learning_rate=10000000.0, loss="warp")
with pytest.raises(ValueError):
model.fit(train, epochs=10)
示例10: test_state_reset
def test_state_reset():
model = LightFM(random_state=SEED)
model.fit(train, epochs=1)
assert np.mean(model.user_embedding_gradients) > 1.0
model.fit(train, epochs=0)
assert np.all(model.user_embedding_gradients == 1.0)
示例11: test_hogwild_accuracy
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
示例12: test_nan_interactions
def test_nan_interactions():
no_users, no_items = (1000, 1000)
train = sp.rand(no_users, no_items, format="csr", random_state=42)
train.data *= np.nan
model = LightFM(loss="warp")
with pytest.raises(ValueError):
model.fit(train)
示例13: test_warp_stability
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()
示例14: test_movielens_accuracy_pickle
def test_movielens_accuracy_pickle():
model = LightFM(random_state=SEED)
model.fit(train, epochs=10)
model = pickle.loads(pickle.dumps(model))
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
示例15: test_regularization
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