DataLane

31. F1 score

hardProNot started~14 minPythonImplement f1_score()

Asked in screens shaped like: OpenAI, Anthropic

Binary F1: the harmonic mean of precision and recall for labels y and predictions pred in {0,1}. Round to 4 decimals. If precision + recall is 0, return 0.

Examples

  • Example 1

    Input: { "y": [1, 0, 1, 1], "pred": [1, 0, 0, 1] }
    Output: 0.8

Constraints

  • 1 <= n <= 10**5, where n is the length of the input
  • Input is non-empty

Python editor

Loading Python engine…

Ctrl/Cmd + Enter submits

Run your code to see stdout.

Pro problem

Hard problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close