DEV Community

Cover image for Linear Regression: Putting things in line
the_undefined_architect
the_undefined_architect

Posted on

Linear Regression: Putting things in line

Like every developer knows, Hello World is famously the first application you build when you start learning how to code. In the same spirit, house price prediction is one of the best beginner examples for understanding how machine learning models are trained and used.

Say you want to build an app that predicts house prices. How would you do it? There is no simple if-else statement that can accurately predict the price of a house in your neighborhood.

So let’s simplify the problem.

Imagine you collected data for 10 houses and wanted to explore whether house size can help us predict house price. Your dataset might look like this:

Dataset of house size-price

If we plot these points on an X and Y axis, this is what we get:

The dataset plotted on a scatterplot

As we can see, a correlation emerges between the size of the house and its price (duh).
But how can we define this relationship so that, given a real value of X (the house size), we can predict the price?

This is where Linear Regression comes in.

Instead of trying to match every point perfectly, linear regression finds a line, called the regression line, that best represents the overall trend in the data.

Here’s what that looks like:

The regression line represents the overall trend

Now that we have our regression line, we can actually use it to make predictions.

Let’s say we want to estimate the price of a house that is 250 m².

We simply take that value (X = 250), project it onto our regression line, and get the predicted price:

Predicting price

And that’s it — we’ve trained our first model for predicting house prices.

I know, I know… we didn’t go into how the model actually finds this line, or how to implement it in code. We’ll get there in the next post.

For now, the goal was to give you an intuition for how Machine Learning works:

  • how models learn from data
  • how training shapes their behavior
  • and most importantly — that there is no certainty, only probability and prediction

Top comments (0)