multiple linear regression

Multiple Linear Regression: A Beginner-Friendly Guide with Python

Imagine two houses. Both sit on a 1,600 square foot plot. Yet one sells for $260,000, and the other sells for $310,000. Why?

Size alone can’t explain that gap. Bedrooms matter. Age matters. Location matters, too. So a single-feature model runs out of answers fast.

This is exactly where multiple linear regression comes in. It takes several inputs at once, and it blends them into one confident prediction. Better yet, it does this with almost the same code you already know from simple linear regression.

This article walks through the full idea, step by step. It also matches the companion video, EP34 on the Intelevo YouTube channel. So if you’d rather watch and follow along, hit play below and keep this page open for the code and notes.

▶ Watch the full walkthrough: EP34 – Multiple Linear Regression (Intelevo YouTube channel)

By the end, you’ll understand the concept, the math, and the code. You’ll also see how to run it yourself in Python.

The Recipe Analogy: Why This Concept Feels Simple

Think about baking a cake. One ingredient alone, say flour, can’t explain the final taste. Sugar changes it. Eggs change it. Butter changes it, too. Each ingredient pulls the result in its own direction, and the final cake reflects all of them combined.

A house price behaves the same way. Size pulls the price up. So does an extra bedroom. Meanwhile, age often pulls it down, since buyers tend to prefer newer construction. None of these ingredients act alone. Instead, they blend together into one final number.

Multiple linear regression simply gives each ingredient its own measurable pull. Once you see it this way, the math stops feeling abstract. It starts feeling like common sense, written in numbers instead of words.

A Quick Recap: Where EP33 Left Off

In EP33, we built a simple linear regression model. It used one input, house size, to predict one output, house price. The formula looked like this:

y = mx + b

Here, m is the slope, and b is the intercept. Scikit-learn found both values automatically. Three commands did all the heavy lifting: fit(), predict(), and score().

That model worked well. In fact, it explained 98% of the price pattern in our sample data. Impressive, right? But real estate rarely behaves that simply.

Why One Feature Isn’t Enough

Here’s the problem. Two houses can share the same size, yet sell for very different prices. So something else must be driving that difference.

Bedrooms count. A three-bedroom home usually beats a two-bedroom home at the same size. Age counts, too. Buyers often pay less for an older house, even if it’s just as big. Location counts as well, though we’ll keep that one for a future episode.

In short, a house’s price isn’t baked from one ingredient. It comes from a recipe. Therefore, we need a model that can taste every ingredient at once, not just one.

That’s the whole idea behind multiple linear regression. It still draws a straight-line relationship. However, it lets more than one input contribute its own share to the final answer.

What Is Multiple Linear Regression?

Multiple linear regression extends simple linear regression. Instead of one slope, it uses several. Each input gets its own weight, and every weight tells you how much that input moves the prediction.

The equation looks like this:

price = b + m1(size) + m2(bedrooms) + m3(age)

Notice the pattern. Every ingredient earns its own multiplier: m1, m2, and m3. Meanwhile, b still marks the baseline, the starting point before any ingredient gets added.

Scikit-learn finds every one of these weights automatically. As a result, you don’t calculate anything by hand. You simply feed it more columns, and it handles the rest.

The Dataset We’ll Use

Let’s expand our house-price data. This time, every house comes with four details: size, bedrooms, age, and price.

Size (sq ft)BedroomsAge (yrs)Price ($)
1,000215195,000
1,200210232,000
1,40038271,000
1,600312292,000
1,80045341,000
2,000420335,000

In Python, this becomes four simple lists:

sizes    = [1000, 1200, 1400, 1600, 1800, 2000]
bedrooms = [2, 2, 3, 3, 4, 4]
age      = [15, 10, 8, 12, 5, 20]
prices   = [195000, 232000, 271000, 292000, 341000, 335000]

Notice the pattern here, too. We haven’t changed the story. We’ve only added more detail to it.

The Three-Step Workflow: Still Just Prepare, Fit, Predict

Good news first: nothing new happens at the workflow level. The three steps from EP33 stay exactly the same.

  1. Prepare the data, so scikit-learn can read it correctly.
  2. Fit the model, so it learns every weight.
  3. Predict, so it answers questions about new houses.

Let’s walk through each step, one at a time.

Step 1: Prepare the Data

Before, our input, X, held just one column: size. Now, X needs three columns, one for every ingredient. However, every house still occupies just one row.

import numpy as np

X = list(zip(sizes, bedrooms, age))
X = np.array(X)
y = np.array(prices)

print(X.shape)
# (6, 3)  -> 6 rows, 3 columns

Why does the reshape matter? Because scikit-learn always expects rows and columns, even when you only have one feature. Now that we have three features, each one simply becomes its own column. The row count still matches the number of houses.

Step 2: Create and Fit the Model

This step should feel familiar. In fact, it’s identical to EP33.

from sklearn.linear_model import LinearRegression

model = LinearRegression()   # create an empty model
model.fit(X, y)              # find every weight automatically

Notice what changed, and what didn’t. More columns went into X. However, zero new code appeared. The model doesn’t care how many ingredients it receives. It applies the same learning process either way.

Understanding the Coefficients

So, what did the model actually learn? Let’s check.

print(model.coef_)
# [137.34, 7313.39, -2171.78]

print(model.intercept_)
# 75051.64

