code: def test_spark_precision_at_k(spark_data, spark):
      df_true, df_pred = spark_data
  
      evaluator = SparkRankingEvaluation(df_true, df_pred, k=10)
      assert evaluator.precision_at_k() == pytest.approx(0.26666, TOL)
  
      evaluator = SparkRankingEvaluation(
          df_true,
          df_pred,
          relevancy_method="by_threshold",
          threshold=3.5,
      )
      assert evaluator.precision_at_k() == pytest.approx(0.26666, TOL)
  
      # Check normalization
      single_user = pd.DataFrame(
          {
              "userID": [1, 1, 1],
              "itemID": [1, 2, 3],
              "rating": [5, 4, 3],
          }
      )
      df_single = spark.createDataFrame(single_user)
      evaluator = SparkRankingEvaluation(
          df_single, df_single, k=3, col_prediction="rating"
      )
      assert evaluator.precision_at_k() == 1
  
      same_items = pd.DataFrame(
          {
              "userID": [1, 1, 1, 2, 2, 2],
              "itemID": [1, 2, 3, 1, 2, 3],
              "rating": [5, 4, 3, 5, 5, 3],
          }
      )
      df_same = spark.createDataFrame(same_items)
      evaluator = SparkRankingEvaluation(
          df_same, df_same, k=3, col_prediction="rating"
      )
      assert evaluator.precision_at_k() == 1
  
      # Check that if the sample size is smaller than k, the maximum precision can not be 1
      # if we do precision@5 when there is only 3 items, we can get a maximum of 3/5.
      evaluator = SparkRankingEvaluation(
          df_same, df_same, k=5, col_prediction="rating"
      )
      assert evaluator.precision_at_k() == 0.6
  
imports: import pytest
  import numpy
  import pandas
  from pandas.util.testing import assert_frame_equal
  from recommenders.evaluation.python_evaluation import precision_at_k, recall_at_k, ndcg_at_k, map_at_k, map
label_desc: Use image node_id 10 for calling a global function with example usage: test_spark_precision_at_k(spark_data, spark) without return types