Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 60

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.

💡 Classification predicts a label (spam / not spam). Regression predicts a number (a price).

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")
Predicted price: 60.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 with model.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?
Predicting a house price is a regression problem, because the answer is a continuous number (like Rs 45 lakh), not a category. Regression predicts numbers, while classification predicts labels like spam or not spam.
What is linear regression?
Linear regression finds the best straight-line relationship between input features (like size, rooms) and the output number (price). Once trained, it uses that line to predict prices for new houses.
What are features in house price prediction?
Features are the inputs that affect price — like area in square feet, number of bedrooms, location and age. The model learns how each feature influences the final price.
How do I predict a price for a new house?
After training the model, call model.predict([[...features...]]) with the new house's features. It returns the predicted price based on what it learned from the training data.
How do I know if the model is accurate?
Split data into training and test sets, and check the error (like R-squared or mean squared error) on the test set. A small error on unseen houses means the model predicts well.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicSimple Chatbot Your progress: 60 / 66 (91%) · Lesson 61 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free