class_docstr: Unit test class for correctness of model predictions.
class_docstr_tok: ["Unit","test","class","for","correctness","of","model","predictions","."]
docstr: Unit test for the top-k predictions method.
docstr_tok: ["Unit","test","for","the","top-k","predictions","method","."]
code: def test_predict_topk(self):
      """
      Unit test for the top-k predictions method.
      """
  
      ## Check type out output.
      topk = self.model.predict_topk(self.sf[:1], verbose=False)
  
      self.assertIsInstance(topk, tc.SFrame)
      self.assertItemsEqual(
          topk.column_names(), ["row_id", "class", "probability"]
      )
      self.assertItemsEqual(topk.column_types(), [int, str, float])
  
      ## Test plausibility of probability output.
      self.assertTrue(all(topk["probability"] >= 0))
      self.assertTrue(all(topk["probability"] <= 1))
  
      ## Test that k = 1 returns the same top probabilities as model.classify.
      #  Note, the existence of ties means it's impossible to check for the
      #  same class labels.
      topk = self.model.predict_topk(self.sf, k=1, verbose=False)
      ystar = self.model.classify(self.sf, verbose=False)
  
      # TODO @papayawarrior -- this assertion fails now that SArray
      # comparison is meaningful. Please fix the test and uncomment.
      # self.assertTrue((topk['probability'] == ystar['probability']).all())
  
      ## Check that k results are returned for each query.
      topk = self.model.predict_topk(self.sf, k=2, verbose=False)
      counts = topk.groupby("row_id", tc.aggregate.COUNT)
      self.assertEqual(counts.num_rows(), self.sf.num_rows())
      self.assertTrue(all(counts["Count"] == 2))
  
      ## Check that 3 results are returned for each query even if k is bigger.
      topk = self.model.predict_topk(self.sf, k=100, verbose=False)
      counts = topk.groupby("row_id", tc.aggregate.COUNT)
  
      num_classes = len(self.sf["class"].unique())
      self.assertTrue(all(counts["Count"] <= num_classes))
      self.assertEqual(counts.num_rows(), self.sf.num_rows())
  
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import unittest
  import copy
  import array
  import numpy
  import turicreate
  from .None import util
  from turicreate.util import _assert_sframe_equal
  from turicreate.toolkits._main import ToolkitError
  import sys
label_desc: Use image node_id 5 for calling the KnnClassifierPredictTest obj's underlying member method code with example usage: obj.test_predict_topk() without return types