Linear Regression: Finding the Best Line
Can we estimate rent from the size of a flat?
Suppose you are looking at flats in Pune. You know the size of a few flats and the monthly rent they actually got. A 500 square foot flat rented for ₹18,000, a 700 square foot flat for ₹24,000, a 900 square foot flat for ₹31,000, and a 1,100 square foot flat for ₹37,000.
Now somebody asks about a 750 square foot flat. You do not have its rent yet, but the four examples suggest that larger flats usually cost more. Could we draw a line through those examples and use it to make a sensible estimate?
That is the first model you will build in this chapter. It will not know anything about Pune, bedrooms, or balconies. It will only learn the relationship between one input, the flat size, and one target, the rent.
The idea: a line that makes useful guesses
A straight line gives us a simple rule for turning a size into a predicted rent. It goes up when the flat gets bigger, and the amount it goes up by stays the same for every extra square foot. Real rents are more complicated than that, but a simple rule is a useful place to start.
The model does not need to pass exactly through every example. Real data is noisy: two flats with the same area can have different rents because of their neighbourhood, furnishing, floor, and many things our one input cannot see. We want a line that is close to all the examples overall.
Tip:Linear regression means using a straight-line relationship to predict a number. Regression is the broader name for predicting a quantity, such as rent, delivery time, temperature, or a cricket player's score. Linear tells you the shape of the rule we are using.
The line has two adjustable parts
You may remember the line from school: y = mx + b. In machine learning we usually write the prediction as ŷ = wx + b. The small hat on y means "the value we predicted", not the real value that eventually happens.
Key Points
- x is the input feature. Here, x is the flat size in square feet.
- ŷ is the predicted target. Here, ŷ is the predicted monthly rent.
- w is the weight, also called the slope. It says how much the prediction changes when x increases by one unit.
- b is the bias, also called the intercept. It is the prediction when x is zero, even if x = 0 is not a realistic flat size.
The model starts with values for w and b, then adjusts them until the line fits the examples reasonably well. Learning a linear model means finding useful values for these two numbers.
A small example we can check by hand
For our four flats, a useful line is approximately ŷ = 32x + 1,900. The units matter: the model adds about ₹32 for every extra square foot, then starts with an intercept of ₹1,900.
For a 750 square foot flat, the calculation is 32 × 750 + 1,900 = ₹25,900. That is not a promise about the rent. It is the model's estimate based only on size and the examples it saw.
1import numpy as np
2
3sizes = np.array([500, 700, 900, 1100])
4rents = np.array([18000, 24000, 31000, 37000])
5
6w = 32
7b = 1900
8predicted_rent = w * 750 + b
9
10print(f"Estimated rent: ₹{predicted_rent:,.0f}")Estimated rent: ₹25,900
Notice what this code does not do yet. It does not discover 32 and 1,900. We wrote those values ourselves after finding a good line. The next lesson will ask how to measure whether a line is good, and later in the chapter you will see how gradient descent can find the values automatically.
See the line instead of guessing
A plot makes the idea easier to trust. The dots are the flats we have already seen. The line is the rule the model uses for a new flat. A dot does not need to sit exactly on the line for the line to be useful.
1import matplotlib.pyplot as plt
2
3line_rents = w * sizes + b
4
5plt.scatter(sizes, rents, label="Real flats")
6plt.plot(sizes, line_rents, color="orange", label="Model line")
7plt.xlabel("Size (square feet)")
8plt.ylabel("Monthly rent (₹)")
9plt.legend()
10plt.show()A scatter plot of the four flats with an upward orange line through them.
The vertical gap between a dot and the line is the model's error for that flat. We will give those gaps a single number in the next lesson, so we can compare one line with another instead of relying only on our eyes.
Let scikit-learn find the line
In real work you will not usually choose w and b by hand. scikit-learn can find a line from the examples. As in Chapter 2, the pattern is create, fit, and predict. The important new word is fit: the model looks at the training examples and learns the parameters that make its predictions as close as possible overall.
1import numpy as np
2from sklearn.linear_model import LinearRegression
3
4sizes = np.array([[500], [700], [900], [1100]])
5rents = np.array([18000, 24000, 31000, 37000])
6
7model = LinearRegression()
8model.fit(sizes, rents)
9
10new_flat = np.array([[750]])
11prediction = model.predict(new_flat)[0]
12
13print(f"Slope: ₹{model.coef_[0]:.1f} per square foot")
14print(f"Intercept: ₹{model.intercept_:,.0f}")
15print(f"Estimated rent: ₹{prediction:,.0f}")Slope: ₹32.0 per square foot Intercept: ₹1,900 Estimated rent: ₹25,900
The double brackets around sizes are deliberate. scikit-learn expects X, the features, to be a two-dimensional table with one row per example and one column per feature. Even when there is only one feature, [[500], [700], [900], [1100]] means four rows and one column.
This model has learned from only four flats and only one feature. It is a useful first experiment, not a rent-setting product. A good model is not just a line that can make a number. It is a line whose mistakes you have measured and whose limits you understand.
Where people get confused
Key Points
- "Linear regression predicts a category." No. Regression predicts a number, such as rent or delivery time. Predicting labels such as late or on time is classification, which comes later.
- "The line must pass through every training point." Usually not. Real examples contain noise, and forcing a line through every point can make it less useful for new examples.
- "The intercept is always a real-world starting price." Not necessarily. It is the prediction at x = 0, which may be outside the range of values that make sense in the problem.
- "A high slope proves that size causes rent to rise." A slope describes the relationship the model found. It does not by itself prove cause and effect, especially when other features are missing.
Quick check
In ŷ = wx + b, what do w and b control?
w controls the slope, or how much the prediction changes when the input changes. b is the intercept, the prediction when the input is zero.
What would this model predict for a 1,000 square foot flat?
Using ŷ = 32x + 1,900, it predicts 32 × 1,000 + 1,900 = ₹33,900.
Why should you not call ₹25,900 the guaranteed rent for the 750 square foot flat?
The estimate uses only size and four examples. Location, furnishing, floor, timing, and other missing features can change the real rent, and the model will have some error even on a useful relationship.
Recap
Key Points
- Linear regression predicts a number by fitting a straight-line relationship between features and a target.
- The line is written as ŷ = wx + b. w is the slope or weight, and b is the intercept or bias.
- The line does not need to pass through every example. It needs to make useful predictions on new examples.
- fit learns the line's parameters from examples, and predict uses the learned line on a new input.
- A prediction is an estimate, not a guarantee. You need to measure its mistakes before trusting it.
We can now draw a line and use it to make a prediction. But how do we decide whether this line is better than another one? Next, we will turn all those vertical gaps into one number: the cost of the model.
Theia
Can you explain this concept?
Practise Linear Regression: Finding the Best Line with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.