Deploy house price predictor
Browse files- .gitignore +7 -0
- .hfignore +7 -0
- .pytest_cache/.gitignore +2 -0
- .pytest_cache/CACHEDIR.TAG +4 -0
- .pytest_cache/README.md +8 -0
- .pytest_cache/v/cache/lastfailed +1 -0
- .pytest_cache/v/cache/nodeids +5 -0
- README.md +57 -5
- app.py +100 -0
- data/sample_house_prices.csv +13 -0
- requirements.txt +10 -0
- scripts/download_kaggle_data.py +33 -0
- src/__init__.py +2 -0
- src/api.py +35 -0
- src/data.py +44 -0
- src/modeling.py +123 -0
- src/schemas.py +20 -0
- src/train.py +24 -0
- tests/__init__.py +1 -0
- tests/test_api.py +32 -0
- tests/test_pipeline.py +55 -0
.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
.pytest_cache/
|
| 3 |
+
.venv/
|
| 4 |
+
venv/
|
| 5 |
+
models/*.joblib
|
| 6 |
+
data/raw/
|
| 7 |
+
*.pyc
|
.hfignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.git/
|
| 2 |
+
.pytest_cache/
|
| 3 |
+
__pycache__/
|
| 4 |
+
*/__pycache__/
|
| 5 |
+
data/raw/
|
| 6 |
+
tests/
|
| 7 |
+
*.pyc
|
.pytest_cache/.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Created by pytest automatically.
|
| 2 |
+
*
|
.pytest_cache/CACHEDIR.TAG
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Signature: 8a477f597d28d172789f06886806bc55
|
| 2 |
+
# This file is a cache directory tag created by pytest.
|
| 3 |
+
# For information about cache directory tags, see:
|
| 4 |
+
# https://bford.info/cachedir/spec.html
|
.pytest_cache/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pytest cache directory #
|
| 2 |
+
|
| 3 |
+
This directory contains data from the pytest's cache plugin,
|
| 4 |
+
which provides the `--lf` and `--ff` options, as well as the `cache` fixture.
|
| 5 |
+
|
| 6 |
+
**Do not** commit this to version control.
|
| 7 |
+
|
| 8 |
+
See [the docs](https://docs.pytest.org/en/stable/how-to/cache.html) for more information.
|
.pytest_cache/v/cache/lastfailed
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{}
|
.pytest_cache/v/cache/nodeids
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
"tests/test_api.py::test_predict_endpoint_returns_price",
|
| 3 |
+
"tests/test_pipeline.py::test_house_price_model_predicts_positive_value",
|
| 4 |
+
"tests/test_pipeline.py::test_train_model_persists_artifacts_and_reports_metrics"
|
| 5 |
+
]
|
README.md
CHANGED
|
@@ -1,12 +1,64 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: House Price Prediction
|
| 3 |
+
emoji: 🏠
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.0.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# House Price Prediction
|
| 13 |
+
|
| 14 |
+
End-to-end machine learning app for predicting house sale prices with the Kaggle
|
| 15 |
+
`house-prices-advanced-regression-techniques` dataset.
|
| 16 |
+
|
| 17 |
+
## Train
|
| 18 |
+
|
| 19 |
+
Set up Kaggle credentials, then download the dataset:
|
| 20 |
+
|
| 21 |
+
```bash
|
| 22 |
+
python scripts/download_kaggle_data.py
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
Train the model:
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
python -m src.train
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
If `data/raw/train.csv` is not present, training falls back to the included sample
|
| 32 |
+
dataset so the app remains runnable.
|
| 33 |
+
|
| 34 |
+
## FastAPI
|
| 35 |
+
|
| 36 |
+
```bash
|
| 37 |
+
uvicorn src.api:app --reload
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
Prediction endpoint:
|
| 41 |
+
|
| 42 |
+
```http
|
| 43 |
+
POST /predict
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
## Gradio
|
| 47 |
+
|
| 48 |
+
```bash
|
| 49 |
+
python app.py
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
## Hugging Face Spaces
|
| 53 |
+
|
| 54 |
+
This repository is configured for a Gradio Space. The target Space is:
|
| 55 |
+
|
| 56 |
+
```text
|
| 57 |
+
pcsekhar/MLModels
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
Deploy with:
|
| 61 |
+
|
| 62 |
+
```bash
|
| 63 |
+
hf upload --repo-type space --exclude "data/raw/*" --exclude ".git/*" --commit-message "Deploy house price predictor" pcsekhar/MLModels .
|
| 64 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
from src.data import load_training_data
|
| 8 |
+
from src.modeling import DEFAULT_MODEL_PATH, HousePriceModel, train_model
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
MODEL_PATH = Path(DEFAULT_MODEL_PATH)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def ensure_model() -> HousePriceModel:
|
| 15 |
+
if not MODEL_PATH.exists():
|
| 16 |
+
train_model(load_training_data(), artifact_path=MODEL_PATH)
|
| 17 |
+
return HousePriceModel.load(MODEL_PATH)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
MODEL = ensure_model()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def predict_price(
|
| 24 |
+
overall_qual: int,
|
| 25 |
+
gr_liv_area: int,
|
| 26 |
+
garage_cars: int,
|
| 27 |
+
total_bsmt_sf: int,
|
| 28 |
+
full_bath: int,
|
| 29 |
+
year_built: int,
|
| 30 |
+
neighborhood: str,
|
| 31 |
+
house_style: str,
|
| 32 |
+
) -> str:
|
| 33 |
+
prediction = MODEL.predict(
|
| 34 |
+
{
|
| 35 |
+
"OverallQual": overall_qual,
|
| 36 |
+
"GrLivArea": gr_liv_area,
|
| 37 |
+
"GarageCars": garage_cars,
|
| 38 |
+
"TotalBsmtSF": total_bsmt_sf,
|
| 39 |
+
"FullBath": full_bath,
|
| 40 |
+
"YearBuilt": year_built,
|
| 41 |
+
"Neighborhood": neighborhood,
|
| 42 |
+
"HouseStyle": house_style,
|
| 43 |
+
}
|
| 44 |
+
)
|
| 45 |
+
return f"${prediction:,.0f}"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
with gr.Blocks(title="House Price Predictor") as demo:
|
| 49 |
+
gr.Markdown("# House Price Predictor")
|
| 50 |
+
gr.Markdown("Predict sale prices using a model trained on the Kaggle House Prices dataset.")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
overall_qual = gr.Slider(1, 10, value=7, step=1, label="Overall quality")
|
| 55 |
+
gr_liv_area = gr.Number(value=1800, label="Above-ground living area")
|
| 56 |
+
garage_cars = gr.Slider(0, 5, value=2, step=1, label="Garage capacity")
|
| 57 |
+
total_bsmt_sf = gr.Number(value=1000, label="Total basement square feet")
|
| 58 |
+
with gr.Column():
|
| 59 |
+
full_bath = gr.Slider(0, 5, value=2, step=1, label="Full bathrooms")
|
| 60 |
+
year_built = gr.Number(value=1995, label="Year built")
|
| 61 |
+
neighborhood = gr.Dropdown(
|
| 62 |
+
["NAmes", "CollgCr", "OldTown", "Edwards", "Somerst", "NridgHt", "Gilbert", "NoRidge"],
|
| 63 |
+
value="Somerst",
|
| 64 |
+
label="Neighborhood",
|
| 65 |
+
allow_custom_value=True,
|
| 66 |
+
)
|
| 67 |
+
house_style = gr.Dropdown(
|
| 68 |
+
["1Story", "2Story", "1.5Fin", "SLvl", "SFoyer"],
|
| 69 |
+
value="2Story",
|
| 70 |
+
label="House style",
|
| 71 |
+
allow_custom_value=True,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
output = gr.Textbox(label="Predicted sale price", interactive=False)
|
| 75 |
+
predict_button = gr.Button("Predict", variant="primary")
|
| 76 |
+
predict_button.click(
|
| 77 |
+
fn=predict_price,
|
| 78 |
+
inputs=[
|
| 79 |
+
overall_qual,
|
| 80 |
+
gr_liv_area,
|
| 81 |
+
garage_cars,
|
| 82 |
+
total_bsmt_sf,
|
| 83 |
+
full_bath,
|
| 84 |
+
year_built,
|
| 85 |
+
neighborhood,
|
| 86 |
+
house_style,
|
| 87 |
+
],
|
| 88 |
+
outputs=output,
|
| 89 |
+
api_name="predict",
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
gr.Markdown(
|
| 93 |
+
f"Model metrics: RMSE `{MODEL.metrics.get('rmse')}`, "
|
| 94 |
+
f"MAE `{MODEL.metrics.get('mae')}`, R2 `{MODEL.metrics.get('r2')}`"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo.launch()
|
| 100 |
+
|
data/sample_house_prices.csv
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
OverallQual,GrLivArea,GarageCars,TotalBsmtSF,FullBath,YearBuilt,Neighborhood,HouseStyle,SalePrice
|
| 2 |
+
5,1200,1,800,1,1960,NAmes,1Story,135000
|
| 3 |
+
6,1500,2,900,2,1975,CollgCr,1Story,165000
|
| 4 |
+
7,1800,2,1100,2,1990,Somerst,2Story,210000
|
| 5 |
+
8,2100,3,1300,2,2001,NridgHt,2Story,260000
|
| 6 |
+
9,2400,3,1500,3,2010,NoRidge,2Story,320000
|
| 7 |
+
4,900,1,600,1,1950,OldTown,1Story,110000
|
| 8 |
+
6,1600,2,950,2,1982,NAmes,1Story,175000
|
| 9 |
+
7,2000,2,1250,2,1998,Gilbert,2Story,235000
|
| 10 |
+
8,2200,3,1350,2,2005,Somerst,2Story,275000
|
| 11 |
+
5,1300,1,850,1,1965,Edwards,1Story,140000
|
| 12 |
+
7,1750,2,1050,2,1993,CollgCr,2Story,205000
|
| 13 |
+
9,2600,3,1600,3,2012,NridgHt,2Story,345000
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
scikit-learn
|
| 3 |
+
joblib
|
| 4 |
+
fastapi
|
| 5 |
+
uvicorn
|
| 6 |
+
gradio
|
| 7 |
+
pydantic
|
| 8 |
+
kaggle
|
| 9 |
+
pytest
|
| 10 |
+
httpx
|
scripts/download_kaggle_data.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import subprocess
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
COMPETITION = "house-prices-advanced-regression-techniques"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def main() -> None:
|
| 12 |
+
parser = argparse.ArgumentParser(description="Download Kaggle House Prices data.")
|
| 13 |
+
parser.add_argument("--output", type=Path, default=Path("data/raw"))
|
| 14 |
+
args = parser.parse_args()
|
| 15 |
+
args.output.mkdir(parents=True, exist_ok=True)
|
| 16 |
+
subprocess.run(
|
| 17 |
+
[
|
| 18 |
+
"kaggle",
|
| 19 |
+
"competitions",
|
| 20 |
+
"download",
|
| 21 |
+
"-c",
|
| 22 |
+
COMPETITION,
|
| 23 |
+
"-p",
|
| 24 |
+
str(args.output),
|
| 25 |
+
"--unzip",
|
| 26 |
+
],
|
| 27 |
+
check=True,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
main()
|
| 33 |
+
|
src/__init__.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""House price prediction package."""
|
| 2 |
+
|
src/api.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
|
| 7 |
+
from src.data import load_training_data
|
| 8 |
+
from src.modeling import DEFAULT_MODEL_PATH, HousePriceModel
|
| 9 |
+
from src.modeling import train_model
|
| 10 |
+
from src.schemas import HouseFeatures, PredictionResponse
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def create_app(model_path: str | Path = DEFAULT_MODEL_PATH) -> FastAPI:
|
| 14 |
+
app = FastAPI(title="House Price Prediction API", version="1.0.0")
|
| 15 |
+
model_path = Path(model_path)
|
| 16 |
+
if not model_path.exists():
|
| 17 |
+
train_model(load_training_data(), artifact_path=model_path)
|
| 18 |
+
model = HousePriceModel.load(model_path)
|
| 19 |
+
|
| 20 |
+
@app.get("/health")
|
| 21 |
+
def health() -> dict[str, str]:
|
| 22 |
+
return {"status": "ok"}
|
| 23 |
+
|
| 24 |
+
@app.get("/metrics")
|
| 25 |
+
def metrics() -> dict[str, float]:
|
| 26 |
+
return model.metrics
|
| 27 |
+
|
| 28 |
+
@app.post("/predict", response_model=PredictionResponse)
|
| 29 |
+
def predict(features: HouseFeatures) -> PredictionResponse:
|
| 30 |
+
return PredictionResponse(predicted_price=model.predict(features.model_dump()))
|
| 31 |
+
|
| 32 |
+
return app
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
app = create_app()
|
src/data.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
| 9 |
+
DATA_DIR = PROJECT_ROOT / "data"
|
| 10 |
+
RAW_TRAIN_PATH = DATA_DIR / "raw" / "train.csv"
|
| 11 |
+
SAMPLE_TRAIN_PATH = DATA_DIR / "sample_house_prices.csv"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
DEFAULT_FEATURES = [
|
| 15 |
+
"OverallQual",
|
| 16 |
+
"GrLivArea",
|
| 17 |
+
"GarageCars",
|
| 18 |
+
"TotalBsmtSF",
|
| 19 |
+
"FullBath",
|
| 20 |
+
"YearBuilt",
|
| 21 |
+
"Neighborhood",
|
| 22 |
+
"HouseStyle",
|
| 23 |
+
]
|
| 24 |
+
TARGET_COLUMN = "SalePrice"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def load_training_data(path: str | Path | None = None) -> pd.DataFrame:
|
| 28 |
+
"""Load Kaggle training data, falling back to the included sample dataset."""
|
| 29 |
+
if path is not None:
|
| 30 |
+
return pd.read_csv(path)
|
| 31 |
+
|
| 32 |
+
if RAW_TRAIN_PATH.exists():
|
| 33 |
+
return pd.read_csv(RAW_TRAIN_PATH)
|
| 34 |
+
|
| 35 |
+
return pd.read_csv(SAMPLE_TRAIN_PATH)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def select_model_frame(frame: pd.DataFrame) -> pd.DataFrame:
|
| 39 |
+
required = [*DEFAULT_FEATURES, TARGET_COLUMN]
|
| 40 |
+
missing = [column for column in required if column not in frame.columns]
|
| 41 |
+
if missing:
|
| 42 |
+
raise ValueError(f"Training data is missing required columns: {', '.join(missing)}")
|
| 43 |
+
return frame[required].copy()
|
| 44 |
+
|
src/modeling.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from math import sqrt
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
import joblib
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from sklearn.compose import ColumnTransformer
|
| 11 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 12 |
+
from sklearn.impute import SimpleImputer
|
| 13 |
+
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
|
| 14 |
+
from sklearn.model_selection import train_test_split
|
| 15 |
+
from sklearn.pipeline import Pipeline
|
| 16 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 17 |
+
|
| 18 |
+
from src.data import DEFAULT_FEATURES, TARGET_COLUMN, select_model_frame
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
NUMERIC_FEATURES = [
|
| 22 |
+
"OverallQual",
|
| 23 |
+
"GrLivArea",
|
| 24 |
+
"GarageCars",
|
| 25 |
+
"TotalBsmtSF",
|
| 26 |
+
"FullBath",
|
| 27 |
+
"YearBuilt",
|
| 28 |
+
]
|
| 29 |
+
CATEGORICAL_FEATURES = ["Neighborhood", "HouseStyle"]
|
| 30 |
+
DEFAULT_MODEL_PATH = Path("models") / "house_price_model.joblib"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@dataclass(frozen=True)
|
| 34 |
+
class TrainedModel:
|
| 35 |
+
pipeline: Pipeline
|
| 36 |
+
feature_names: list[str]
|
| 37 |
+
metrics: dict[str, float]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class HousePriceModel:
|
| 41 |
+
def __init__(self, trained: TrainedModel):
|
| 42 |
+
self._trained = trained
|
| 43 |
+
|
| 44 |
+
@property
|
| 45 |
+
def metrics(self) -> dict[str, float]:
|
| 46 |
+
return self._trained.metrics
|
| 47 |
+
|
| 48 |
+
@property
|
| 49 |
+
def feature_names(self) -> list[str]:
|
| 50 |
+
return self._trained.feature_names
|
| 51 |
+
|
| 52 |
+
@classmethod
|
| 53 |
+
def load(cls, path: str | Path = DEFAULT_MODEL_PATH) -> "HousePriceModel":
|
| 54 |
+
payload = joblib.load(path)
|
| 55 |
+
return cls(
|
| 56 |
+
TrainedModel(
|
| 57 |
+
pipeline=payload["pipeline"],
|
| 58 |
+
feature_names=list(payload["feature_names"]),
|
| 59 |
+
metrics=dict(payload["metrics"]),
|
| 60 |
+
)
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
def predict(self, features: dict[str, Any]) -> float:
|
| 64 |
+
frame = pd.DataFrame([{name: features.get(name) for name in self.feature_names}])
|
| 65 |
+
prediction = self._trained.pipeline.predict(frame)[0]
|
| 66 |
+
return round(float(max(prediction, 0.0)), 2)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def build_pipeline() -> Pipeline:
|
| 70 |
+
numeric_pipeline = Pipeline(
|
| 71 |
+
steps=[
|
| 72 |
+
("imputer", SimpleImputer(strategy="median")),
|
| 73 |
+
]
|
| 74 |
+
)
|
| 75 |
+
categorical_pipeline = Pipeline(
|
| 76 |
+
steps=[
|
| 77 |
+
("imputer", SimpleImputer(strategy="most_frequent")),
|
| 78 |
+
("encoder", OneHotEncoder(handle_unknown="ignore", sparse_output=False)),
|
| 79 |
+
]
|
| 80 |
+
)
|
| 81 |
+
preprocessor = ColumnTransformer(
|
| 82 |
+
transformers=[
|
| 83 |
+
("num", numeric_pipeline, NUMERIC_FEATURES),
|
| 84 |
+
("cat", categorical_pipeline, CATEGORICAL_FEATURES),
|
| 85 |
+
]
|
| 86 |
+
)
|
| 87 |
+
regressor = RandomForestRegressor(n_estimators=250, random_state=42, min_samples_leaf=2)
|
| 88 |
+
return Pipeline(steps=[("preprocessor", preprocessor), ("regressor", regressor)])
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def train_model(frame: pd.DataFrame, artifact_path: str | Path = DEFAULT_MODEL_PATH) -> TrainedModel:
|
| 92 |
+
model_frame = select_model_frame(frame).dropna(subset=[TARGET_COLUMN])
|
| 93 |
+
features = model_frame[DEFAULT_FEATURES]
|
| 94 |
+
target = model_frame[TARGET_COLUMN]
|
| 95 |
+
|
| 96 |
+
if len(model_frame) >= 10:
|
| 97 |
+
x_train, x_test, y_train, y_test = train_test_split(
|
| 98 |
+
features, target, test_size=0.2, random_state=42
|
| 99 |
+
)
|
| 100 |
+
else:
|
| 101 |
+
x_train, x_test, y_train, y_test = features, features, target, target
|
| 102 |
+
|
| 103 |
+
pipeline = build_pipeline()
|
| 104 |
+
pipeline.fit(x_train, y_train)
|
| 105 |
+
predictions = pipeline.predict(x_test)
|
| 106 |
+
metrics = {
|
| 107 |
+
"rmse": round(sqrt(mean_squared_error(y_test, predictions)), 2),
|
| 108 |
+
"mae": round(mean_absolute_error(y_test, predictions), 2),
|
| 109 |
+
"r2": round(r2_score(y_test, predictions), 4),
|
| 110 |
+
}
|
| 111 |
+
trained = TrainedModel(pipeline=pipeline, feature_names=list(DEFAULT_FEATURES), metrics=metrics)
|
| 112 |
+
|
| 113 |
+
artifact_path = Path(artifact_path)
|
| 114 |
+
artifact_path.parent.mkdir(parents=True, exist_ok=True)
|
| 115 |
+
joblib.dump(
|
| 116 |
+
{
|
| 117 |
+
"pipeline": trained.pipeline,
|
| 118 |
+
"feature_names": trained.feature_names,
|
| 119 |
+
"metrics": trained.metrics,
|
| 120 |
+
},
|
| 121 |
+
artifact_path,
|
| 122 |
+
)
|
| 123 |
+
return trained
|
src/schemas.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from pydantic import BaseModel, Field
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class HouseFeatures(BaseModel):
|
| 7 |
+
OverallQual: int = Field(7, ge=1, le=10)
|
| 8 |
+
GrLivArea: int = Field(1800, ge=200)
|
| 9 |
+
GarageCars: int = Field(2, ge=0, le=5)
|
| 10 |
+
TotalBsmtSF: int = Field(1000, ge=0)
|
| 11 |
+
FullBath: int = Field(2, ge=0, le=5)
|
| 12 |
+
YearBuilt: int = Field(1995, ge=1800, le=2026)
|
| 13 |
+
Neighborhood: str = "Somerst"
|
| 14 |
+
HouseStyle: str = "2Story"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class PredictionResponse(BaseModel):
|
| 18 |
+
predicted_price: float
|
| 19 |
+
currency: str = "USD"
|
| 20 |
+
|
src/train.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
from src.data import load_training_data
|
| 7 |
+
from src.modeling import DEFAULT_MODEL_PATH, train_model
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def main() -> None:
|
| 11 |
+
parser = argparse.ArgumentParser(description="Train the house price prediction model.")
|
| 12 |
+
parser.add_argument("--data", type=Path, default=None, help="Path to Kaggle train.csv")
|
| 13 |
+
parser.add_argument("--output", type=Path, default=DEFAULT_MODEL_PATH, help="Model artifact path")
|
| 14 |
+
args = parser.parse_args()
|
| 15 |
+
|
| 16 |
+
frame = load_training_data(args.data)
|
| 17 |
+
trained = train_model(frame, artifact_path=args.output)
|
| 18 |
+
print(f"Saved model to {args.output}")
|
| 19 |
+
print(f"Metrics: {trained.metrics}")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
if __name__ == "__main__":
|
| 23 |
+
main()
|
| 24 |
+
|
tests/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|
tests/test_api.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
from fastapi.testclient import TestClient
|
| 4 |
+
|
| 5 |
+
from src.api import create_app
|
| 6 |
+
from src.modeling import train_model
|
| 7 |
+
from tests.test_pipeline import sample_training_frame
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_predict_endpoint_returns_price(tmp_path: Path) -> None:
|
| 11 |
+
artifact_path = tmp_path / "house_price_model.joblib"
|
| 12 |
+
train_model(sample_training_frame(), artifact_path=artifact_path)
|
| 13 |
+
client = TestClient(create_app(model_path=artifact_path))
|
| 14 |
+
|
| 15 |
+
response = client.post(
|
| 16 |
+
"/predict",
|
| 17 |
+
json={
|
| 18 |
+
"OverallQual": 7,
|
| 19 |
+
"GrLivArea": 1850,
|
| 20 |
+
"GarageCars": 2,
|
| 21 |
+
"TotalBsmtSF": 1050,
|
| 22 |
+
"FullBath": 2,
|
| 23 |
+
"YearBuilt": 1995,
|
| 24 |
+
"Neighborhood": "Somerst",
|
| 25 |
+
"HouseStyle": "2Story",
|
| 26 |
+
},
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
assert response.status_code == 200
|
| 30 |
+
body = response.json()
|
| 31 |
+
assert body["predicted_price"] > 0
|
| 32 |
+
assert body["currency"] == "USD"
|
tests/test_pipeline.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
from src.modeling import HousePriceModel, train_model
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def sample_training_frame() -> pd.DataFrame:
|
| 9 |
+
return pd.DataFrame(
|
| 10 |
+
{
|
| 11 |
+
"OverallQual": [5, 6, 7, 8, 9, 4, 6, 7],
|
| 12 |
+
"GrLivArea": [1200, 1500, 1800, 2100, 2400, 900, 1600, 2000],
|
| 13 |
+
"GarageCars": [1, 2, 2, 3, 3, 1, 2, 2],
|
| 14 |
+
"TotalBsmtSF": [800, 900, 1100, 1300, 1500, 600, 950, 1250],
|
| 15 |
+
"FullBath": [1, 2, 2, 2, 3, 1, 2, 2],
|
| 16 |
+
"YearBuilt": [1960, 1975, 1990, 2001, 2010, 1950, 1982, 1998],
|
| 17 |
+
"Neighborhood": ["NAmes", "CollgCr", "Somerst", "NridgHt", "NoRidge", "OldTown", "NAmes", "Gilbert"],
|
| 18 |
+
"HouseStyle": ["1Story", "1Story", "2Story", "2Story", "2Story", "1Story", "1Story", "2Story"],
|
| 19 |
+
"SalePrice": [135000, 165000, 210000, 260000, 320000, 110000, 175000, 235000],
|
| 20 |
+
}
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_train_model_persists_artifacts_and_reports_metrics(tmp_path: Path) -> None:
|
| 25 |
+
artifact_path = tmp_path / "house_price_model.joblib"
|
| 26 |
+
|
| 27 |
+
trained = train_model(sample_training_frame(), artifact_path=artifact_path)
|
| 28 |
+
|
| 29 |
+
assert artifact_path.exists()
|
| 30 |
+
assert trained.metrics["rmse"] >= 0
|
| 31 |
+
assert trained.metrics["r2"] <= 1
|
| 32 |
+
assert "OverallQual" in trained.feature_names
|
| 33 |
+
assert "SalePrice" not in trained.feature_names
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_house_price_model_predicts_positive_value(tmp_path: Path) -> None:
|
| 37 |
+
artifact_path = tmp_path / "house_price_model.joblib"
|
| 38 |
+
trained = train_model(sample_training_frame(), artifact_path=artifact_path)
|
| 39 |
+
model = HousePriceModel.load(artifact_path)
|
| 40 |
+
|
| 41 |
+
prediction = model.predict(
|
| 42 |
+
{
|
| 43 |
+
"OverallQual": 7,
|
| 44 |
+
"GrLivArea": 1850,
|
| 45 |
+
"GarageCars": 2,
|
| 46 |
+
"TotalBsmtSF": 1050,
|
| 47 |
+
"FullBath": 2,
|
| 48 |
+
"YearBuilt": 1995,
|
| 49 |
+
"Neighborhood": "Somerst",
|
| 50 |
+
"HouseStyle": "2Story",
|
| 51 |
+
}
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
assert prediction > 0
|
| 55 |
+
assert model.metrics == trained.metrics
|