This repository contains the Capstone 1 Project for the DataTalks.Club Machine Learning Zoomcamp.
The project is an end-to-end machine learning service that predicts the optimal nightly price for an Airbnb listing in New York City based on its characteristics (location, amenities, room type, etc.). The final product is a containerized REST API built with FastAPI that serves predictions from a tuned XGBoost model.
In the competitive short-term rental market of NYC, hosts often struggle to price their listings effectively.
- Overpricing leads to high vacancy rates and lost revenue.
- Underpricing leaves money on the table and attracts lower-quality guests.
The Solution: This project implements a "Dynamic Pricing Engine" that analyzes market data to suggest a competitive price. This helps hosts maximize occupancy and revenue (Yield Management).
- Language: Python 3.10
- Dependency Management: Pipenv
- Machine Learning: Scikit-Learn (Pipeline), XGBoost (Gradient Boosting)
- API Framework: FastAPI with Uvicorn
- Data Validation: Pydantic
- Containerization: Docker & Docker Compose
- Cloud Deployment: Google Cloud Run (Serverless)
This project follows a structured MLOps workflow to ensure reproducibility and performance.
-
Research & EDA (
notebooks/01_eda_and_engineering.ipynb):- Analyzed the "New York City Airbnb Open Data" from Kaggle.
- Feature Engineering:
- Target Transformation: Applied
log1pto the price variable to handle the long-tail distribution. - Geospatial: Calculated Haversine Distance to city center (Times Square) and used K-Means clustering to identify "micro-neighborhoods."
- NLP Lite: Extracted keywords from listing titles (e.g., "Luxury", "View").
- Target Transformation: Applied
- Cleaning: Handled outliers (prices > $2000) and missing values.
-
Model Training & Tuning (
notebooks/02_modeling.ipynb):- Established a Linear Regression Baseline (RMSE: ~0.45).
- Trained a Challenger XGBoost Model.
- Hyperparameter Tuning: Used
Hyperopt(Bayesian Optimization) to tunemax_depth,learning_rate,subsample, etc. - The final XGBoost model achieved an RMSE of 0.40 (on log price), significantly outperforming the baseline.
- The logic was exported to
src/train.pyfor automated training.
-
Deployment (
src/main.py&Dockerfile):- Built a FastAPI service with Pydantic schemas for strict input validation.
- Implemented Lifespan Events to load the model efficiently on startup.
- Containerized the application using a multi-stage Dockerfile with a non-root user for security.
- Deployed to Google Cloud Run for serverless scalability.
The service is currently live and accessible via the following URL.
API Base URL:
https://airbnb-pricing-service-644458477502.us-central1.run.app
You can test the API directly in your browser using the auto-generated Swagger UI: 👉 Open Live Swagger UI
Copy and paste this into your terminal to get a real-time price prediction:
curl -X 'POST' \
'https://airbnb-pricing-service-644458477502.us-central1.run.app/predict' \
-H 'Content-Type: application/json' \
-d '{
"neighbourhood_group": "Manhattan",
"neighbourhood": "Midtown",
"room_type": "Entire home/apt",
"latitude": 40.75,
"longitude": -73.98,
"minimum_nights": 2,
"availability_365": 100,
"name": "Luxury Suite near Times Square"
}'Expected JSON Response:
{
"suggested_price": 244.13
}The entire application is containerized. You can spin it up with a single command.
Prerequisites:
git clone <your-repo-url>
cd nyc-airbnb-pricingdocker compose up --buildThe API will be available at http://localhost:9696.
Option A: Interactive Docs Open http://localhost:9696/docs in your browser.
Option B: Python Script
You can use the provided test script (if created) or curl:
curl -X 'POST' \
'http://localhost:9696/predict' \
-H 'Content-Type: application/json' \
-d '{
"neighbourhood_group": "Brooklyn",
"neighbourhood": "Williamsburg",
"room_type": "Private room",
"latitude": 40.71,
"longitude": -73.96,
"minimum_nights": 3
}'If you prefer to run without Docker (e.g., for development), follow these steps.
-
Install Dependencies (Pipenv):
pip install pipenv pipenv install --dev pipenv shell
-
Download Data: Ensure
AB_NYC_2019.csvis in thedata/folder. -
Train the Model: This script will train the model and save
models/model_pipeline.pkl.python -m src.train
-
Run the Server:
uvicorn src.main:app --host 0.0.0.0 --port 9696 --reload
This section maps the project files to the course evaluation criteria.
- Problem Description:
README.md(Section 1). - EDA:
notebooks/01_eda_and_engineering.ipynb(Analysis of target, features, and geospatial data). - Model Training:
notebooks/02_modeling.ipynb(XGBoost vs Linear, Hyperopt tuning). - Export to Script:
src/train.py(Reproducible training logic). - Reproducibility:
Pipfile&Pipfile.lockincluded. - Model Deployment:
src/main.py(FastAPI service). - Containerization:
Dockerfile&docker-compose.yml. - Cloud Deployment: Service deployed to Google Cloud Run (URL in Section 4).