Start with a flat line through 10 delivery trips, measure each error and nudge w and b for 1,000 rounds. It learns minutes = 2.98 × km + 4.21 and predicts a 12 km trip at 39.9 minutes.
python3 --version.git clone https://github.com/DayanEbrar0X/data-anatomy.ai.git cd data-anatomy.ai
python3 -m venv .venv source .venv/bin/activate
cd machine-learning/07-linear-regression-no-libraries python3 src/delivery.py
km = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
mins = [8, 9, 15, 15, 20, 20, 26, 27, 31, 35]
w, b = 0.0, 0.0
lr = 0.02
n = len(km)
for epoch in range(1000):
dw, db = 0.0, 0.0
for x, y in zip(km, mins):
err = w * x + b - y
dw += err * x / n
db += err / n
w -= lr * dw
b -= lr * db
print(f"mins = {w:.2f} * km + {b:.2f}")
print(f"12 km: {w * 12 + b:.1f} min")The loop that trains every neural network fits in fifteen lines. No libraries. Guess, check, nudge, repeat. Say we're predicting delivery times.
Ten past trips: kilometers and minutes. The model is just a line: w tilts it, b lifts it. Both start at zero: a flat line. lr sets the nudge size.
n counts the trips. Now, a thousand rounds of practice. Each round opens two tallies: tilt, and lift. Then we visit every trip.
The line guesses: w times x, plus b. Minus the real time: the error. Eight minutes short. Each error votes on the tallies, averaged over ten trips.
Then w and b take a small step against the error. Like a shower: too cold? Turn it a bit. Check again.
Round after round, its average miss drops to about one minute. Print the line and one prediction. Let's run it. It learned: 2.
98 minutes per kilometer, plus 4.21. A new 12 km order? 39.
9 minutes. The same loop trains every neural network. Just billions of numbers, not two. Plain lines still price and forecast: fast, and explainable.
One catch: set lr too big, and it overshoots. Guess, check, nudge. Fifteen lines, and it learns.
Read the lesson on GitHub →