title: Customized loss function for classification
body: <p>I have a classification problem with target Y taking integer values from 1 to 20. My loss function is defined in following way:</p>
  
  <pre><code>def loss_func(y, y_pred):
      numData = len(y)
      diff = y-y_pred
      averageDif = np.mean(diff)
      averageAbsDif = np.mean(np.absolute(diff))
      up = sum(x &gt; 0 for x in diff)
      down = sum(x &lt; 0 for x in diff)
      balanceOffBy= abs(down-up)/float(numData)
      distanceFromIdeal = 0
      requiredPortion = [0.3,0.6,0.9]
      res = {}
  
      for num in range(-2,3):
          res['count'+str(num)] = sum(x == num for x in diff)
  
      count = np.cumsum([res['count0'],res['count1']+res['count-1'],res['count2']+res['count-2']])
  
      for prop,val in zip(requiredPortion,count):
          penalty = 0
          if val &lt; prop*numData:
              penalty = (prop*numData-val)/(prop*numData)
  
          if penalty != 0:
              distanceFromIdeal += 1+penalty
  
      if distanceFromIdeal != 0:
          loss = 10 + distanceFromIdeal
      else:
          loss = 4*averageAbsDif+6*balanceOffBy+2*abs(averageDif)
      return score
  </code></pre>
  
  <p>In this case I not only care about the exact match, but also the distance between y and y_pred, so this may not be a standard classification problem. Can anyone provide any suggestions about which way I should go? Is it possible to use custom loss function to any of the classification libraries included in sklearn?</p>