I'll explain the housing price prediction code step by step in simple terms:
1. Importing Libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import locale
- pandas (pd): Helps manage data in tables (DataFrames)
- numpy (np): Handles mathematical operations
- train_test_split: Divides data into training and testing sets
- LinearRegression: The actual prediction model
- metrics: Tools to measure how accurate our predictions are
- locale: Helps format currency in Indian style (₹)
2. Setting Up Indian Currency Format
locale.setlocale(locale.LC_ALL, 'en_IN')
This tells the program to display money in Indian format (₹50,00,000 instead of ₹5,000,000)
3. Creating Sample Housing Data
data = {
'square_footage': [1000, 1200, 1500, 1800, 2000, 2200, 2500, 2800, 3000, 3500],
'bedrooms': [2, 2, 3, 3, 3, 4, 4, 4, 5, 5],
'bathrooms': [1, 1.5, 2, 2, 2.5, 2.5, 3, 3, 3.5, 4],
'price_inr': [3500000, 4200000, 5500000, 6800000, 7500000, 8200000, 9500000, 10500000, 11500000, 13000000]
}
df = pd.DataFrame(data)
This creates a table with 10 houses, each with:
- Square footage (area in square feet)
- Number of bedrooms
- Number of bathrooms
- Price in Indian Rupees
4. Preparing Data for the Model
X = df[['square_footage', 'bedrooms', 'bathrooms']]
y = df['price_inr']
- X: Contains the house features (what we know)
- y: Contains the prices (what we want to predict)
5. Splitting Data into Training and Testing Sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
- Uses 80% of data to train the model
- Keeps 20% to test how accurate it is
random_state=42 ensures we get the same split every time we run the code
6. Creating and Training the Model
model = LinearRegression()
model.fit(X_train, y_train)
- Creates a linear regression model (finds the best line that fits our data)
- Trains it using our training data to learn the relationship between features and prices
7. Testing the Model's Accuracy
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
- Predicts prices for the test data
- Calculates how far off our predictions are:
- MAE: Average error in rupees
- RMSE: Similar to MAE but penalizes big mistakes more
- R²: How well our model explains price variations (1.0 = perfect)
8. User Input Function
def predict_house_price():
# Get user input
sq_ft = int(input("Square footage: "))
bedrooms = int(input("Number of bedrooms: "))
bathrooms = int(input("Number of bathrooms: "))
# Create input DataFrame
user_input = pd.DataFrame({
'square_footage': [sq_ft],
'bedrooms': [bedrooms],
'bathrooms': [bathrooms]
})
# Predict and format the result
predicted_price = model.predict(user_input)[0]
formatted_price = locale.currency(predicted_price, grouping=True)
print(f"\nPredicted House Price: {formatted_price}")
This function:
- Asks the user for house details
- Creates a small table with just this one house
- Uses our trained model to predict its price
- Displays the price in Indian Rupees format
How It Works in Simple Terms:
- The model learns patterns from existing house data
- It figures out how much each feature (area, bedrooms, bathrooms) affects price
- When you enter details of a new house, it uses these patterns to estimate a price
For example, it might learn that:
- Each square foot adds ₹3,000 to the price
- Each bedroom adds ₹5,00,000
- Each bathroom adds ₹3,50,000
So a 2000 sq ft house with 3 bedrooms and 2 bathrooms might be predicted as:
₹60,00,000 (base) + ₹15,00,000 (bedrooms) + ₹7,00,000 (bathrooms) = ₹82,00,000
The actual formula is more complex, but this gives you the basic idea of how linear regression works for house price prediction.