Click4Ai

63.

Hard

Implement a **Multi-class Logistic Regression** classifier from scratch using the softmax function and gradient descent.

Softmax Function:

softmax(z_i) = exp(z_i) / sum(exp(z_j)) for all j

**One-Hot Encoding:** Convert integer labels to binary vectors (e.g., label 2 with 3 classes -> [0, 0, 1]).

Gradient Descent Update:

dW = (1/n) * X^T . (probabilities - y_one_hot)

db = (1/n) * sum(probabilities - y_one_hot)

W = W - lr * dW

b = b - lr * db

Example:

X = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]]

y = [0, 0, 1, 1, 2, 2]

model = MultiClassLogisticRegression(learning_rate=0.1, n_iterations=1000)

model.fit(X, y)

predictions = model.predict(X)

# predictions should approximate [0, 0, 1, 1, 2, 2]

**Explanation:** The softmax function converts raw scores (logits) into probability distributions across multiple classes. Training uses cross-entropy loss with gradient descent to update weights. Prediction selects the class with the highest probability.

Constraints:

  • Use numerically stable softmax (subtract max before exp)
  • Initialize weights to zeros
  • Support any number of classes (>= 2)
  • X shape: (n_samples, n_features), y shape: (n_samples,) with integer labels
  • Test Cases

    Test Case 1
    Input: Example input 1
    Expected: Expected output 1
    Test Case 2
    Input: Example input 2
    Expected: Expected output 2
    + 1 hidden test cases