Linear regression

Supervised · Regression Linear regression predicts a number by finding the straight line that best fits past data. The line is a simple formula, such as price = base amount + (rate × size). "Best fit" means the line where the total error between the line and the real points is as small as possible.

Everyday analogy

An auto-rickshaw fare: a fixed base fare plus a rate per kilometre. Linear regression looks at many past trips and works out the base fare and the per-km rate on its own.

Example

Used for: house and flat prices, sales forecasts from advertising spend, estimating delivery time, predicting electricity use.

House size →Priceprediction for a new house
Linear regression draws the best straight line through the points, then reads predictions off it.

The best-fit line

Flat size (sq ft) →Price →▲ predicted priceDots = past flats · Dashes = errors

How it works, step by step

  1. Collect past examples: each flat's size and its actual price.
  2. Draw a straight line through the points.
  3. Measure how far each point is from the line. These gaps are the errors.
  4. Tilt and shift the line until the total of the squared errors is the smallest possible. This is called least squares.
  5. Use the final line to predict the price of any new flat from its size.

Worked example

Past flats: 600 sq ft sold for ₹30 lakh, 800 sq ft for ₹40 lakh, 1,000 sq ft for ₹50 lakh. The best line is price = ₹5,000 × size.

New flat of 900 sq ft → 900 × ₹5,000 = ₹45 lakh. Real data is messier, so the line is the best compromise through all the points. With more features (location, floor, age) it becomes multiple linear regression.

✓ Good for

  • Simple, fast and easy to explain
  • A great first model to compare others against
  • Shows how much each feature matters

⚠ Watch out for

  • Assumes a straight-line relationship
  • A few extreme values (outliers) can pull the line badly
  • Can't capture complex patterns on its own

Try it yourself

About 15 min

Draw a best-fit line in a spreadsheet

  1. Type 8 rows: house size and price (made-up numbers that roughly rise together).
  2. Insert a scatter chart and add a linear trendline.
  3. Use the line to predict the price of a new size.

What you should see: You just trained and used a linear regression model.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.

Build it for real · guided projectYour first machine learning model: predict house pricesAbout 75 min · Copy-paste code · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.