test / train_model.py
leadingbridge's picture
Create train_model.py
1c1ad43 verified
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
import joblib
# URL to the Excel dataset on Hugging Face
data_url = "https://huggingface.co/datasets/leadingbridge/flat/resolve/main/NorthPoint30.xlsx"
# Load dataset
df = pd.read_excel(data_url, engine="openpyxl")
# Drop columns that are not needed for prediction
cols_to_drop = ['Usage', 'Address', 'PricePerSquareFeet', 'InstrumentDate', 'Floor', 'Unit']
df.drop(columns=cols_to_drop, inplace=True, errors='ignore')
# Rename useful columns for consistency
df.rename(columns={"Floor.1": "Floor", "Unit.1": "Unit"}, inplace=True)
# Define features and target variable
feature_names = ['District', 'Longitude', 'Latitude', 'Floor', 'Unit', 'Area', 'Year', 'WeekNumber']
X = df[feature_names]
y = df['PriceInMillion']
# Train/test split
X_train, _, y_train, _ = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a fixed-parameter RandomForest (no grid search)
model = RandomForestRegressor(
n_estimators=100,
max_depth=6,
max_features='sqrt',
random_state=42
)
model.fit(X_train, y_train)
# Save model and feature list
joblib.dump({"model": model, "features": feature_names}, "model.pkl")
print("βœ… Model trained and saved to model.pkl")