#282Logistic regression — the BCE gradient (error × feature)Easy

Logistic regression — the BCE gradient (error × feature)

Background

Differentiating binary cross-entropy looks scary, but the sigmoid factor y^(1−y^)\hat{y}(1-\hat{y}) cancels the 1/y^1/\hat{y} from the log and the cross terms vanish, leaving a strikingly simple gradient:

∂J∂θj=1m∑i=1m(y^i−yi) xij⟹∇θJ=1mX⊤(y^−y)\frac{\partial J}{\partial \theta_j} = \frac{1}{m}\sum_{i=1}^{m}(\hat{y}_i - y_i)\,x_{ij} \qquad\Longrightarrow\qquad \nabla_{\boldsymbol\theta} J = \frac{1}{m}X^\top(\hat{\mathbf{y}} - \mathbf{y})

Read it as error × feature: (y^i−yi)(\hat{y}_i - y_i) is the prediction error, and multiplying by xijx_{ij} says "nudge θj\theta_j in proportion to how much feature jj drove that error." It is the same shape as the linear-regression gradient — only y^=σ(z)\hat{y}=\sigma(z) changed.

Problem statement

Implement bce_gradient(X, y, y_hat) returning 1mX⊤(y^−y)\frac{1}{m}X^\top(\hat{\mathbf{y}} - \mathbf{y}).

Input

  • X — array-like of shape (m, n): one feature row per example.
  • y — array-like of m true labels (0/1).
  • y_hat — array-like of m predicted probabilities.

Output

Returns an np.ndarray of shape (n,): the gradient, one component per feature.

Examples

Example 1 — zero error, zero gradient

Input:  X = [[1, 2], [3, 4]], y = [1, 0], y_hat = [1, 0]
Output: [0. 0.]

Explanation: predictions match labels, so y^−y=0\hat{y}-y=0 and the gradient vanishes.

Example 2

Input:  X = [[1, 2], [1, 3]], y = [1, 0], y_hat = [0.5, 0.5]
Output: [0.   0.25]

Explanation: errors are [−0.5,0.5][-0.5, 0.5]; X⊤(y^−y)=[ 0, 0.5 ]X^\top(\hat{y}-y) = [\,0,\ 0.5\,], divided by m=2m=2 gives [0,0.25][0, 0.25].

Constraints

  • Use the error y^−y\hat{\mathbf{y}} - \mathbf{y} (predicted minus true), then project with X⊤X^\top and divide by mm.
  • Return an np.ndarray of shape (n,).

Notes

  • This is the gradient you plug into the update θ←θ−η ∇θJ\boldsymbol\theta \leftarrow \boldsymbol\theta - \eta\,\nabla_{\boldsymbol\theta}J to train logistic regression — structurally identical to the linear-regression step from Lesson 1.
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.

  • •Reference example: worked two-feature gradient
  • •Sample single feature averages the error
  • •Example three-row two-feature gradient