code: def test_log_loss(self):
      # Binary classification
      y = turicreate.SArray([1, 1, 0, 1, 1])
      yhat = turicreate.SArray([0.5, 0.2, 0.8, 0.3, 0.9])
  
      logp = yhat.apply(lambda x: math.log(x))
      log1mp = yhat.apply(lambda x: math.log(1 - x))
      expected = -(y * logp + (1 - y) * log1mp).mean()
  
      observed = turicreate.toolkits.evaluation.log_loss(y, yhat)
      self.assertAlmostEqual(expected, observed)
  
      # Binary classification, string
      y = turicreate.SArray([1, 1, 0, 1, 1])
      yhat = turicreate.SArray([0.5, 0.2, 0.8, 0.3, 0.9])
  
      logp = yhat.apply(lambda x: math.log(x))
      log1mp = yhat.apply(lambda x: math.log(1 - x))
      expected = -(y * logp + (1 - y) * log1mp).mean()
  
      observed = turicreate.toolkits.evaluation.log_loss(
          y.astype(str), yhat
      )
      self.assertAlmostEqual(expected, observed)
  
      # Binary classification vs sklearn example
      y = turicreate.SArray([1, 0, 0, 1])
      yhat = turicreate.SArray(
          [[0.1, 0.9], [0.9, 0.1], [0.8, 0.2], [0.35, 0.65]]
      )
  
      expected = (
          -(
              math.log(0.9)
              + math.log(0.9)
              + math.log(0.8)
              + math.log(0.65)
          )
          / 4.0
      )
  
      observed = turicreate.toolkits.evaluation.log_loss(y, yhat)
      self.assertAlmostEqual(expected, observed)
  
      # Binary classification with missing data
      y = turicreate.SArray([1, None, None, 1])
      yhat = turicreate.SArray(
          [[0.1, 0.9], [0.9, 0.1], [0.8, 0.2], [0.35, 0.65]]
      )
  
      expected = (
          -(
              math.log(0.9)
              + math.log(0.9)
              + math.log(0.8)
              + math.log(0.65)
          )
          / 4.0
      )
  
      observed = turicreate.toolkits.evaluation.log_loss(y, yhat)
      self.assertAlmostEqual(expected, observed)
  
      # Multiclass
      y = turicreate.SArray([0, 1, 2, 0])
      yhat = turicreate.SArray(
          [
              [0.5, 0.1, 0.4],
              [0.5, 0.1, 0.4],
              [0.1, 0.5, 0.4],
              [0.2, 0.3, 0.5],
          ]
      )
  
      true_probs = [yhat[0][0], yhat[1][1], yhat[2][2], yhat[3][0]]
      expected = -sum([math.log(x) for x in true_probs]) / 4.0
  
      observed = turicreate.toolkits.evaluation.log_loss(y, yhat)
      self.assertAlmostEqual(expected, observed)
  
      # Multiclass with strings
      y = turicreate.SArray(["a", "b", "c", "a"])
      yhat = turicreate.SArray(
          [
              [0.5, 0.1, 0.4],
              [0.5, 0.1, 0.4],
              [0.1, 0.5, 0.4],
              [0.2, 0.3, 0.5],
          ]
      )
  
      true_probs = [yhat[0][0], yhat[1][1], yhat[2][2], yhat[3][0]]
      expected = -sum([math.log(x) for x in true_probs]) / 4.0
  
      observed = turicreate.toolkits.evaluation.log_loss(y, yhat)
      self.assertAlmostEqual(expected, observed)
  
      # Multiclass with strings and explicit index map
      y = turicreate.SArray(["a", "c", "d", "a"])
      yhat = turicreate.SArray(
          [
              [0.5, 0.0, 0.1, 0.4],
              [0.5, 0.0, 0.1, 0.4],
              [0.1, 0.0, 0.5, 0.4],
              [0.2, 0.0, 0.3, 0.5],
          ]
      )
      index_map = {"a": 0, "b": 1, "c": 2, "d": 3}
  
      true_probs = [yhat[0][0], yhat[1][2], yhat[2][3], yhat[3][0]]
      expected = -sum([math.log(x) for x in true_probs]) / 4.0
  
      observed = turicreate.toolkits.evaluation.log_loss(
          y, yhat, index_map=index_map
      )
      self.assertAlmostEqual(expected, observed)
  
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import unittest
  import turicreate
  from turicreate.toolkits._main import ToolkitError
  import math
  from numpy import inf
  import numpy
label_desc: Use image node_id 2 for calling the MetricsTest obj's underlying member method code with example usage: obj.test_log_loss() without return types