Problem · Matrix

Logistic Regression Diagnostics

Learn this problem
MediumLinkedIn logoLinkedInFULLTIMEPHONE SCREEN

Problem statement

You are given a feature matrix features, a binary label vector labels, and a weight vector weights for logistic regression without an intercept term.

For sample i, compute the raw logit z[i] = features[i] dot weights and probability h[i] = sigmoid(z[i]).

Return a ragged double[][] with exactly three rows:

  1. [cost], where cost is the mean full binary cross-entropy over all samples.
  2. The gradient vector features^T(h - labels) / m, in feature-column order.
  3. The sigmoid derivative sigmoid(z[i]) * (1 - sigmoid(z[i])) for every raw logit, in sample order.

Evaluate each loss term with the stable logit identity max(z, 0) - y * z + log(1 + exp(-abs(z))). Returned floating-point values are compared with relative or absolute tolerance 10^-9.

Function

logisticRegressionDiagnostics(features: double[][], labels: int[], weights: double[]) → double[][]

Examples

Example 1

features = [[1.0,0.0],[1.0,1.0]]labels = [0,1]weights = [0.0,0.0]return = [[0.6931471805599453],[0.0,-0.25],[0.25,0.25]]

Both logits are 0, so both probabilities are 0.5. The complete positive-and-negative binary cross-entropy is log(2).

Example 2

features = [[1.0,-1.0],[2.0,1.0]]labels = [1,0]weights = [1.0,0.5]return = [[1.5264833592363282],[0.735371485579684,0.650841244388451],[0.2350037122015945,0.07010371654510815]]

The logits are 0.5 and 2.5. The second negative-label sample contributes the omitted negative-class loss term and a large positive residual.

Example 3

features = [[1.0],[-1.0]]labels = [1,0]weights = [1000.0]return = [[0.0],[0.0],[0.0,0.0]]

The stable formulation handles logits 1000 and -1000 without overflow. Both predictions agree with their labels to double precision.

Constraints

  • 1 <= features.length <= 200.
  • 1 <= features[i].length = weights.length <= 50.
  • labels.length = features.length and each label is 0 or 1.
  • -1000 <= features[i][j], weights[j] <= 1000.
  • All inputs are finite doubles, and every returned value fits in a finite double.

More LinkedIn problems

drafts saved locally
public double[][] logisticRegressionDiagnostics(double[][] features, int[] labels, double[] weights) {
  // write your code here
}
features[[1.0,0.0],[1.0,1.0]]
labels[0,1]
weights[0.0,0.0]
expected[[0.6931471805599453],[0.0,-0.25],[0.25,0.25]]
checking account