A production-ready machine learning API for predicting housing prices using XGBoost, deployed on Kubernetes with FastAPI.
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.
- 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
| Metric | Score |
|---|---|
| RΒ² Score (Test) | 0.62 |
| Mean Squared Error | 1.20e12 |
| Features Used | 16 (12 input + 4 engineered) |
This project uses the Housing Prices Dataset from Kaggle, released under CC0 (public domain).
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.
βββββββββββββββββββββββββββββββββββββββββββ
β Client Request (JSON) β
ββββββββββββββββββ¬βββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Endpoint (/predict) β
β - Pydantic Validation β
β - Feature Engineering β
ββββββββββββββββββ¬βββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β XGBoost Model + MinMaxScaler β
β - Real-time Inference β
ββββββββββββββββββ¬βββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β JSON Response (Predicted Price) β
βββββββββββββββββββββββββββββββββββββββββββ
- Python 3.8+
- Docker
- Kubernetes (optional, for deployment)
# Clone repository
git clone https://github.com/breaseabrol/house-price-prediction.git
cd house-price-prediction
# Install dependencies
pip install -r requirements.txtmodel.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# Start FastAPI server
uvicorn main:app --reload --port 8080
# API will be available at http://localhost:8080
# Interactive docs: http://localhost:8080/docscurl -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
}# Build image
docker build -t house-price-api:latest .
# Run container
docker run -p 8080:8080 house-price-api:latest# 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:80house-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
| 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 |
- MinMaxScaler β Normalizes area to [0, 1] range
- One-Hot Encoding β Converts binary features (mainroad, guestroom, etc.)
- Outlier Removal β IQR-based filtering on price
XGBRegressor(
n_estimators=800,
learning_rate=0.05,
max_depth=6,
subsample=0.8,
colsample_bytree=0.8,
random_state=42
)See Project.ipynb for the complete training workflow:
- Data Loading & Exploration
- Feature Engineering
- Train-Test Split (70-30)
- Model Training & Validation
- Model & Scaler Serialization (outputs
model.pklandscaler.pkl)
- Machine Learning: XGBoost, Scikit-Learn, Pandas, NumPy
- API Framework: FastAPI, Pydantic, Uvicorn
- Containerization: Docker
- Orchestration: Kubernetes
- Serialization: Joblib
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"
}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
Contributions are welcome! Feel free to:
- Report bugs via Issues
- Submit pull requests for improvements
- Suggest new features
This project is open source and available under the MIT License.
Branden Rease Abrol
- GitHub: @breaseabrol
- LinkedIn: breaseabrol
- Email: brandenabrol6805@gmail.com
β If this project helped you, please consider giving it a star!