# Lesson 7: pipelines and cross-validation (and avoiding leakage)
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import Ridge
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score, KFold

flats = pd.read_csv("flats.csv")          # raw, with gaps: the pipeline does the cleaning
X, y = flats.drop(columns="price_lakh"), flats["price_lakh"]
num = ["area_sqft", "bedrooms", "age_years", "floor"]

prep = ColumnTransformer([
    ("num", Pipeline([("fill", SimpleImputer(strategy="median")), ("scale", StandardScaler())]), num),
    ("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),
])
cv = KFold(n_splits=5, shuffle=True, random_state=0)
for name, est in [("Ridge regression", Ridge(alpha=1.0)), ("Random forest", RandomForestRegressor(n_estimators=200, random_state=0))]:
    pipe = Pipeline([("prep", prep), ("model", est)])
    mae = -cross_val_score(pipe, X, y, cv=cv, scoring="neg_mean_absolute_error")
    print(f"{name:17} MAE per fold: {[float(round(m, 1)) for m in mae]}  average {mae.mean():.1f} lakh")
