Skip to content

breaseabrol/HousePrice-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

House Price Prediction API

License: MIT

A production-ready machine learning API for predicting housing prices using XGBoost, deployed on Kubernetes with FastAPI.

🎯 Overview

This project demonstrates end-to-end ML engineering: from data preprocessing and feature engineering to model training, API development, and cloud deployment. The system achieves 0.62 RΒ² score on test data with automated real-time predictions.

✨ Features

  • XGBoost ML Model – Trained on housing dataset with advanced feature engineering
  • FastAPI REST Endpoint – Real-time predictions with Pydantic validation
  • Kubernetes Deployment – Containerized microservices with load balancing and fault tolerance
  • Automated Feature Engineering – Interaction terms, amenity aggregation, luxury index calculation
  • Model Persistence – Joblib serialization for reproducible inference across deployments

πŸ“Š Model Performance

Metric Score
RΒ² Score (Test) 0.62
Mean Squared Error 1.20e12
Features Used 16 (12 input + 4 engineered)

πŸ“Š Dataset

This project uses the Housing Prices Dataset from Kaggle, released under CC0 (public domain).

πŸ—ƒοΈ Model Artifacts

model.pkl and scaler.pkl are intentionally excluded from version control (see .gitignore) β€” binary model artifacts don't belong in git history, as they bloat repo size and cause unnecessary merge conflicts on every retrain.

To regenerate them locally, run the training workflow in Project.ipynb. The notebook will output model.pkl and scaler.pkl into the project root, which main.py loads on startup.

Note: You must generate these files before running the API locally β€” see Quick Start below.

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client Request (JSON)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FastAPI Endpoint (/predict)           β”‚
β”‚   - Pydantic Validation                 β”‚
β”‚   - Feature Engineering                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   XGBoost Model + MinMaxScaler          β”‚
β”‚   - Real-time Inference                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   JSON Response (Predicted Price)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • Docker
  • Kubernetes (optional, for deployment)

Installation

# Clone repository
git clone https://github.com/breaseabrol/house-price-prediction.git
cd house-price-prediction

# Install dependencies
pip install -r requirements.txt

Generate Model Artifacts

model.pkl and scaler.pkl are not tracked in this repo (see Model Artifacts) and must be generated before the API will run:

# Run all cells in Project.ipynb to train the model and save
# model.pkl and scaler.pkl into the project root
jupyter notebook Project.ipynb

Run Locally

# Start FastAPI server
uvicorn main:app --reload --port 8080

# API will be available at http://localhost:8080
# Interactive docs: http://localhost:8080/docs

Example Request

curl -X POST "http://localhost:8080/predict" \
  -H "Content-Type: application/json" \
  -d '{
    "area": 7420,
    "bedrooms": 4,
    "bathrooms": 2,
    "stories": 3,
    "parking": 2,
    "furnishingstatus": 2,
    "mainroad_yes": 1,
    "guestroom_yes": 0,
    "basement_yes": 0,
    "hotwaterheating_yes": 0,
    "airconditioning_yes": 1,
    "prefarea_yes": 1
  }'

Response:

{
  "prediction": 13250000.5
}

πŸ“¦ Deployment

Docker

# Build image
docker build -t house-price-api:latest .

# Run container
docker run -p 8080:8080 house-price-api:latest

Kubernetes

# Deploy
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

# Check deployment
kubectl get pods
kubectl get svc

# Access via LoadBalancer
kubectl port-forward svc/data-api-service 8080:80

πŸ“‚ Project Structure

house-price-prediction/
β”œβ”€β”€ main.py                 # FastAPI application
β”œβ”€β”€ model.pkl                # Trained XGBoost model (generated, gitignored)
β”œβ”€β”€ scaler.pkl                # MinMaxScaler for normalization (generated, gitignored)
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ Dockerfile               # Container configuration
β”œβ”€β”€ deployment.yaml          # Kubernetes deployment
β”œβ”€β”€ service.yaml              # Kubernetes service
β”œβ”€β”€ Project.ipynb             # Jupyter notebook (model training)
β”œβ”€β”€ .gitignore                 # Excludes model artifacts, caches, env files
└── README.md                # This file

πŸ”§ Model Details

Feature Engineering

Feature Description
area_bedrooms Interaction term: area Γ— bedrooms
stories_bathrooms Interaction term: stories Γ— bathrooms
amenities_count Sum of 6 binary amenity features
luxury_index Sum of AC, heating, guest room, basement

Data Preprocessing

  • MinMaxScaler – Normalizes area to [0, 1] range
  • One-Hot Encoding – Converts binary features (mainroad, guestroom, etc.)
  • Outlier Removal – IQR-based filtering on price

Model Hyperparameters

XGBRegressor(
    n_estimators=800,
    learning_rate=0.05,
    max_depth=6,
    subsample=0.8,
    colsample_bytree=0.8,
    random_state=42
)

πŸ“ˆ Training Pipeline

See Project.ipynb for the complete training workflow:

  1. Data Loading & Exploration
  2. Feature Engineering
  3. Train-Test Split (70-30)
  4. Model Training & Validation
  5. Model & Scaler Serialization (outputs model.pkl and scaler.pkl)

πŸ› οΈ Technologies Used

  • Machine Learning: XGBoost, Scikit-Learn, Pandas, NumPy
  • API Framework: FastAPI, Pydantic, Uvicorn
  • Containerization: Docker
  • Orchestration: Kubernetes
  • Serialization: Joblib

πŸ“ API Documentation

Endpoint: /predict

Method: POST

Request Body:

{
  "area": float,
  "bedrooms": int,
  "bathrooms": int,
  "stories": int,
  "parking": int,
  "furnishingstatus": int,
  "mainroad_yes": int (0 or 1),
  "guestroom_yes": int (0 or 1),
  "basement_yes": int (0 or 1),
  "hotwaterheating_yes": int (0 or 1),
  "airconditioning_yes": int (0 or 1),
  "prefarea_yes": int (0 or 1)
}

Response:

{
  "prediction": float
}

Error Response:

{
  "error": "Error message"
}

πŸŽ“ Learning Outcomes

This project demonstrates:

  • βœ… End-to-end ML pipeline development
  • βœ… Feature engineering and data preprocessing
  • βœ… Model training and evaluation
  • βœ… REST API design with FastAPI
  • βœ… Containerization with Docker
  • βœ… Kubernetes microservices deployment
  • βœ… Production-grade model persistence, with binary artifacts kept out of version control

🀝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs via Issues
  • Submit pull requests for improvements
  • Suggest new features

πŸ“„ License

This project is open source and available under the MIT License.

πŸ‘€ Author

Branden Rease Abrol


⭐ If this project helped you, please consider giving it a star!

About

A repository full of machine learning projects. Open to collaboration and insightful feedback.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors