code: 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)
  
file_path: lightfm/tests/test_evaluation.py
imports: import numpy
  import pytest
  import scipy.sparse
  from sklearn.metrics import roc_auc_score
  from lightfm.lightfm import LightFM
  from lightfm import evaluation
label_desc: Use image node_id 5 for calling a global function with example usage: test_precision_at_k() without return types