Logistic regression from scratch
Fit binary logistic regression with full-batch gradient descent.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input x is a nonempty feature matrix, y contains binary labels, weights and bias are initial parameters, and learning_rate and steps control batch updates. Each update averages gradients across examples, using the same pre-update parameters for both gradients. Return weights, bias, probabilities in input order, and mean binary cross-entropy loss after the final update. Evaluate sigmoid and loss stably for extreme finite logits.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Fit binary logistic regression with full-batch gradient descent.
Reproduction
{"x":[[-1],[1]],"y":[0,1],"weights":[0],"bias":0,"learning_rate":1,"steps":2}{"weights":[0.877541],"bias":0,"probabilities":[0.293688,0.706312],"loss":0.347698}{"x":[[4]],"y":[1],"weights":[0],"bias":0,"learning_rate":0.2,"steps":0}{"weights":[0],"bias":0,"probabilities":[0.5],"loss":0.693147}{"x":[[0],[0]],"y":[1,1],"weights":[0],"bias":0,"learning_rate":1,"steps":1}{"weights":[0],"bias":0.5,"probabilities":[0.622459,0.622459],"loss":0.474077}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.