Each number tells its own story:

  • Size adds about $137.34 for every extra square foot.
  • Bedrooms add about $7,313.39 for every extra bedroom.
  • Age subtracts about $2,171.78 for every extra year.

That negative sign on age makes sense, too. Older houses typically sell for less, once you hold size and bedrooms steady. Meanwhile, the intercept, $75,051.64, marks the baseline price before any ingredient gets added.

This is the real payoff of multiple linear regression. You don’t just get one prediction. You get a clear weight for every input, so you can explain exactly why the model reached its answer.

Step 3: Make a Prediction

Now, let’s test the model on a brand-new house. It’s 1,700 square feet, has 3 bedrooms, and is 6 years old. This exact combination never appeared in our training data.

new_house = np.array([[1700, 3, 6]])
predicted = model.predict(new_house)

print(predicted)
# [317441.72]

The model answers with a single confident number: $317,441.72. It reached that figure by blending all three ingredients at once, exactly the way we defined in our equation.

Checking Model Accuracy with R²

Before trusting any prediction, always check the fit. R² measures how well the model explains the data. It ranges from 0 to 1. A higher score means a better fit.

print(model.score(X, y))
# 0.9998

An R² of 0.9998 is excellent. It means size, bedrooms, and age together explain almost all of the price pattern in our small dataset. So, this model has learned the relationship well.

However, keep this in mind. A near-perfect score on six rows doesn’t guarantee the same accuracy on thousands of new houses. Always test with more data before trusting a model completely.

The Complete Workflow in One Glance

Here’s every piece, combined into one script.

import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array(list(zip(sizes, bedrooms, age)))
y = np.array(prices)

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

print(model.predict([[1700, 3, 6]]))   # $317,442
print(model.score(X, y))                # 0.9998

That’s the entire solution. Ten lines, three imports, and one trained model. This is why scikit-learn remains so popular among beginners and professionals alike.

Common Mistakes to Avoid

Multiple linear regression is powerful, but it comes with a few traps. Watch out for these three.

1. Adding features that don’t help. More columns don’t automatically mean a better model. Sometimes, an unrelated feature sneaks in disguised as useful information. Instead, choose features that logically connect to your target.

2. Trusting one shiny R² score. A high R² feels reassuring. However, on a small dataset, it doesn’t guarantee every coefficient stays meaningful once you add real-world data. Always validate with a larger, separate dataset.

3. Ignoring features that move together. When two features rise and fall together, like size and bedrooms often do, their individual weights become harder to trust. This effect is called multicollinearity. For now, just remember this: correlated features can blur the story your model tells.

Why This Pattern Matters Everywhere

Multiple linear regression doesn’t stop at real estate. In fact, the exact same three steps power predictions across many industries.

  • Real estate: size, bedrooms, age, and location predict price.
  • College admissions: GPA, test scores, and activities predict outcomes.
  • Insurance: age, driving record, and coverage predict the premium.

Notice the pattern once more. One feature becomes many. Yet the process, prepare, fit, and predict, never changes. Once you understand this workflow, you can apply it almost anywhere numbers tell a story.

Quick Recap

Let’s tie everything together.

  • Prepare: Stack every ingredient into columns inside X.
  • Fit: Call model.fit(X, y). The same one line handles any number of columns.
  • Predict: Call model.predict() for instant, data-driven answers.
  • Score: Call model.score() to check how well your model actually fits.

Multiple linear regression takes the foundation from simple linear regression, and it simply scales it up. Once you see this pattern, the concept stops feeling complicated. It starts feeling logical.

Frequently Asked Questions

Does multiple linear regression need scaled features? Not strictly, but scaling often helps. When features sit on very different ranges, like square footage versus bedroom count, scaling can make coefficients easier to compare. Scikit-learn’s LinearRegression doesn’t require it, though many other algorithms do. So, it’s a good habit to build early.

How many features can I add? Technically, you can add as many as you want. However, more isn’t always better. Every extra feature adds complexity, and some features add noise instead of signal. Therefore, choose features that logically relate to your target, rather than adding everything available.

What if two features are highly correlated? This is called multicollinearity, and it can distort your coefficients. The model still predicts reasonably well, but the individual weights become harder to trust in isolation. If you notice this, consider removing one of the correlated features, or combining them into a single measure.

Is multiple linear regression the same as multivariate regression? Not quite. Multiple linear regression predicts one output from several inputs. Multivariate regression predicts several outputs at once. It’s a subtle difference, but worth remembering as you explore more advanced models.

When should I move beyond linear regression? Once your data stops following a straight-line pattern, linear models start losing accuracy. That’s exactly the situation polynomial regression solves, and it’s exactly where EP35 picks up the story.

What’s Next: EP35 – Polynomial Regression

Straight lines work well, but they have limits. Sometimes, a relationship curves instead of running flat. So, what happens then?

That question leads directly into EP35: Polynomial Regression. We’ll explore what happens when data refuses to follow a straight path, and how a slightly different equation can capture that curve.

Watch, Practice, and Stay Connected

If this article helped clarify multiple linear regression, please watch the full video walkthrough on the Intelevo YouTube channel. Seeing the code run live often makes these ideas click even faster.

While you’re there, consider a few small favors. Like the video if it helped you. Subscribe so you don’t miss future episodes. Then, drop a comment and share your thoughts, questions, or your own prediction results. Feedback like this genuinely shapes future episodes.

Thanks for reading, and see you in EP35!

Leave a Comment

Your email address will not be published. Required fields are marked *