Dataset Viewer
Auto-converted to Parquet Duplicate
text
stringlengths
0
81
# train_model.py
# Generates a synthetic dataset, trains an ML model, and saves cost_estimator.pkl
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
import joblib
np.random.seed(42)
# Settings
n_samples = 800
project_types = ["Residential", "Commercial", "Infrastructure", "Road"]
location_opts = ["Urban", "Semi-Urban", "Rural"]
material_opts = ["Low", "Medium", "High"]
# base rates (USD per m2) for synthetic generation
base_rates = {
"Residential": 350,
"Commercial": 500,
"Infrastructure": 700,
"Road": 400
}
material_factor = {"Low": 0.9, "Medium": 1.0, "High": 1.2}
location_factor = {"Urban": 1.1, "Semi-Urban": 1.0, "Rural": 0.85}
rows = []
for _ in range(n_samples):
pt = np.random.choice(project_types)
loc = np.random.choice(location_opts)
mat = np.random.choice(material_opts)
# area ranges by project type
if pt == "Residential":
area = int(np.random.uniform(50, 400))
elif pt == "Commercial":
area = int(np.random.uniform(200, 3000))
elif pt == "Infrastructure":
area = int(np.random.uniform(500, 5000))
else: # Road
area = int(np.random.uniform(1000, 10000))
# generate cost with some noise
base = base_rates[pt]
cost = area * base * material_factor[mat] * location_factor[loc]
noise = np.random.normal(loc=0.0, scale=0.08 * cost) # 8% noise
cost = max(100.0, cost + noise)
rows.append({
"Project_Type": pt,
"Area_m2": area,
"Location": loc,
"Material_Quality": mat,
"Cost_USD": round(cost, 2)
})
df = pd.DataFrame(rows)
print("Sample data:")
print(df.head())
# train/test split (simple)
X = df.drop(columns=["Cost_USD"])
y = df["Cost_USD"]
categorical = ["Project_Type", "Location", "Material_Quality"]
numeric = ["Area_m2"]
preprocessor = ColumnTransformer([
("cat", OneHotEncoder(handle_unknown="ignore"), categorical),
("num", "passthrough", numeric)
])
pipeline = Pipeline([
("preproc", preprocessor),
("rf", RandomForestRegressor(n_estimators=100, random_state=42))
])
print("Training model...")
pipeline.fit(X, y)
# save
joblib.dump(pipeline, "cost_estimator.pkl")
print("Saved model to cost_estimator.pkl")

No dataset card yet

Downloads last month
3