docstr: Test mean absolute precision @ k (APK) function
docstr_tok: ["Test","mean","absolute","precision","@","k","(","APK",")","function"]
code: def test_apk(self):
      """
      Test mean absolute precision @ k (APK) function
      """
  
      ## Predictions align with Stanford slides
      ## https://web.stanford.edu/class/cs276/handouts/EvaluationNew-handout-1-per.pdf
      self.assertEqual(
          metrics._apk(
              ["a", "b", "c", "d", "e", "f"],
              ["a", "x", "b", "c", "d", "e", "q", "y", "z", "f"],
              10,
          ),
          0.7749999999999999,
      )
  
      actual = ["A", "B", "X"]
      predicted = ["X", "Y", "Z"]
  
      self.assertEqual(metrics._apk(actual, predicted, 1), 1)
      self.assertEqual(metrics._apk(actual, predicted, 2), 1)
      self.assertEqual(metrics._apk(actual, predicted, 3), 1)
  
      actual = ["A", "B", "X"]
      predicted = ["foo", "B", "A"]
  
      self.assertEqual(metrics._apk(actual, predicted, 1), 0)
      self.assertEqual(metrics._apk(actual, predicted, 2), 1 / 2)
      self.assertEqual(
          metrics._apk(actual, predicted, 3), ((1 / 2) + (2 / 3)) / 2
      )
  
      ## You shouldn't get extra credit for
      ##  predicting the same thing twice
      actual = ["A", "B", "X"]
      predicted = ["A", "A", "Z"]
  
      self.assertEqual(metrics._apk(actual, predicted, 3), 1)
  
      ## If k is less than the number of predictions
      ##  made, we effectively "don't know" about
      ##  the other predictions
      actual = ["A", "B", "X"]
      predicted = ["A", "B", "Z"]
  
      self.assertEqual(metrics._apk(actual, predicted, 3), 1)
  
      ## High K values don't change things
      actual = ["A", "B", "X"]
      predicted = ["A", "A", "Z"]
  
      self.assertEqual(
          metrics._apk(actual, predicted, 3),
          metrics._apk(actual, predicted, 1000),
      )
  
      ## Returns None if no predictions exist
      actual = ["A", "B", "X"]
      predicted = []
  
      self.assertEqual(metrics._apk(actual, predicted, 1), 0)
  
      self.assertEqual(metrics._apk(actual, predicted, 3), 0)
  
      self.assertEqual(metrics._apk(actual, predicted, 100), 0)
  
      ## Returns correctly for single prediction
      actual = ["A", "B", "X"]
  
      self.assertEqual(metrics._apk(actual, ["B"], 1), 1)
  
      self.assertEqual(metrics._apk(actual, ["B"], 3), 1)
  
      self.assertEqual(metrics._apk(actual, ["Z"], 100), 0)
  
      self.assertEqual(metrics._apk(actual, ["B", "B", "B"], 100), 1)
  
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 5 for calling the TestMetrics obj's underlying member method code with example usage: obj.test_apk() without return types