File size: 1,137 Bytes
9e03a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations

from pathlib import Path

import pandas as pd


PROJECT_ROOT = Path(__file__).resolve().parents[1]
DATA_DIR = PROJECT_ROOT / "data"
RAW_TRAIN_PATH = DATA_DIR / "raw" / "train.csv"
SAMPLE_TRAIN_PATH = DATA_DIR / "sample_house_prices.csv"


DEFAULT_FEATURES = [
    "OverallQual",
    "GrLivArea",
    "GarageCars",
    "TotalBsmtSF",
    "FullBath",
    "YearBuilt",
    "Neighborhood",
    "HouseStyle",
]
TARGET_COLUMN = "SalePrice"


def load_training_data(path: str | Path | None = None) -> pd.DataFrame:
    """Load Kaggle training data, falling back to the included sample dataset."""
    if path is not None:
        return pd.read_csv(path)

    if RAW_TRAIN_PATH.exists():
        return pd.read_csv(RAW_TRAIN_PATH)

    return pd.read_csv(SAMPLE_TRAIN_PATH)


def select_model_frame(frame: pd.DataFrame) -> pd.DataFrame:
    required = [*DEFAULT_FEATURES, TARGET_COLUMN]
    missing = [column for column in required if column not in frame.columns]
    if missing:
        raise ValueError(f"Training data is missing required columns: {', '.join(missing)}")
    return frame[required].copy()