Final Model Leaderboard, Held-Out Test Set
545 properties · 12 features
1
Linear RegressionThe simplest model, deployed to production
898,804RMSE (₹)
0.7625R² Score
Best · Deployed
2
Random Forest Regressor100 trees, bootstrap aggregation
936,839RMSE (₹)
0.7419R² Score
Close second
3
Decision Tree RegressorUnconstrained single tree
1,706,019RMSE (₹)
0.1442R² Score
Overfit
Abstract
This report presents a complete, end-to-end machine learning pipeline developed for the purpose of house price prediction using a real-world housing dataset containing 545 records and 13 features. Unlike isolated model training exercises, this project was designed to demonstrate the full lifecycle of a deployment-ready machine learning solution, encompassing every major stage from raw data ingestion to interactive web-based deployment.
The dataset comprises both numerical and categorical features. Extensive exploratory data analysis was conducted to understand feature distributions, identify relationships between variables, and extract early insights about price determinants. Categorical variables were encoded using LabelEncoder, and all features were normalized using StandardScaler prior to model training.
Three supervised regression algorithms were trained and evaluated: Linear Regression, Decision Tree Regressor, and Random Forest Regressor. Model performance was assessed using RMSE and R² score on a held-out test set, followed by 5-Fold Cross Validation to obtain a generalization estimate. Hyperparameter tuning was performed by systematically varying the number of estimators and maximum depth of the Random Forest to explore performance sensitivity.
Contrary to initial expectations, Linear Regression outperformed both tree-based methods, achieving an R² of 0.762 and an RMSE of 898,804. This result strongly suggests that the housing dataset exhibits predominantly linear relationships, and that simpler models generalize better under such conditions. This finding reinforces a fundamental machine learning principle: model complexity is not inherently beneficial, and should always be justified by the nature of the data.
A reusable Scikit-Learn Pipeline was constructed to encapsulate preprocessing and model inference in a reproducible workflow. The trained pipeline was serialized using Joblib for deployment readiness. A web-based prediction interface was developed using HTML, CSS, JavaScript, and FastAPI, allowing users to interactively input house features and receive instant price predictions along with contextual information such as property classification and market position indicators.
Feature importance analysis conducted using the Random Forest model revealed that property area is the dominant predictor, contributing approximately 45.7% of the model's predictive power. Bathroom count emerged as the second most influential feature, followed by parking availability and number of stories. These findings align well with real-estate domain knowledge and were consistent with correlation patterns identified during exploratory data analysis.
This project demonstrates that a well-structured, reproducible ML pipeline is more valuable than a collection of standalone model experiments. Every stage of the workflow has been documented, analyzed, and justified, producing a system that is not merely functional but explainable, deployable, and scalable.
Chapter 01
Introduction
1.1 Problem Statement
Machine Learning projects often fail due to lack of structured pipelines.
1.2 Objectives
- Load and validate a real-world housing dataset consisting of 545 records and 13 attributes.
- Perform comprehensive Exploratory Data Analysis.
- Apply appropriate preprocessing techniques including categorical encoding and feature scaling.
- Train, evaluate, and compare three supervised regression algorithms: Linear Regression, Decision Tree Regressor, and Random Forest Regressor.
- Perform 5-Fold Cross Validation to obtain a realistic estimate of model generalization performance.
- Analyze feature importance to identify the most significant predictors of house price.
- Construct a reusable Scikit-Learn Pipeline encapsulating preprocessing and inference.
- Perform hyperparameter tuning to explore performance sensitivity of the Random Forest model.
- Serialize the trained pipeline using Joblib for persistent storage and deployment readiness.
- Develop a web-based prediction interface using HTML, CSS, JavaScript, and FastAPI.
- Provide contextual output including property classification and dataset position indicator.
Chapter 02
Dataset Description
2.1 Dataset Overview
The dataset used in this project is the Housing Price Prediction Dataset, a structured tabular dataset comprising information about residential properties. The dataset contains 545 rows and 13 columns, where each row represents a single property and each column represents an attribute or feature of that property. The target variable is the house price, expressed as a continuous numerical value in Indian Rupees.
It contains a meaningful mix of numerical and categorical features that reflect real-world property attributes, it is small enough to enable rapid experimentation while large enough to support statistically meaningful model evaluation.
2.2 Feature Description
The dataset consists of 13 columns. Six columns contain numerical data and seven contain categorical data.
| Feature Name | Data Type | Description |
| price | Numerical (int) | Target variable. Sale price of the property in Indian Rupees. |
| area | Numerical (int) | Total built-up area of the property in square feet. |
| bedrooms | Numerical (int) | Number of bedrooms in the property (range: 1 to 6). |
| bathrooms | Numerical (int) | Number of bathrooms in the property (range: 1 to 4). |
| stories | Numerical (int) | Number of floors/stories in the property (range: 1 to 4). |
| parking | Numerical (int) | Number of available parking spaces (range: 0 to 3). |
| mainroad | Categorical (yes/no) | Whether the property faces a main road. |
| guestroom | Categorical (yes/no) | Whether the property has a guest room. |
| basement | Categorical (yes/no) | Whether the property has a basement. |
| hotwaterheating | Categorical (yes/no) | Whether the property has hot water heating. |
| airconditioning | Categorical (yes/no) | Whether the property has air conditioning. |
| prefarea | Categorical (yes/no) | Whether the property is in a preferred/premium area. |
| furnishingstatus | Categorical | Furnishing level: furnished, semi-furnished, or unfurnished. |
Table 2.2: Feature Descriptions and Data Types
2.3 Statistical Summary
Descriptive statistics were computed using the describe() method to characterize the distribution of numerical features. Key observations from the statistical summary are presented in the table below.
| Feature | Min | Max | Mean | Std Dev | Median |
| price | 1,750,000 | 13,300,000 | 4,766,729 | 1,870,440 | 4,340,000 |
| area | 1,650 | 16,200 | 5,150.5 | 2,170.1 | 4,600 |
| bedrooms | 1 | 6 | 2.97 | 0.74 | 3 |
| bathrooms | 1 | 4 | 1.29 | 0.50 | 1 |
| stories | 1 | 4 | 1.81 | 0.87 | 2 |
| parking | 0 | 3 | 0.69 | 0.86 | 0 |
Table 2.3: Statistical Summary of Numerical Features
2.4 Missing Value Analysis
One of the first and most critical steps in any data preparation workflow is assessing the completeness of the dataset. The isnull().sum() function was applied to every column to identify the presence of missing values. The result confirmed that all 13 columns contained exactly 545 non-null entries, meaning no missing values were present in the dataset.
This is a significant positive finding. Missing data introduces complexity into the preprocessing pipeline, typically requiring imputation strategies that carry their own assumptions and risks. The absence of missing values in this dataset meant that the preprocessing workflow could proceed directly to encoding and scaling, without any risk of information loss or artificial data introduction. This was explicitly noted during the data exploration phase and is worth highlighting as a factor that reduced preprocessing complexity and improved data integrity.
Chapter 03
Technology Stack
3.1 Core Libraries
| Library / Tool | Role in Project |
| Python 3.x | Primary programming language. Selected for its mature ML ecosystem, readable syntax, and broad community support. |
| Pandas | Data loading, inspection, manipulation, and groupby analysis. Used for reading the CSV dataset, computing descriptive statistics, and grouping operations for EDA visualizations. |
| NumPy | Numerical operations including square root computation for RMSE and array handling during model evaluation. |
| Matplotlib | Base plotting library used for histograms, scatter plots, and bar charts across data exploration and model evaluation sections. |
| Seaborn | Statistical visualization library used for the correlation heatmap and count plots. Provides styled, information-dense plots with minimal code. |
| Scikit-Learn | Primary machine learning library. Used for preprocessing (LabelEncoder, StandardScaler), model training (LinearRegression, DecisionTreeRegressor, RandomForestRegressor), evaluation (mean_squared_error, r2_score), cross-validation (KFold, cross_val_score), and pipeline construction (Pipeline). |
| Joblib | Model serialization library. Used to save the trained Scikit-Learn Pipeline object to disk as a .pkl file for subsequent deployment. |
| FastAPI | Python web framework used to create a REST API that receives feature inputs from the frontend and returns predictions from the loaded model. |
| HTML / CSS / JS | Frontend technologies used to build the interactive prediction interface. The interface collects user input, sends API requests, and displays prediction results with additional contextual information. |
Table 3.1: Technology Stack and Library Roles
Chapter 04
Exploratory Data Analysis
4.1 Overview of EDA Process
Exploratory Data Analysis forms the foundation of any responsible machine learning project. Before selecting algorithms or engineering features, it is essential to understand the data's structure, distribution, and internal relationships. In this project, EDA was conducted systematically using a combination of descriptive statistics and visualizations, covering univariate distributions, bivariate relationships, and multivariate correlation analysis.
The EDA phase was instrumental in forming hypotheses about feature relevance, identifying potential challenges in modeling (such as skewed distributions or weak predictors), and guiding later decisions about preprocessing and model selection. Importantly, several EDA findings were later validated by the Random Forest feature importance analysis, demonstrating consistency between exploratory observations and model-derived insights.
4.2 Distribution of House Prices
The histogram of house prices reveals a distinctly right-skewed distribution. The majority of properties are concentrated in the lower price range, with the peak frequency occurring between approximately 3.0 and 4.5 million rupees. As prices increase beyond this central cluster, the frequency drops sharply, with only a small number of properties priced above 10 million rupees.
From a modeling perspective, the skewed distribution means that regression models trained on raw price values may be pulled toward the extremes. This was considered during model evaluation, particularly when interpreting RMSE, which is more sensitive to large errors in the high-price region.
4.3 Area vs. House Price
The scatter plot of property area against price reveals the most clearly identifiable positive trend among all features. While considerable scatter is present, indicating that area alone does not perfectly determine price, the general direction is unmistakable: larger properties tend to command higher prices. The highest concentration of data points lies in the area range of 1,500 to 7,000 square feet, corresponding to prices mostly between 2 million and 7 million rupees. As area increases beyond 7,000 square feet, data becomes sparser but prices remain elevated, with some properties in the 9,000 to 16,000 square foot range reaching the dataset's highest price points.
The scatter in the lower area range is notable. Several small properties have relatively high prices, suggesting that factors other than area can significantly elevate the price of a physically compact property. This reinforces the importance of a multi-feature model rather than a simple univariate relationship.
The correlation coefficient between area and price was confirmed to be 0.54, the highest single-feature correlation in the dataset. This observation aligned with the subsequent Random Forest feature importance analysis, which ranked area as the dominant predictor with 45.7% importance.
4.4 Bedrooms vs. House Price
The bedrooms scatter plot presents a discrete distribution along the x-axis, with price values spread vertically within each category. Properties with 3 and 4 bedrooms constitute the largest groups, consistent with the dataset's mean bedroom count of approximately 2.97.
A general trend of increasing median price with bedroom count is visible, though the effect is less pronounced than area. Notably, properties with 4 bedrooms show the widest price range, from approximately 2 million to 13 million rupees, suggesting that within this category, other features play a significant differentiating role.
The correlation between bedrooms and price was 0.37, moderate but not dominant. This suggests that bedroom count is a meaningful feature but one that interacts significantly with other variables rather than acting as an independent price driver. Interestingly, bedroom count ranked 7th in Random Forest feature importance with 4.08%, behind area, bathrooms, parking, stories, furnishing status, and air conditioning.
4.5 Bathrooms vs. House Price
The bathrooms scatter plot demonstrates a clearer positive relationship with price than bedrooms, despite bathrooms having a narrower range (1 to 4). Properties with 2 bathrooms form the largest group and span the full price range, indicating that this category is not cleanly associated with a specific price tier.
Properties with 3 and 4 bathrooms are concentrated at higher price points, with several 3-bathroom and 4-bathroom properties reaching price levels above 10 million rupees. The correlation between bathrooms and price was 0.52, the second-highest correlation in the dataset. This relationship was further validated by the Random Forest importance analysis, which ranked bathrooms second with 14.9% importance.
4.6 Furnishing Status Analysis
Two separate visualizations were created for furnishing status: one showing the distribution of properties across categories, and one showing the average price per category.
The distribution chart reveals that semi-furnished properties are the most common, accounting for approximately 228 of the 545 properties, followed by unfurnished (approximately 180) and fully furnished (approximately 140).
The average price chart shows a clear and consistent relationship: fully furnished properties command the highest average price (approximately 5.5 million rupees), followed by semi-furnished (approximately 4.9 million), and unfurnished properties have the lowest average price (approximately 4.0 million). The price differential between furnished and unfurnished is approximately 1.5 million rupees, a substantial margin that confirms furnishing status as a meaningful predictor.
The negative correlation of -0.30 between furnishingstatus and price (after label encoding) reflects the encoding scheme where 0=furnished, 1=semi-furnished, 2=unfurnished. As the encoded value increases, price decreases, which is consistent with the domain expectation. This is an important interpretation note: the negative sign does not imply that furnishing is inversely related to price in the naive sense, but rather reflects the ordering of the encoded categories.
4.7 Air Conditioning Availability
The air conditioning countplot reveals that 373 of the 545 properties do not have air conditioning, while approximately 172 do. This means roughly 31% of properties in the dataset are equipped with air conditioning, representing a minority but a significant one from a price prediction standpoint.
The correlation between air conditioning and price was 0.45, the third-highest correlation in the dataset, which is notably high for a binary feature. This suggests that the presence of air conditioning is a strong marker of property quality and investment, significantly influencing perceived and actual market value. In the Random Forest model, air conditioning contributed 5.7% of the feature importance, ranking 6th overall.
4.8 Correlation Heatmap
The correlation heatmap provides a comprehensive view of pairwise linear relationships across all features in the dataset. The heatmap was generated after applying LabelEncoder to all categorical columns, converting the entire dataset into numerical form.
The top row and leftmost column of the heatmap, representing correlations with the target variable price, confirm the ordering of feature relevance established through individual scatter plots. The features with the highest positive correlations to price are: area (0.54), bathrooms (0.52), air conditioning (0.45), stories (0.42), parking (0.38), bedrooms (0.37), and preferred area (0.33). Furnishing status shows the only notable negative correlation (-0.30), attributable to the encoding scheme discussed earlier.
Several inter-feature correlations are also of interest. Bedrooms and stories show a moderate positive correlation (0.41), which makes physical sense: taller properties tend to have more rooms. Guestroom and basement show a moderate positive correlation (0.37), which may indicate that both are markers of premium construction. Parking and area have a correlation of 0.35, consistent with the expectation that larger properties are more likely to have additional parking space.
Notably, hotwaterheating is the weakest predictor of price with a correlation of only 0.09, and mainroad shows a correlation of 0.30. These observations guided feature importance interpretation in the later modeling phase.
4.9 Correlation with Price
| Feature | Correlation with Price |
| area | 0.536 |
| bathrooms | 0.518 |
| airconditioning | 0.453 |
| stories | 0.421 |
| parking | 0.384 |
| bedrooms | 0.366 |
| prefarea | 0.330 |
| mainroad | 0.297 |
| guestroom | 0.256 |
| basement | 0.187 |
| hotwaterheating | 0.093 |
| furnishingstatus | -0.305 |
Table 4.9: Correlation of Each Feature with House Price
Chapter 05
Data Preprocessing
5.1 Overview of Preprocessing Strategy
Data preprocessing is the stage at which raw dataset attributes are transformed into a format that machine learning algorithms can process effectively. For this project, preprocessing involved two primary operations: categorical encoding and feature scaling. Both decisions were made deliberately based on the characteristics of the dataset and the requirements of the algorithms selected for training.
5.2 Categorical Encoding with LabelEncoder
The dataset contains seven categorical columns: mainroad, guestroom, basement, hotwaterheating, airconditioning, prefarea, and furnishingstatus. These columns contain string values such as "yes", "no", "furnished", "semi-furnished", and "unfurnished". Since machine learning algorithms operate on numerical data, these categorical values had to be converted to integers before training could proceed.
LabelEncoder from Scikit-Learn was used for this transformation. LabelEncoder assigns a unique integer to each distinct category in a column. For binary columns (yes/no), the encoding resulted in 0 and 1. For the furnishingstatus column (which has three categories), the encoding produced values 0, 1, and 2, corresponding to the alphabetical ordering: furnished=0, semi-furnished=1, unfurnished=2.
The decision to use LabelEncoder was intentional and appropriate for this project's scope. For binary yes/no features, LabelEncoder produces the same result as one-hot encoding with the additional benefit of not expanding the feature space. For furnishingstatus, which has an implicit ordinal relationship (furnished > semi-furnished > unfurnished in terms of price), the integer encoding preserves this ordering.
It is acknowledged that more sophisticated approaches such as one-hot encoding or ordinal encoding with explicitly defined category orderings exist and may be preferable in certain contexts. However, for the purposes of this project, LabelEncoder provides a clean, beginner-accessible transformation that produces valid results. The subsequent EDA confirmation (particularly the negative correlation of furnishingstatus with price) validated the encoding behavior.
5.3 Feature Scaling with StandardScaler
After encoding, the feature matrix contained values across wildly different numerical ranges. The area feature, for example, contains values between 1,650 and 16,200, while bedrooms ranges from 1 to 6 and parking from 0 to 3. Without normalization, algorithms that are sensitive to the magnitude of input values, particularly distance-based and gradient-descent-based methods, would implicitly over-weight features with larger ranges.
StandardScaler was applied to normalize all features to zero mean and unit variance using the following transformation: z = (x − μ) / σ, where μ is the column mean and σ is the standard deviation. This transforms each feature distribution to have a mean of 0 and a standard deviation of 1, placing all features on a comparable scale regardless of their original range.
The scaler was fitted exclusively on the training set (X_train) and then applied to both the training and test sets using the transform() method. This prevents data leakage, which would occur if test set statistics influenced the scaling parameters. Using fit_transform() on training data and transform() on test data was followed in this project.
Although tree-based models such as Decision Tree and Random Forest are inherently scale-invariant (they make decisions based on thresholds rather than distances), scaling was still applied uniformly across all models. This was done to ensure consistency in the experimental setup and to simplify the pipeline construction, where a single scaler is applied before any model.
5.4 Train-Test Split
The dataset was divided into training and test sets using an 80/20 split, resulting in approximately 436 training samples and 109 test samples. The random_state parameter was set to 50 to ensure reproducibility across runs. The test set was held out entirely during the training phase and used exclusively for final model evaluation, ensuring that evaluation metrics reflect genuine out-of-sample performance rather than training set memorization.
Chapter 06
Model Development
6.1 Overview of Algorithm Selection
Three supervised regression algorithms were selected for training and evaluation: Linear Regression, Decision Tree Regressor, and Random Forest Regressor. This selection was deliberate and structured to cover a spectrum from simple linear methods to ensemble tree-based methods, enabling a meaningful comparison of complexity versus performance.
6.2 Model 1: Linear Regression
6.2.1 Algorithm Description
Linear Regression is the foundational algorithm for supervised regression tasks. It models the relationship between the input features and the target variable as a linear combination: y = β0 + β1x1 + β2x2 + ... + βnxn, where β coefficients are estimated by minimizing the Ordinary Least Squares (OLS) objective, which is the sum of squared differences between predicted and actual values.
Despite its apparent simplicity, Linear Regression makes a strong assumption about the nature of the relationship between features and target: it assumes linearity. When this assumption holds, Linear Regression can achieve results comparable to or exceeding those of far more complex models, while offering superior interpretability, training speed, and robustness.
6.2.2 Training and Results
| Metric | Value |
| RMSE | 898,804 |
| R² Score | 0.7625 |
Table 6.2.2: Linear Regression Performance Metrics
Linear Regression achieved the best performance among all three models with an R² of 0.762, meaning the model explains approximately 76.2% of the variance in house prices. The RMSE of 898,804 represents the average prediction error in rupees. For a dataset where prices range from 1.75 million to 13.3 million rupees, an RMSE of approximately 900,000 is a reasonably competitive result.
6.3 Model 2: Decision Tree Regressor
6.3.1 Algorithm Description
Decision Tree Regressor is a non-parametric algorithm that recursively partitions the feature space into regions, fitting a constant prediction value within each region. At each split, the algorithm selects the feature and threshold that minimizes the variance within the resulting child nodes. This process continues until a stopping criterion (such as maximum depth or minimum samples per leaf) is reached.
Decision Trees are inherently prone to overfitting when allowed to grow without constraints. A fully grown tree can memorize the training data by creating leaves with individual training samples, achieving zero training error but poor generalization to unseen data.
6.3.2 Training and Results
| Metric | Value |
| RMSE | 1,706,019 |
| R² Score | 0.1442 |
Table 6.3.2: Decision Tree Regressor Performance Metrics
The Decision Tree delivered significantly worse results than both other models. An R² of 0.144 indicates that the model explains only approximately 14% of variance in house prices. The RMSE of 1,706,019 is nearly double that of the Linear Regression model and represents a practically unacceptable prediction error for any real-world pricing application.
The poor performance is attributable to severe overfitting. With default parameters and no maximum depth constraint, the Decision Tree grew until each leaf contained a very small number of training samples. While this produces a model that fits the training data precisely, it fails to generalize because the fine-grained partitions it has learned reflect noise in the training set rather than underlying patterns.
Additionally, Decision Trees are sensitive to the specific training-test split. Small changes in which data points fall in the training or test set can dramatically alter the tree structure, leading to high variance in performance across different splits. This characteristic makes single Decision Trees particularly unreliable as standalone predictors, which is precisely why ensemble methods like Random Forests were developed.
6.4 Model 3: Random Forest Regressor
6.4.1 Algorithm Description
Random Forest Regressor is an ensemble learning algorithm that builds multiple Decision Trees in parallel, each trained on a bootstrap sample of the training data with a random subset of features considered at each split. The final prediction is the average of predictions across all trees. This bagging (bootstrap aggregation) strategy reduces variance compared to a single tree, making the model more robust to overfitting and more stable across different data splits.
The two key sources of randomness in Random Forest, bootstrap sampling and random feature selection at each split, ensure that individual trees are decorrelated from each other. Averaging predictions from decorrelated trees yields a lower-variance ensemble prediction than any single tree could produce.
6.4.2 Training and Results
| Metric | Value |
| RMSE | 936,839 |
| R² Score | 0.7419 |
Table 6.3: Random Forest Regressor Performance Metrics
The Random Forest achieved competitive results with an R² of 0.742 and RMSE of 936,839. While it substantially outperformed the Decision Tree as expected, it was slightly below Linear Regression in this dataset. The improvement over the Decision Tree confirms the effectiveness of ensemble averaging in reducing variance, but the inability to surpass Linear Regression reveals something important about the nature of this dataset.
Chapter 07
Model Evaluation and Comparison
7.1 Evaluation Metrics
Two primary evaluation metrics were used throughout this project:
Root Mean Squared Error (RMSE) measures the average magnitude of prediction errors in the same units as the target variable (rupees). It penalizes large errors more heavily than small ones due to the squaring operation, making it particularly relevant for house price prediction where large outliers can have significant consequences. A lower RMSE indicates better predictive accuracy.
R² Score (Coefficient of Determination) measures the proportion of variance in the target variable that is explained by the model. A value of 1.0 indicates a perfect fit, while a value of 0 indicates that the model performs no better than predicting the mean. Negative values indicate a model worse than the mean predictor. Higher R² values indicate a better fit to the data.
7.2 RMSE Comparison
The RMSE comparison bar chart clearly illustrates the relative prediction error across the three models. Linear Regression achieves the lowest RMSE at approximately 898,804, followed closely by Random Forest at 936,839. The Decision Tree's RMSE of 1,706,019 is nearly 1.9 times higher than Linear Regression, representing a dramatically worse level of prediction accuracy.
The relatively small gap between Linear Regression and Random Forest RMSE (approximately 38,000 rupees difference) suggests that both models are capturing similar underlying patterns in the data. The much larger gap to the Decision Tree confirms that uncontrolled tree growth leads to a poor generalization ability.
7.3 R² Score Comparison
The R² comparison chart reinforces the findings from the RMSE analysis. Linear Regression leads with R² = 0.762, followed by Random Forest at R² = 0.742. The Decision Tree trails dramatically at R² = 0.144.
The proximity of Linear Regression and Random Forest R² values is the most notable observation from this chart. It suggests that the additional computational complexity of the Random Forest (100 trees, averaging, bootstrap sampling) offers minimal benefit over a well-fitted linear model for this dataset. This outcome points to a fundamental property of the data: the relationships between features and price are predominantly linear in nature.
7.4 Actual vs. Predicted Price Analysis
The scatter plot of actual versus predicted prices was generated using the final model to visually evaluate prediction accuracy. In an ideal model, all points would lie exactly on the diagonal line (y = x), indicating perfect agreement between actual and predicted values. The plot displays a strong positive relationship, showing that the model successfully captures the overall pricing patterns within the dataset. Predictions for lower and mid-range properties are generally close to their actual values, where the density of data points is highest. As property prices increase, the spread of points becomes larger and some predictions deviate more noticeably from actual values. This behaviour is consistent with the right-skewed distribution of the dataset, where premium-priced properties are relatively rare and therefore provide less training information for the model. Despite this increased variability at higher prices, the overall trend confirms that the model produces reliable predictions across most of the dataset while maintaining strong generalization performance.
7.5 Consolidated Results Table
| Model | RMSE | R² Score | Rank |
| Linear Regression | 898,804 | 0.7625 | 1st (Best) |
| Random Forest Regressor | 936,839 | 0.7419 | 2nd |
| Decision Tree Regressor | 1,706,019 | 0.1442 | 3rd (Worst) |
Table 7.5: Consolidated Performance Comparison of All Models
7.6 Key Insight: Why Linear Regression Outperformed Random Forest
Key Insight
The most academically significant finding of this project is the superior performance of Linear Regression compared to Random Forest. This result is counterintuitive in an era where ensemble methods and deep learning are often assumed to be the default superior choice.
The explanation lies in the nature of the dataset. The EDA analysis revealed strong linear correlations between the most important features and price. Area (r=0.54), bathrooms (r=0.52), and air conditioning (r=0.45) all show moderate-to-strong linear relationships with the target variable. When the underlying data generation process is approximately linear, Linear Regression has a structural advantage: it is specifically designed to exploit linear relationships, while tree-based methods approximate them through piecewise constant partitioning.
Random Forest, despite being a powerful general-purpose learner, can be less efficient at capturing clean linear trends because each tree makes binary splits that approximate a continuous linear relationship with a step function. With 545 data points and 12 features, the dataset may not be large enough to allow the forest to learn the global linear structure as effectively as Linear Regression does analytically.
This result reinforces one of the most important principles in machine learning: model complexity is not inherently beneficial. The optimal model is the one that best matches the structure of the data, not the most sophisticated algorithm available. Choosing a simpler, interpretable model over a complex one, when the simpler model generalizes better, is a mark of strong ML engineering judgment rather than a limitation.
Chapter 08
Cross Validation
8.1 Purpose and Methodology
Cross validation is a model evaluation strategy designed to provide a more reliable estimate of generalization performance than a single train-test split. In a single split, the choice of which data points fall in the training or test set can meaningfully influence the measured performance, particularly in smaller datasets. Cross validation addresses this by systematically rotating the training and test partitions.
5-Fold Cross Validation was implemented using KFold from Scikit-Learn. The dataset was divided into 5 equal folds. In each of the 5 rounds, one fold was used as the test set and the remaining four folds were used for training. Performance was measured in each round using the R² metric. The mean and variance of the 5 scores provide both an estimate of expected performance and a measure of performance stability.
8.2 Cross Validation Results
| Fold | R² Score |
| Fold 1 | 0.76245 |
| Fold 2 | 0.66839 |
| Fold 3 | 0.60484 |
| Fold 4 | 0.65341 |
| Fold 5 | 0.59886 |
| Average R² | 0.65759 |
Table 8.2: 5-Fold Cross Validation Results for Linear Regression
8.3 Analysis and Interpretation
The average cross-validation R² of 0.658 is lower than the test set R² of 0.762 achieved in the holdout evaluation. This difference requires careful interpretation.
First, it is important to note that this difference is expected and does not indicate a problem with the model. A single holdout split may, by chance, produce a test set that is particularly favorable for the trained model. Cross validation, by averaging over multiple different splits, provides a less optimistic and more representative estimate of performance on genuinely unseen data.
Second, the variability across folds (ranging from 0.599 to 0.762) indicates that model performance is somewhat sensitive to which data points are used for training. This variability is consistent with a relatively small dataset of 545 points: with only about 436 training samples per fold in a 5-fold scheme, the model has limited data from which to learn, making performance somewhat dependent on which subset of patterns happens to be represented in each training partition.
Third, an average R² of 0.658 is still a meaningful positive result. It indicates that the model consistently explains approximately 66% of housing price variance across different training-test configurations, demonstrating genuine predictive capability rather than memorization of a particular split.
Cross validation is indispensable in professional ML practice because it provides the most honest estimate of how a model will perform on future data. In a real deployment scenario, the cross-validated R² of 0.658 would be the figure reported to stakeholders as the expected performance, rather than the higher holdout score of 0.762.
Chapter 09
Feature Importance Analysis
9.1 Methodology
Feature importance analysis was conducted using the trained Random Forest Regressor model. Scikit-Learn's RandomForestRegressor provides feature importances computed as the mean and standard deviation of the impurity decrease accumulated over all trees for each feature. These values, normalized to sum to 1.0, represent each feature's fractional contribution to the model's total predictive power. Features that appear near the top of more splits and reduce prediction error more substantially receive higher importance scores.
9.2 Feature Importance Results
| Rank | Feature | Importance Score | Contribution (%) |
| 1 | area | 0.4571 | 45.71% |
| 2 | bathrooms | 0.1492 | 14.92% |
| 3 | parking | 0.0674 | 6.74% |
| 4 | stories | 0.0594 | 5.94% |
| 5 | furnishingstatus | 0.0579 | 5.79% |
| 6 | airconditioning | 0.0574 | 5.74% |
| 7 | bedrooms | 0.0408 | 4.08% |
| 8 | prefarea | 0.0356 | 3.56% |
| 9 | basement | 0.0292 | 2.92% |
| 10 | hotwaterheating | 0.0181 | 1.81% |
| 11 | guestroom | 0.0172 | 1.72% |
| 12 | mainroad | 0.0106 | 1.06% |
Table 9.2.2: Complete Feature Importance Ranking from Random Forest
9.3 Analysis and Real-Estate Interpretation
The feature importance results provide both technical and domain insights.
Area dominates with 45.71% importance, nearly three times that of the next highest feature. This result confirms what was already suggested by the EDA correlation analysis (area had the highest linear correlation with price at 0.54). Bathrooms rank second at 14.92%, significantly ahead of all other features. Parking (6.74%), stories (5.94%), furnishing status (5.79%), and air conditioning (5.74%) contribute at broadly comparable levels, each adding meaningful but moderate predictive power. Bedrooms ranked 7th at 4.08%, which is lower than many might expect. The EDA revealed that bedroom count has a moderate correlation with price (0.37), but the feature importance analysis suggests its predictive value is partially redundant with area: larger properties naturally tend to have more bedrooms. When the area is already in the model, bedroom count adds relatively little new information.
Hotwaterheating, guestroom, and mainroad rank at the bottom of the importance chart. Mainroad at 1.06% suggests that proximity to a main road has minimal and possibly mixed effects on price.
The consistency between EDA correlation findings and Random Forest feature importances validates the analytical approach. The top features identified through linear correlation analysis (area, bathrooms, air conditioning) are the same features that the Random Forest identifies as most important through its non-linear tree-splitting mechanism. This cross-validation of findings from two independent analytical methods strengthens confidence in the conclusions.
Chapter 10
Scikit-Learn Pipeline Design
10.1 Motivation for Pipeline Construction
After independently training and evaluating models, a key step toward production readiness is consolidating the preprocessing and inference stages into a single, unified Pipeline object. A Scikit-Learn Pipeline chains together a sequence of transformers and a final estimator, ensuring that every input to the model passes through exactly the same preprocessing steps that were applied during training. Without a Pipeline, deploying a model requires manually recreating the scaler, applying it to input data, and then passing the result to the model. This introduces risk: the scaler must have been fitted on the same data, applied in the correct order, and its parameters must match those used during training. A Pipeline eliminates this risk by packaging the entire sequence as a single object that is fitted once and applied consistently.
10.2 Pipeline Architecture
The constructed pipeline consists of two stages:
- StandardScaler: Normalizes all input features to zero mean and unit variance, using parameters fitted on the training data.
- LinearRegression: The final estimator that receives scaled features and produces price predictions.
Note
Although the pipeline was constructed with LinearRegression as the final estimator (reflecting the final model selection decision), the same pipeline architecture was also tested with RandomForestRegressor during the hyperparameter tuning phase, achieving equivalent R² results to the standalone model, which confirms that the pipeline correctly applies preprocessing.
10.3 Pipeline R² Result
The pipeline was trained using the original feature set, with preprocessing handled internally by the StandardScaler before prediction through Linear Regression. The resulting R² was 0.762458, matching the standalone model's performance. This confirms that the Pipeline successfully encapsulates both preprocessing and prediction in a single reproducible workflow.
10.4 Model Persistence with Joblib
After training, the pipeline was serialized to disk using the joblib.dump() function, saving the complete pipeline object (including the fitted scaler and trained model) as house_price_pipeline.pkl. This .pkl file encapsulates everything needed to make predictions: the scaler's mean and variance parameters, and all model coefficients. Model persistence is a critical step for deployment readiness. Without serialization, a model must be retrained every time predictions are needed. With a saved .pkl file, the trained model can be loaded instantly using joblib.load() and used to make predictions on new data without any additional training. In the FastAPI backend (main.py), the model is loaded at application startup using joblib.load("house_price_pipeline.pkl"). All subsequent prediction requests use this loaded pipeline without retraining, making the API fast and resource-efficient.
Chapter 11
Hyperparameter Tuning
11.1 Overview
Hyperparameter tuning is the process of searching over the configuration space of a model to find settings that improve generalization performance. Unlike model parameters (which are learned from training data), hyperparameters are user-defined settings that govern how the model learns. Common Random Forest hyperparameters include the number of trees (n_estimators), the maximum depth of each tree (max_depth), and the minimum number of samples required to create a leaf node.
In this project, two hyperparameter dimensions were explored: n_estimators and max_depth. The exploration was conducted by iterating over a predefined grid of values, training a new Random Forest model for each configuration, and recording the test set R². This approach is known as manual grid search and provides intuitive insight into how each hyperparameter influences performance.
11.2 Number of Trees (n_estimators) Experiment
| Number of Trees | R² Score |
| 50 | 0.7391 |
| 100 | 0.7419 |
| 150 | 0.7406 |
| 200 | 0.7398 |
Table 11.2.2: R² Scores for Different Numbers of Trees
The results show that performance peaks at 100 trees (R² = 0.7419) and then decreases marginally as more trees are added. The performance curve is convex: too few trees leave the model underfit due to insufficient averaging, while too many trees add computational cost without improving generalization.
The marginal differences between configurations (all R² values fall within 0.003 of each other) indicate that the Random Forest has reached its performance ceiling for this dataset at a relatively low tree count. Adding more trees beyond 100 provides no measurable benefit and unnecessarily increases computation time. This finding has a practical implication: for this dataset, 100 trees is the optimal configuration and investing in larger forests is wasteful.
11.3 Maximum Depth Experiment
| Max Depth | R² Score |
| 3 | 0.6520 |
| 5 | 0.7279 |
| 7 | 0.7462 |
| 10 | 0.7487 |
| 15 | 0.7396 |
Table 11.3.2: R² Scores for Different Maximum Tree Depths
The max depth experiment reveals a clear inverted-U performance curve. At a max depth of 3, the model is strongly constrained and underfit, explaining only 65.2% of variance. As depth increases, performance improves rapidly, reaching a peak at max depth 10 (R² = 0.749). Beyond depth 10, performance declines slightly to 0.740 at depth 15, indicating the onset of overfitting as trees become excessively deep.
The optimal max depth of 10 represents a balance between model complexity (sufficient depth to capture non-linear patterns) and regularization (constrained enough to avoid memorizing training noise). This finding confirms that tree depth is a meaningful hyperparameter for this dataset, with a clearly identifiable optimal region.
11.4 Before vs. After Tuning Comparison
| Configuration | R² Score |
| Default Random Forest (n=100) | 0.7419 |
| Tuned Random Forest (n=100, depth=10) | 0.7487 |
| Improvement | +0.0068 |
Table 11.4.2: Before vs. After Tuning Performance Comparison
The best tuned configuration (n_estimators=100, max_depth=10) achieved an R² of 0.7487, representing an improvement of approximately 0.007 over the default configuration. While this improvement is technically measurable, it is extremely small in practical terms and does not change the relative ranking of models: even the best-tuned Random Forest (0.7487) remains slightly below Linear Regression (0.7625).
This result demonstrates a critical lesson: hyperparameter tuning does not guarantee meaningful improvement. When a model's default parameters are already near-optimal for the dataset's characteristics, the tuning process merely confirms that conclusion rather than unlocking significant additional performance.
Chapter 12
Final Model Selection
12.1 Selection Decision
After conducting a comprehensive evaluation of all three models across multiple dimensions, Linear Regression was selected as the final deployed model. This decision is based on the following convergent evidence:
| Criterion | Justification |
| Highest R² Score | Linear Regression achieved R² = 0.762, higher than Random Forest (0.742) and substantially higher than Decision Tree (0.144). |
| Lowest RMSE | RMSE of 898,804 represents the smallest average prediction error among all models. |
| Model Simplicity | Linear Regression has the simplest hypothesis class, making it easier to reason about, audit, and explain to non-technical stakeholders. |
| Interpretability | The model coefficients directly indicate the relationship between each feature and price, making it possible to explain predictions with complete transparency. |
| Training Speed | Linear Regression trains in milliseconds, enabling rapid retraining on updated datasets without infrastructure constraints. |
| Deployment Efficiency | The absence of an ensemble structure means the model is compact in memory and produces predictions with minimal computational overhead. |
| Dataset Alignment | The strong linear correlations identified in EDA confirm that Linear Regression's assumptions are appropriate for this dataset. |
Table 12.1: Rationale for Final Model Selection
12.2 Discussion of Model Selection Philosophy
The selection of Linear Regression over a more sophisticated ensemble method such as Random Forest is a deliberate and principled engineering decision, not a simplification. The goal of model selection is not to deploy the most complex available algorithm, but to deploy the model that best serves the prediction objective for the specific dataset at hand.
Occam's Razor, a foundational principle in scientific modeling, states that among competing hypotheses that equally well explain observed phenomena, the simpler one should be preferred. In machine learning terms, this translates to preferring lower-complexity models when they achieve equivalent or superior performance. Linear Regression satisfies this criterion in this project.
Chapter 13
Frontend Development and Deployment Interface
13.1 Overview
A core distinguishing feature of this project is the development of a fully functional, interactive web-based prediction interface. The system consists of a FastAPI backend that loads the trained model and exposes a REST prediction endpoint, and an HTML/CSS/JavaScript frontend that provides a user-friendly interface for entering property details and visualizing prediction results.
The deployment layer transforms the machine learning model from a static artifact into a usable product. This reflects real-world ML engineering practice, where model training is considered complete only when the model is accessible to end users through a well-designed interface.
13.2 Backend Architecture (FastAPI)
The backend was implemented using FastAPI, a modern Python web framework designed for high-performance API development. The application (main.py) consists of:
- Model Loading: At application startup, the trained pipeline is loaded from house_price_pipeline.pkl using joblib.load(). This ensures predictions are made without retraining.
- Data Validation: A Pydantic BaseModel class (House) defines the expected input schema with 12 integer fields. FastAPI automatically validates that incoming requests conform to this schema, returning structured error messages for invalid inputs.
- GET Endpoint (/): Serves the HTML frontend allowing the application to be accessed through a web browser.
- POST Endpoint (/predict): Accepts a JSON request body containing the 12 house features, constructs a Pandas DataFrame in the correct column order, passes it through the loaded pipeline, and returns the predicted price as a JSON response.
13.3 Frontend Interface Design
The frontend interface was developed using HTML, CSS, and JavaScript as a single-page application. The prediction is triggered by a single button click, which executes an asynchronous JavaScript fetch() request to the /predict endpoint. The predicted price is displayed prominently and formatted with locale-aware number notation (e.g., ₹8,542,339) for readability.
13.4 Extended Features
13.4.1 Property Classification System
Beyond displaying a raw price number, the frontend categorizes each predicted property into one of three tiers based on the predicted price:
- Budget Property: Predicted price below ₹4,000,000. These are entry-level or affordable segment properties.
- Mid-Range Property: Predicted price between ₹4,000,000 and ₹8,000,000. These represent the mainstream market segment.
- Premium Property: Predicted price above ₹8,000,000. These are luxury or high-value properties in the top market tier.
This classification transforms a raw numerical output into a meaningful market interpretation that requires no specialized real-estate knowledge to understand.
13.4.2 Dataset Position Indicator
A custom market position indicator was implemented to provide additional context for each predicted house price. It calculates the percentile rank of the predicted price within the housing dataset. This percentile represents the percentage of properties priced below the predicted value among the 545 houses in the dataset.
13.4.3 Model Information Panel
A dedicated section of the interface displays key model metadata: the best model selected (Linear Regression), the dataset size (545 houses), the RMSE (898,804), the R² score (0.762), and the effective prediction accuracy (76.2%).
13.4.4 Feature Importance Dashboard
The bottom section of the interface contains a static feature importance visualization implemented as custom HTML and CSS bar charts. The top 5 features (area, bathrooms, parking, stories, furnishing status) are displayed with proportional horizontal bars representing their importance percentages. This panel provides users with educational insight into the factors that most influence house prices according to the model, adding explanatory value to the prediction tool.
Chapter 14
Results and Discussion
14.1 Summary of Results
The project produced a fully functional, end-to-end machine learning pipeline with the following measured outcomes:
| Metric / Outcome | Value / Result |
| Best Model | Linear Regression |
| Best R² Score (Test) | 0.7625 (76.25%) |
| Best RMSE (Test) | ₹898,804 |
| Cross-Validation R² (Average) | 0.6228 over 5 folds |
| Dataset Size | 545 properties, 12 features + 1 target |
| Missing Values | None detected in any column |
| Feature Most Important | Area (45.71% importance) |
| Pipeline Architecture | StandardScaler + LinearRegression (Scikit-Learn Pipeline) |
| Model Persistence | Saved as house_price_pipeline.pkl via Joblib |
| Deployment Readiness | FastAPI REST API + HTML/CSS/JS Frontend |
Table 14.1: Project Outcomes Summary
14.2 Discussion of Key Findings
The most significant technical finding is the dominance of property area as a price predictor, confirmed by both EDA correlation analysis and Random Forest feature importance. Nearly half of the model's predictive power originates from a single variable.
The second most significant finding is the superior performance of Linear Regression over ensemble methods. This challenges the assumption that more sophisticated algorithms always yield better results. In domains where linear relationships are predominant, and where datasets are relatively small, simpler models frequently outperform more flexible ones.
The cross-validation analysis revealed a performance gap between holdout evaluation (0.742) and cross-validated performance (0.623). This 11.9 percentage point difference highlights the risk of overly optimistic evaluation when using a single train-test split. In any production deployment, cross-validated metrics should be the reported performance benchmark.
The hyperparameter tuning experiments demonstrated that model optimization does not always yield dramatic improvements. The maximum achievable Random Forest R² through tuning was 0.749, still below the untuned Linear Regression score of 0.762. This outcome reinforces the importance of model selection prior to optimization: tuning the wrong model provides limited returns.
Chapter 15
Key Insights
Lesson 1 End-to-End Structure is More Valuable Than Isolated Accuracy
A well-structured, reproducible pipeline is more professionally valuable than a collection of disconnected experiments. Data scientists who can build complete workflows from raw data to deployed API are significantly more effective than those who optimize only the training step.
Lesson 2 Data Quality is the Foundation
The absence of missing values in this dataset simplified preprocessing substantially. In practice, real-world datasets rarely come this clean. Investing time in understanding data quality issues before modeling prevents compounding errors downstream.
Lesson 3 Simpler Models Can Outperform Complex Ones
Linear Regression outperformed both Decision Tree and Random Forest on this dataset. The appropriate model depends on the data structure, not on algorithmic sophistication. Understanding when to use simple versus complex models is a critical ML engineering skill.
Lesson 4 EDA and Feature Importance Findings Should Align
The consistency between correlation-based EDA findings and model-derived feature importances validates the analytical process. When these two independent analyses agree, confidence in the conclusions is substantially strengthened.
Lesson 5 Cross-Validation Provides the Realistic Performance Estimate
Single holdout evaluation can produce optimistic results due to favorable train-test splits. Cross-validation provides a more reliable estimate and should be the primary reported performance metric in any professional context.
Lesson 6 Hyperparameter Tuning Has Diminishing Returns
Systematic tuning of n_estimators and max_depth produced only marginal improvements over default parameters. This reinforces that tuning is a refinement step, not a solution to structural modeling limitations.
Lesson 7 Feature Importance Provides Business Value
Knowing that area contributes 45.7% of predictive power is actionable information for real estate practitioners. Feature importance bridges the gap between ML models and domain expertise, making model outputs more interpretable and trustworthy.
Lesson 8 Deployment Readiness Completes the ML Lifecycle
Saving a model as a .pkl file and wrapping it in a REST API is not an afterthought, it is a fundamental deliverable. A model that cannot be served to users provides zero business value, regardless of its training accuracy.