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 , the probability assigned to the true label is
A good model makes this number large — close to — 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, each0or1.p— array-like of predicted probabilities , same length, each in .
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 itself (, ); for the class-0 point it is .
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 , 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
pis 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.
▶ 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