Linear & Logistic Regression
The mathematical foundation that every deep learning course only gets to after three hours.
Every machine-learning model starts the same way: multiply inputs by weights, add a bias, and check how wrong the prediction is. Linear regression does this for continuous values, logistic regression does it for classes — and together they form the blueprint that every neural network still follows today.
In the previous article, you learned what supervised learning is: data with labels, features, and a target value. Now you meet the first models that actually do something with them. The pattern you learn here — weighted sum, error measurement, non-linear activation — is exactly the skeleton of a neural network.
The Prediction Machine — Linear Regression
Linear Regression
w is the weight (the slope of the line), x is the input value, and b is the bias (the y-intercept). With multiple inputs, the formula becomes ŷ = w₁·x₁ + w₂·x₂ + … + b — instead of a line, it describes a plane or hyperplane.
Example: House Prices
Misconception: Linear regression only works with one input
Interactive: How Training Cost Scales
Move the slider to see how computational cost of different solution methods grows with dataset size. Gradient descent scales linearly, the normal equation quadratically.
At n=100, the difference becomes visible: O(n²) requires 10.000 operations, while O(n) needs only 100. O(log n) needs just 6.6 — that's 15x less than O(n).
Ratio to O(n)
| Complexity | Operations | Factor vs. O(n) |
|---|---|---|
| Prediction (1) | 1 | 100x faster |
| Gradient Descent | 100 | 1x (Reference) |
| Normal Equation | 10.000 | 100x slower |
Keeping Score — Mean Squared Error
Mean Squared Error (MSE)
Why square the errors? Three reasons: (1) Without squaring, positive and negative errors cancel out — an error of +10 and one of −10 would average to zero. (2) Large errors are punished disproportionately: an error of 10 gives 100, but an error of 50 gives 2,500 — not 5 times more, but 25 times. (3) The mean makes MSE comparable across datasets of different sizes.
Example: Three Houses, Three Errors
Misconception: MSE is just a number you look at after training
Deep Dive: Why Not Just Absolute Values?
Interactive: Beat the Line
From Numbers to Decisions — Logistic Regression
Logistic Regression
This z is exactly the result of the weighted sum from the previous step — the bouncer’s internally calculated score. The sigmoid function squeezes this number into the range between 0 and 1. For large positive z it approaches 1, for large negative z it approaches 0, and at z = 0 it gives exactly 0.5. This turns an unbounded weighted sum into a probability.
Example: Spam Classifier
Misconception: Logistic regression is a regression algorithm
Output: continuous number (e.g., price in €). Use case: Regression (predicting values). Loss function: MSE.
Output: probability between 0 and 1. Use case: Classification (assigning classes). Loss function: Log Loss.
Interactive: The Sigmoid Formula Step by Step
Click on each term of the sigmoid formula to understand its role. The example shows a concrete spam classification with z = 2.5.
Sigmoid Formula
The output of the sigmoid function: a probability between 0 and 1. When σ(z) > 0.5, the model classifies as positive (e.g., spam); when σ(z) < 0.5, as negative.
Concrete Example
Given: Spam detection z = w·x + b = 0.5·5 + 0 = 2.5
Computation: z = w·x + b (Linear score) , σ(z) = 1/(1+e⁻ᶿ) (Sigmoid function)
The Big Picture — Bridge to the Artificial Neuron
What you learned in this article is more than three separate concepts. It is a chain that exactly matches the structure of an artificial neuron:
You already understand 80% of an artificial neuron — you just don't know it yet. In the next path (I.F: Deep Learning), you will meet the neuron formally — and recognise that it is exactly this chain, just with more layers.
Deep Dive: Historical Origins
Key Takeaways
Comprehension Check
- Explain the formula ŷ = w · x + b: What is w, what is b, and what happens with multiple inputs?
- Calculate the MSE for errors +4, −2, and +6.
- A logistic model outputs σ(z) = 0.42. The hospital uses a threshold of 0.3. Is the patient classified as at-risk?