Linear Regression
Imagine you're a kid with a ruler and a piece of graph paper covered in dots. Each dot is a house β the x-axis is the house's size in square feet, and the y-axis is its price. Your job: place the ruler so the line passes as close to all the dots as possible.
That's linear regression in a nutshell. You're drawing the best straight line through your data so you can use it to predict new values.
The equation behind the line
Every straight line can be written as:
y = mx + b
You already know this from school. In ML-speak, we just rename things:
- y β the prediction (house price)
- x β the input feature (house size)
- m β the weight (how much price changes per square foot)
- b β the bias (the base price even for a "zero-size" house)
Training a linear regression model means finding the best values of m and b so the line fits the data as closely as possible.
How do we measure "best"?
We use something called Mean Squared Error (MSE). For every data point, we calculate the difference between the actual value and our line's prediction, square it (so negatives don't cancel out), and average all those squared errors.
Think of it like a teacher grading your guesses. Each guess that's off by 10 gets penalized 100 (10 squared). A guess that's off by 2 only gets penalized 4. Big mistakes get punished way more than small ones β that's the beauty of squaring.
The model's goal: minimize the MSE. Find the m and b that make the total error as tiny as possible.
Linear Regression from Scratch
Key Metrics
Quick check
Continue reading