House Price Prediction in Python
In this lesson: Build a house price prediction model with linear regression in Python — dataset, training, evaluation — full code.
What we are building
A house price predictor is a machine learning model that estimates a price from features like size and number of rooms. Because the answer is a number, this is a regression problem, and we solve it with linear regression using scikit-learn.
Regression, not classification
Spam detection sorts messages into categories — that is classification. House price prediction gives a continuous number like Rs 42,50,000 — that is regression. Linear regression learns a relationship between the inputs (features) and the output number, then uses it to predict prices for new houses.
The data (features and price)
# feature: area in sq ft -> target: price (in lakh)
X = [[500], [800], [1000], [1500]] # areas
y = [25, 40, 50, 75] # prices
Each house has features (here just area) and a known price. The model learns how area relates to price. Real datasets add bedrooms, location, age and more.
Training the model
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X, y) # learn the relationship
LinearRegression finds the best line through the data. fit(X, y) trains it, so it learns roughly how much price increases per square foot.
Predicting a price
new_house = [[1200]] # 1200 sq ft
price = model.predict(new_house)
print("Predicted price:", price[0], "lakh")
We pass the new house's features to predict, and it returns the estimated price based on the learned line. More features give more accurate predictions.
Checking the model
from sklearn.metrics import r2_score
pred = model.predict(X)
print("R2 score:", r2_score(y, pred)) # closer to 1 is better
To trust the model, measure its accuracy. r2_score close to 1 means the line fits the data well. In practice, always test on data the model has not seen.
Common mistakes
- Using classification for a number. Price is continuous — use regression, not a classifier.
- Too few features. One feature is limited; add rooms, location, age for better results.
- Not scaling features. Very different ranges (area vs rooms) can help from scaling.
- Testing on training data only. Always check accuracy on unseen houses.
Summary
- Predicting a price is regression (a number), not classification.
- Linear regression learns a line between features and price.
- Train with
model.fit(X, y), predict withmodel.predict(). - Add more features for more accurate predictions.
- Check accuracy (like R-squared) on unseen test data.
Next, compare with classification in spam detection, or learn ML types in supervised learning.
Frequently Asked Questions
What kind of machine learning problem is price prediction?
What is linear regression?
What are features in house price prediction?
How do I predict a price for a new house?
model.predict([[...features...]]) with the new house's features. It returns the predicted price based on what it learned from the training data.