Linear Regression — Teaching AI to Draw the Best Straight Line
Series 3, Part 1: ML Algorithms, Simply Explained
Linear Regression — Teaching AI to Draw the Best Straight Line
Series 3, Part 1: ML Algorithms, Simply Explained
You’re a real estate agent. A client asks you: “How much should I list my house for?”
You don’t guess randomly. You look at similar houses — their sizes, and what they sold for. You notice a pattern: bigger houses sell for more. You mentally draw a line through that pattern and use it to estimate your client’s house price.
That’s Linear Regression. And it’s one of the oldest, simplest, and most powerful ideas in all of machine learning.
What Is Linear Regression?
Linear Regression is a supervised learning algorithm that finds the best straight line through data — and uses that line to predict a numerical output from one or more inputs.
Two key things:
- Supervised — it learns from labelled examples (inputs with known correct outputs)
- Numerical output — it predicts a number, not a category
Examples of what it predicts:
- House price from size
- Sales revenue from ad spend
- Employee salary from years of experience
- Temperature from time of year
The Simple Version — One Input
Let’s start with the simplest case: one input, one output.
You have data on 100 houses — their size (in sq ft) and their sale price.
Plot this on a graph:
- X-axis = house size
- Y-axis = sale price
- Each house = one dot
You’ll see the dots form a rough upward trend — bigger houses cost more.
Linear Regression draws the best possible straight line through those dots.
That line is described by a simple equation you’ve seen since school:
y = mx + b
Where:
- y = the predicted output (house price)
- x = the input (house size)
- m = the slope (how much price increases per sq ft)
- b = the intercept (the base price when size = 0)

Example: After training, the model might find:
Price = 4,500 × Size + 10,00,000
This means:
- Every additional sq ft adds ₹4,500 to the price
- Even at zero sq ft, there’s a base value of ₹10,00,000 (land value, location etc.)
Now when a new house comes in — say 1,200 sq ft:
Price = 4,500 × 1,200 + 10,00,000 = ₹54,00,000 + ₹10,00,000 = ₹64,00,000
How Does It Find the Best Line?
There are infinite possible straight lines you could draw through the data. How does Linear Regression find the best one?
By minimizing the prediction errors — the gaps between the line’s predictions and the actual values.
These gaps are called residuals.
The goal: Find the line where the total residual error is as small as possible.
How it measures error: Using Mean Squared Error (MSE) — which we covered in Series 2, Part 9.
MSE = (1/n) × Σ(Predicted − Actual)²
The algorithm adjusts the slope (m) and intercept (b) — using gradient descent (Series 2, Part 6) — until MSE is minimized.
The result is the line of best fit — the straight line that comes closest to all data points simultaneously.

The line doesn’t pass through every dot — but it minimizes the total distance from all dots.
The Multiple Version — Many Inputs
Real house prices don’t depend on size alone. They depend on:
- Size
- Number of bedrooms
- Location
- Age of building
- Nearby schools
- Floor number
Multiple Linear Regression handles all of these simultaneously:
Price = (W1 × Size) + (W2 × Bedrooms) + (W3 × Location Score) + (W4 × Age) + b
Each input gets its own weight — representing how much it influences the price.

The model learns these weights automatically from training data — finding the combination that best predicts the actual prices.
The math, kept simple:
y = W1X1 + W2X2 + W3X3 + … + WnXn + b
Same idea as simple linear regression — just more inputs, more weights, same goal: minimize prediction error.
Assumptions of Linear Regression
Linear Regression works well only when certain conditions hold:
1. The relationship is actually linear If the real relationship curves — a straight line won’t capture it well. Predicting something that grows exponentially with a straight line will fail.
2. Features are independent If two inputs are highly correlated — say “number of rooms” and “house size” — the model gets confused about which one is actually driving the price. This is called multicollinearity.
3. No extreme outliers One extremely unusual data point can pull the line significantly — distorting predictions for everyone else. Because MSE squares errors — outliers have outsized influence.
4. Constant variance in errors The spread of prediction errors should be roughly the same across all input values. If errors grow as the input grows — the model is unreliable at higher values.
Real World Business Use Cases
Sales Forecasting Input: advertising spend, seasonality, number of sales reps Output: predicted monthly revenue
HR & Compensation Input: years of experience, education level, role Output: predicted salary range for a new hire
Demand Planning Input: price, season, competitor pricing Output: predicted units sold
Financial Modelling Input: interest rates, GDP growth, consumer confidence Output: predicted loan default rate
Healthcare Input: patient age, BMI, blood pressure Output: predicted risk score for a condition
When to Use Linear Regression
✅ Use it when:
- Your output is a continuous number
- The relationship between inputs and output is roughly linear
- You need an interpretable model — weights tell you exactly how each feature affects the prediction
- You have limited data — linear regression works well even with small datasets
- You need fast training and prediction
❌ Don’t use it when:
- Your output is a category (yes/no, spam/not spam) — use Logistic Regression instead
- The relationship is clearly non-linear
- You have extremely high-dimensional data with complex interactions
- Prediction accuracy is more important than interpretability
Pros and Cons

Quick Recap

Next up → Series 3, Part 2: Logistic Regression — despite the name, it’s not regression at all. It’s the go-to algorithm for yes/no predictions — and understanding it reveals how AI makes binary decisions.
Series 3 covers the core ML algorithms that power real-world AI systems — explained simply, with real business examples. Follow along and share with someone building their AI knowledge.
메타데이터
- post_id
- e1e16e43033c
- slug
- linear-regression-teaching-ai-to-draw-the-best-straight-line-e1e16e43033c
- url
- https://medium.com/@velpragathi673/linear-regression-teaching-ai-to-draw-the-best-straight-line-e1e16e43033c
- canonical_url
- https://medium.com/@velpragathi673/linear-regression-teaching-ai-to-draw-the-best-straight-line-e1e16e43033c
- author_url
- https://medium.com/@velpragathi673
- status
- ok
- fetched_at
- 2026-08-05 10:16:18