DEV Community

Cover image for How Linear Regression Works: Predictive ML Explained with Python Demo
Thabasvini
Thabasvini

Posted on

How Linear Regression Works: Predictive ML Explained with Python Demo

What if a machine could predict your salary just by looking at your experience?

Well that what we call as Linear Regression, one of the most fundamental yet powerful algorithms in machine learning.

In this post, I’ll explain how linear regression works from intuition to code, complete with a real Python demo and visuals.

What Is Linear Regression?

Linear regression is a way for machines to predict a number (continuous value) based on past data.

Think of it like drawing the best-fitting straight line through your data points, and then using that line to make future predictions.

Real-life examples

  • Predicting house price based on area

  • Estimating sales based on ad spend

  • Calculating salary based on experience

Mathematically, it looks like this,

Y = mX + b

Here’s a simple scatter plot with a regression line that represents this idea,

Salary Prediction Linear Regression Plot

The Intuition Behind It

Let’s say you have the following dataset,

Experience salary table

Can you guess the salary for someone with 5 years of experience?

Probably ₹50,000?

That’s exactly what linear regression does, it finds the trend in past data and uses it to predict future values.

Intuition Plot

Linear Regression in Python

#Training the model

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

X = [[1], [2], [3], [4], [5]]
y = [30000, 35000, 40000, 45000, 50000]

model = LinearRegression()
model.fit(X, y)

#Predicting the salary

prediction = model.predict([[6]])
print("Predicted salary for 6 years experience:", prediction[0])

Enter fullscreen mode Exit fullscreen mode

Output:
Predicted salary for 6 years experience: 55000.0

Real-World Use Cases

Linear regression is widely used in industries,

  1. Finance → Predicting stock price trends
  2. Marketing → Estimating ROI from ad spend
  3. Real Estate → Pricing homes based on area/location
  4. Sales Forecasting → Projecting revenue

So yes, Linear regression is the gateway to machine learning.

Need a full YouTube video explanation on Decision Tree,

I build lightweight, fully tailored AI models that run directly on your own data, whether it's spreadsheets, CSVs, or CRM exports.

Whether you're in product, growth, support, or sales, you’ll get clear, AI-powered insights to automate decisions and save time.

Want to see what this looks like?
Check out SiteEncoders, where we turn AI ideas into real working solutions.

Check out our Blog-Page, How AI Can Make Smart Business Decisions ---> Automatically

Top comments (0)