docstr: Test mean average precision @ k
docstr_tok: ["Test","mean","average","precision","@","k"]
code: def test_mapk(self):
      """
      Test mean average precision @ k
      """
  
      ## TODO: Fix this test
      ## Currently it's failing due to (hypothesized reason)
      ##  not giving "extra credit" for multiple correct but repeated
      ##  predictions. For instance, if the actual is ["A", "B"] and
      ##  you predict "A" ten times, the nine "A" predictions after
      ##  the first one are "wrong" since they're repeats.
  
      # First case, binary
      # GIVEN predictions and actual values
      # test_predicted = [[1,0,1,0,0,1,0,0,1,1], [0,2,0,0,2,0,2,0,0,0]]
      # test_actual = [[1,1,1,1,1], [2,2,2]]
  
      # WHEN metrics.mapk is run
      # recommender_mapk = metrics.mapk(
      #     predicted = test_predicted,
      #     actual = test_actual,
      #     k = 10
      # )
  
      # THEN the expected value should equal 0.53254 within three decimal places
      # self.assertAlmostEqual(recommender_mapk, 0.53254, places=5)
  
      # Second case, multi class
      # GIVEN predictions and actual values
      test_predicted = [
          [5, 0, 4, 0, 0, 2, 0, 0, 3, 1],
          [0, 7, 0, 0, 6, 0, 8, 0, 0, 0],
      ]
      test_actual = [[1, 2, 3, 4, 5], [6, 7, 8]]
  
      # WHEN metrics.mapk is run
      recommender_mapk = metrics.mapk(
          predicted=test_predicted, actual=test_actual, k=10
      )
  
      # THEN the expected value should equal 0.53254 within three decimal places
      self.assertAlmostEqual(recommender_mapk, 0.53254, places=5)
  
      # GIVEN test MAP@K metrics
      test_actual = [["A", "B", "X"], ["A", "B", "Y"]]
      test_predicted = [["X", "Y", "Z"], ["A", "Z", "B"]]
      test_k = 5
  
      # WHEN metrics.mapk is run
      mean_abs_precision_k = metrics.mapk(
          actual=test_actual, predicted=test_predicted, k=test_k
      )
  
      # THEN the mean absolute precision @ k should the average
      #  precision over the two sets of predictions
      self.assertEqual(
          mean_abs_precision_k, ((1) + ((1 + (2 / 3)) / 2)) / 2
      )
  
file_path: recmetrics/tests/test_metrics.py
imports: import unittest
  from unittest import mock
  import pandas
  from recmetrics import metrics
label_desc: Use image node_id 13 for calling the TestMetrics obj's underlying member method code with example usage: obj.test_mapk() without return types