Repository navigation
Expand file tree
/
Copy pathmodel_pipeline.py
More file actions
99 lines (76 loc) · 2.59 KB
/
Copy pathmodel_pipeline.py
File metadata and controls
99 lines (76 loc) · 2.59 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from pathlib import Path
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, r2_score
from sklearn.model_selection import train_test_split
DATA_PATH = Path("synthetic_housing_data.csv")
FEATURES = ["sqft", "bedrooms", "age_years", "metro_distance_km"]
TARGET = "price_lakhs"
def load_and_clean_data(path: Path) -> pd.DataFrame:
data = pd.read_csv(path)
starting_rows = len(data)
data = data.dropna()
data = data[(data["sqft"] > 0) & (data["price_lakhs"] > 0)]
dropped_rows = starting_rows - len(data)
print(f"Loaded {starting_rows} rows from {path}")
print(f"Dropped {dropped_rows} rows during cleaning")
return data
def train_and_evaluate(data: pd.DataFrame, verbose: bool = True) -> dict:
x = data[FEATURES]
y = data[TARGET]
x_train, x_test, y_train, y_test = train_test_split(
x,
y,
test_size=0.2,
random_state=42,
)
model = RandomForestRegressor(
n_estimators=200,
max_depth=8,
random_state=42,
)
model.fit(x_train, y_train)
predictions = model.predict(x_test)
mae = mean_absolute_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
importances = sorted(
zip(FEATURES, model.feature_importances_),
key=lambda item: item[1],
reverse=True,
)
if verbose:
print("Model: RandomForestRegressor(n_estimators=200, max_depth=8, random_state=42)")
print(f"Mean Absolute Error: {mae:.2f} Lakhs")
print(f"R2 Score: {r2:.4f}")
print("Feature importances:")
for feature, importance in importances:
print(f" {feature}: {importance:.4f}")
print()
print(
"Reminder: these metrics reflect fit quality on synthetic, formula-generated "
"data. They demonstrate a working ingestion -> training -> evaluation "
"pipeline, not real-world predictive accuracy. See README.md for scope."
)
return {
"model": model,
"mae": mae,
"r2": r2,
"feature_importances": importances,
"x_train": x_train,
"x_test": x_test,
"y_train": y_train,
"y_test": y_test,
"predictions": predictions,
}
def main() -> None:
try:
data = load_and_clean_data(DATA_PATH)
except FileNotFoundError:
print(
"synthetic_housing_data.csv was not found. Compile and run data_engine "
"first to generate the dataset."
)
return
train_and_evaluate(data)
if __name__ == "__main__":
main()