#310Logistic regression — probability of the true labelEasy

Logistic regression — probability of the true label

Background

Maximum likelihood estimation (MLE) asks: under the model, how probable is the label we actually observed? For a binary example with predicted p=P(y=1x)p = \mathbb{P}(y=1\mid\mathbf{x}), the probability assigned to the true label is

P(observed y)={py=11py=0\mathbb{P}(\text{observed } y) = \begin{cases} p & y = 1 \\ 1 - p & y = 0 \end{cases}

A good model makes this number large — close to 11 — for every training point. That is precisely what MLE pushes for.

Problem statement

Implement label_likelihood(y, p) returning, for each example, the probability the model assigns to its observed label.

Input

  • y — array-like of true labels, each 0 or 1.
  • p — array-like of predicted probabilities P(y=1)\mathbb{P}(y=1), same length, each in [0,1][0, 1].

Output

Returns an np.ndarray the same length as y: p where y == 1, and 1 - p where y == 0.

Examples

Example 1

Input:  y = [1, 0, 1], p = [0.9, 0.2, 0.6]
Output: [0.9 0.8 0.6]

Explanation: for the class-1 points the likelihood is pp itself (0.90.9, 0.60.6); for the class-0 point it is 10.2=0.81-0.2 = 0.8.

Example 2 — a confident mistake scores low

Input:  y = [1], p = [0.05]
Output: [0.05]

Explanation: the truth is class 1 but the model gave it only 0.050.05, so the observed label was deemed very unlikely.

Constraints

  • Use np.where(y == 1, p, 1 - p) or equivalent — one vectorised expression, no loop.
  • Assume p is already a valid probability; do not clip.

Notes

  • This per-example number is the building block of the dataset likelihood (the product over all examples) and, after a log, of binary cross-entropy.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example — mixed labels select p or 1-p
  • Reference — a confident mistake scores low
  • Sample — all class 0 gives 1 - p