Hinge Loss

MediumMachine LearningMathArray

Description

Given an array of raw prediction scores and an array of true labels in {-1, +1}, return the mean hinge loss: the average of max(0, 1 - label * score). Round the result to 4 decimal places.

Examples

Input:[1,1,-1], [1,-1,1]
Output:1.3333
Explanation:

Each example is penalized only when its score fails to clear the margin of one on the correct side, and the penalties are averaged.

Input:[2,2], [1,1]
Output:0
Explanation:

Each example is penalized only when its score fails to clear the margin of one on the correct side, and the penalties are averaged.

Input:[0,0], [1,-1]
Output:1
Explanation:

Each example is penalized only when its score fails to clear the margin of one on the correct side, and the penalties are averaged.

Constraints

  • 1 ≤ length ≤ 10⁴
  • labels are -1 or 1

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.