Implement Mini-Batch Logistic Regression From Scratch in NumPy
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `fit_logistic_minibatch(X, y, lr, n_epochs, batch_size, seed)`: binary logistic regression via mini-batch gradient descent using only NumPy. Initialize `w = np.zeros(n_features)` and a separate scalar intercept `b = 0.0` (`X` has no ones column). Return `(w, b)` with `w` of shape `(n_features,)` and `b` a float. Exact parameter values are graded.
```python
def fit_logistic_minibatch(X: np.ndarray, y: np.ndarray, lr: float, n_epochs: int, batch_size: int, seed: int) -> tuple[np.ndarray, float]:
```
The training loop must follow these rules:
- Create `rng = np.random.default_rng(seed)` once before the loop, with no re-seeding.
- At the start of each epoch, draw `perm = rng.permutation(n_samples)`. That is one permutation per epoch, not a single shuffle for the whole run.
- Batches are `perm[i:i+batch_size]` for `i` in `range(0, n_samples, batch_size)`, in order, including the final short batch.
- Make one update per batch with the batch-mean gradient, dividing by `len(idx)` (not by `n_samples` or `batch_size`).
- Use a fixed `lr` and exactly `n_epochs` epochs, with no early stopping and no regularization.
As a check: with `X = [[1.0], [2.0]]`, `y = [1, 0]`, `lr = 1.0`, `n_epochs = 1`, `batch_size = 2` and any seed, the function returns `w = [-0.25]` and `b = 0.0`.
```hint Size of the last batch
When `n_samples` is not a multiple of `batch_size`, count how many rows the last slice really holds and what that means for the mean.
```
```hint Shuffling state
Ask whether two different epochs could ever produce the same batches under your loop structure, and what would cause that.
```
### Constraints & Assumptions
- `X` is a numeric array of shape `(n_samples, n_features)` with no intercept column.
- `y` has shape `(n_samples,)`, holds 0/1 labels, and its dimensions are consistent with `X`.
- `batch_size` is a positive integer and may not divide `n_samples` evenly.
### What a Strong Answer Covers
- The batch-mean cross-entropy gradient for the weights and the intercept, divided by the actual number of rows in the batch.
- One generator created before the loop, a fresh permutation per epoch, and consecutive slices taken in order with the final short batch kept.
- Exactly one parameter update per batch, with both updates using the same batch prediction.
- Return values that match the contract, and the time and space cost of the loop.
### Follow-up Questions
- Why does re-seeding per epoch break reproducibility against this spec?
- How would numerical overflow in `exp` affect this, and how do you guard against it?
- What does this function reduce to when `batch_size` is 1, and when `batch_size` is at least `n_samples`?
Overview: Implement binary logistic regression from scratch in NumPy with mini-batch gradient descent and a fixed shuffling scheme. It tests the batch-mean cross-entropy gradient, handling of a short final batch and reproducible per-epoch permutations drawn from one random generator.