Site icon vanitaai.com

Linear Regression for Beginners


What is Linear Regression?

Imagine you’re running a small chai shop, and you start noticing something:

The more hours your shop stays open, the more cups of chai you sell.

Now you start wondering:

“Can I predict how many cups I’ll sell if I keep the shop open for, say, 10 hours?”

This is where Linear Regression comes in.

Linear Regression is a method to:

Find the relationship between two things– one that you control (input) and one that you observe (output) and then use that relationship to make predictions.


Real-Life Example: Chai Shop

Let’s say you collect this data over a week:

Hours OpenCups Sold
2 hours20 cups
4 hours40 cups
6 hours60 cups
8 hours80 cups

You can clearly see a pattern:

Each hour = 10 extra cups sold.

If we draw this on a graph, the points fall in a straight line. That line is called the regression line.

Prediction:

Now, if someone asks you:

“If I stay open for 10 hours, how many cups will I sell?”

You say:

Cups Sold = 10 × Hours Open
Cups Sold = 10 × 10 = 100

That’s Linear Regression using past data to draw a line and predict the future.


In Technical Terms

In our case:


Where Is Linear Regression Used?


🧑‍💻 Code Example

from sklearn.linear_model import LinearRegression

# Data
hours = [[2], [4], [6], [8]]
cups = [20, 40, 60, 80]

# Model
model = LinearRegression()
model.fit(hours, cups)

# Predict
model.predict([[10]])  # Output: [100.]

Summary

Exit mobile version