But they'll all be reasoning the same way. They know bigger flats cost more, they've seen what similar flats sold for, and they're mentally drawing a line through those sales to guess where yours falls.
That mental line is what linear regression does, only with arithmetic instead of guesswork. It's one of the oldest and most useful tools in machine learning, and it's often the first one people learn. Let's build one by hand with five houses and see how the formula works.
What Is Linear Regression in Machine Learning?
Linear regression is a method for predicting a number by finding the straight-line relationship between that number and one or more things you already know. In linear regression in machine learning, the model studies past examples, finds the line that fits them best, and then uses that line to predict new cases.
The thing you're predicting, like price, is called the target or dependent variable. The things you use to predict it, like area, are called features or independent variables. Because the answer is a number on a scale rather than a category, linear regression belongs to a group of tasks called regression.
Predicting a House Price, Step by Step
Here are five made-up sales from one neighbourhood. Area is in square feet and price is in lakh rupees.
600 sq ft sold for 30. 800 sq ft sold for 38. 1,000 sq ft sold for 52. 1,200 sq ft sold for 58. 1,500 sq ft sold for 75.
Plot those on paper and the dots climb from lower left to upper right, not perfectly straight but close. Linear regression finds the one straight line that sits as near to all five dots as possible.
Doing the sums, the average area is 1,020 and the average price is 50.6. Working through the least squares method, the line comes out at roughly price equals minus 0.48 plus 0.05 times area. In plain words, every extra square foot adds about 0.05 lakh, which is around 5,000 rupees, and the line starts very slightly below zero where it crosses the price axis.
Now ask about the 1,100 square foot flat. Multiply 1,100 by 0.05 and subtract 0.48, and you get about 54.5 lakh. That's the prediction. It isn't magic and it isn't guaranteed. It's just where the line says a flat of that size should land, based on the five sales we showed it.
The Linear Regression Formula, Explained
The linear regression formula for one feature is written like this.
y = b0 + b1x
Here y is the number you're predicting, which is the price. The x is the feature you know, which is the area. The b1 is the slope, meaning how much y changes when x goes up by one. In our example it's about 0.05. The b0 is the intercept, the value of y when x is zero. In our example it's about minus 0.48, and you can ignore its odd sign, since no flat has zero area. It's just where the line is anchored.
If you've seen y = mx + c in school, it's the same thing with different letter names.
How the Model Finds the Best-Fit Line
Many lines could be drawn through those dots, so how does the model pick one? It measures the gap between each real price and the price the line predicts for that flat. These gaps are called errors or residuals. Some are above the line and some below, so the model squares each one to make them all positive, then adds them up.
The best-fit line is the one that makes that total as small as possible. That approach is called ordinary least squares. For small problems there's a direct formula that gives the answer. For bigger ones, computers use a step-by-step method called gradient descent, which nudges the slope and intercept a little at a time until the total error stops shrinking.
For our line, the gaps are small. It predicts 29.6 for the 600 sq ft flat that really sold for 30, and 49.6 for the 1,000 sq ft one that sold for 52. The biggest miss is about 2.4 lakh.
Simple vs Multiple Linear Regression
What we just did is simple linear regression, because it has one feature. In real life, price depends on more than area. Bedrooms, floor, age of the building and distance from the metro all matter.
Multiple linear regression in machine learning handles that by adding more terms.
y = b0 + b1x1 + b2x2 + b3x3 and so on
Each x is a different feature, and each b says how much that feature moves the price while the others stay the same. With more features you can't draw a simple line on paper anymore, but the idea is identical. The model finds the set of weights that keeps the overall error as small as possible.
How to Check Whether Your Prediction Is Accurate
A line that fits the five flats it was built from tells you very little. What matters is how it does on flats it has never seen. So people set aside some data before training and use it only for testing.
Then they look at a few numbers. Mean absolute error tells you how many lakh your predictions miss by, on average. Root mean squared error does something similar but punishes big misses harder. R-squared tells you what share of the ups and downs in price your model explains, with 1 being perfect and 0 meaning it's no better than guessing the average.
In our small example the average miss is about 1.3 lakh. That looks fine, but with only five houses you shouldn't trust it much. A real model would use hundreds or thousands of sales and check against a held-back set.
Linear and Logistic Regression in Machine Learning
The names sound alike, which causes confusion. Linear regression predicts a number, such as a price or a temperature. Logistic regression predicts a category, usually yes or no, such as whether an email is spam or whether a customer will cancel.
Under the hood, logistic regression takes a similar weighted sum and then squeezes the result through an S-shaped curve so it comes out as a probability between 0 and 1. If the probability is above a chosen cutoff, it says yes. So despite the name, logistic regression is used for classification. When people discuss linear and logistic regression in machine learning, that's the main difference to remember.
Where Linear Regression Shows Up in Online Business
Anything that predicts a number from other numbers can start with linear regression. A shop might forecast next month's revenue from advertising spend and past sales. A team running automated marketing campaigns can use it to estimate how much extra revenue an extra bit of budget tends to bring. Records pulled from a payment API, such as order values and dates, give clean data to train on.
It's popular because it's quick, easy to explain and hard to misunderstand. You can look at the weights and say exactly what the model thinks matters. It does have limits. It assumes the relationship is a straight line, and it can be thrown off by odd outliers. When the pattern is curved or tangled, other models do better. But as a first attempt, it's very hard to beat.
Getting Your Data Ready
Before any line is drawn, the data needs a quick tidy. This step takes longer than the modelling itself, and it's where most beginners lose time.
Start by looking for gaps and mistakes. A flat listed at 10 square feet or a price with an extra zero will drag the line off course. Decide what to do with missing values, whether that's filling them in sensibly or leaving those rows out. Check that the units match, so you don't have some areas in square feet and others in square metres.
Features on very different scales can also be awkward. Area runs into the thousands while bedrooms run from one to four. For the basic method this doesn't change the answer, but when you use gradient descent it helps to bring them onto a similar scale so the model settles faster.
Then split the data. Keep around 70 to 80 percent for training and set the rest aside for testing. Don't peek at the test part until you're done. It's your only honest check on how the model behaves with houses it's never seen.
What Linear Regression Assumes, and When It Goes Wrong
The method works well when a few conditions roughly hold. The relationship between the feature and the target should be close to a straight line. The errors should be scattered randomly, without a clear pattern, rather than getting bigger as the price goes up. And the features shouldn't be near-copies of each other. If you include area in square feet and area in square metres, the model can't tell them apart and the weights become unreliable.
You can spot trouble by plotting the errors. If they curve, bunch up or fan out, a straight line probably isn't the right shape for your data. Price against area often fans out in real life, since the gap between cheap and expensive widens for large houses.
Outliers are another risk. One mansion in a street of small flats can tilt the whole line toward itself. It's worth checking whether such a case belongs in the data or should be handled separately.
When the pattern is curved, you've got options. You can transform the data, for example by working with the logarithm of price, or you can move to a model that can bend, such as a decision tree. But it's usually sensible to start with a plain line anyway. It sets a baseline, and if a fancier model can't beat it by much, you've saved yourself a lot of complexity.
FAQs
What is linear regression in machine learning, in simple terms?
It's a method that finds the straight line best describing how one thing changes with another, then uses that line to predict numbers. For example, it can predict a house price from its size using past sales.
How can linear regression predict a house price?
The model studies past sales, finds the line linking area to price, and then reads off the price for a new area. In our example, a 1,100 sq ft flat comes out at about 54.5 lakh.
What is the linear regression formula, and what does each term mean?
The formula is y = b0 + b1x. The y is the predicted value, x is the feature you know, b1 is the slope showing how much y changes per unit of x, and b0 is the intercept where the line starts.
What is the difference between simple and multiple linear regression?
Simple linear regression uses one feature to predict the target. Multiple linear regression uses several features at once, each with its own weight, such as area, bedrooms and building age.
How does a linear regression model find the best-fit line?
It measures the gap between each real value and the line's prediction, squares the gaps and adds them up. The best-fit line is the one with the smallest total. This is called ordinary least squares.
How can you check whether a house-price prediction is accurate?
Test the model on houses it hasn't seen. Check mean absolute error, root mean squared error and R-squared to see how far off the predictions are and how much of the variation the model explains.
What is the difference between linear and logistic regression in machine learning?
Linear regression predicts a number, like a price. Logistic regression predicts a category or probability, like yes or no, so it's used for classification despite its name.