Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sklearn.model_selection import train_test_split, GridSearchCV
|
| 4 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 5 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# URL to the Excel dataset on Hugging Face
|
| 9 |
+
data_url = "https://huggingface.co/datasets/leadingbridge/flat/resolve/main/NorthPoint30.xlsx"
|
| 10 |
+
|
| 11 |
+
# Load dataset (using openpyxl as the engine)
|
| 12 |
+
df = pd.read_excel(data_url, engine="openpyxl")
|
| 13 |
+
|
| 14 |
+
# Drop columns that are not needed for prediction
|
| 15 |
+
cols_to_drop = ['Usage', 'Address', 'PricePerSquareFeet', 'InstrumentDate', 'Floor', 'Unit']
|
| 16 |
+
df.drop(columns=cols_to_drop, inplace=True, errors='ignore')
|
| 17 |
+
|
| 18 |
+
# Rename useful columns for consistency
|
| 19 |
+
df.rename(columns={"Floor.1": "Floor", "Unit.1": "Unit"}, inplace=True)
|
| 20 |
+
|
| 21 |
+
# Ensure the dataset has the necessary columns
|
| 22 |
+
required_columns = ['District', 'PriceInMillion', 'Longitude', 'Latitude', 'Floor', 'Unit', 'Area', 'Year', 'WeekNumber']
|
| 23 |
+
if not all(col in df.columns for col in required_columns):
|
| 24 |
+
raise ValueError("Dataset is missing one or more required columns.")
|
| 25 |
+
|
| 26 |
+
# Define features and target variable
|
| 27 |
+
feature_names = ['District', 'Longitude', 'Latitude', 'Floor', 'Unit', 'Area', 'Year', 'WeekNumber']
|
| 28 |
+
X = df[feature_names]
|
| 29 |
+
y = df['PriceInMillion']
|
| 30 |
+
|
| 31 |
+
# Split into training and test sets
|
| 32 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 33 |
+
|
| 34 |
+
# Define a parameter grid for RandomForestRegressor and perform grid search
|
| 35 |
+
rf_param_grid = {
|
| 36 |
+
'n_estimators': [50, 100, 150],
|
| 37 |
+
"max_depth": [4, 6, 8],
|
| 38 |
+
"max_features": ['sqrt', 'log2', 3],
|
| 39 |
+
"random_state": [42]
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
rf_grid = GridSearchCV(RandomForestRegressor(), rf_param_grid, refit=True, verbose=1, cv=5, error_score='raise')
|
| 43 |
+
rf_grid.fit(X_train, y_train)
|
| 44 |
+
|
| 45 |
+
# Use the best estimator
|
| 46 |
+
model = rf_grid.best_estimator_
|
| 47 |
+
|
| 48 |
+
# Print model performance on the test set
|
| 49 |
+
rf_pred = model.predict(X_test)
|
| 50 |
+
rf_rmse = np.sqrt(mean_squared_error(y_test, rf_pred))
|
| 51 |
+
rf_r2 = r2_score(y_test, rf_pred)
|
| 52 |
+
print("Random Forest RMSE: ", rf_rmse)
|
| 53 |
+
print("Random Forest R2: ", rf_r2)
|
| 54 |
+
|
| 55 |
+
def price_prediction(model, feature_names, new_data):
|
| 56 |
+
new_data_df = pd.DataFrame([new_data], columns=feature_names)
|
| 57 |
+
prediction = model.predict(new_data_df)
|
| 58 |
+
return prediction[0]
|
| 59 |
+
|
| 60 |
+
def predict(district, longitude, latitude, floor, unit, area, year, weeknumber):
|
| 61 |
+
new_data = [district, longitude, latitude, floor, unit, area, year, weeknumber]
|
| 62 |
+
prediction = price_prediction(model, feature_names, new_data)
|
| 63 |
+
return f"${prediction:,.2f} Million"
|
| 64 |
+
|
| 65 |
+
# Gradio Interface
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=predict,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.Dropdown(choices=list(range(1, 9)), label='District (1 = Taikoo Shing, 2 = Mei Foo Sun Chuen, 3 = South Horizons, 4 = Whampoa Garden)'),
|
| 70 |
+
gr.Number(label='Longitude'),
|
| 71 |
+
gr.Number(label='Latitude'),
|
| 72 |
+
gr.Dropdown(choices=list(range(1, 71)), label='Floor'),
|
| 73 |
+
gr.Dropdown(choices=list(range(1, 31)), label='Unit (e.g., A=1, B=2, C=3, ...)'),
|
| 74 |
+
gr.Slider(minimum=137, maximum=5000, step=1, label='Area (in sq. feet)'),
|
| 75 |
+
gr.Dropdown(choices=[2024, 2025], label='Year'),
|
| 76 |
+
gr.Dropdown(choices=list(range(1, 53)), label='Week Number')
|
| 77 |
+
],
|
| 78 |
+
outputs=gr.Textbox(label='Price Prediction'),
|
| 79 |
+
title="PROPERTY PRICE PREDICTION TOOL (Larry Pang Final Year Project)",
|
| 80 |
+
description="Predict the price of a new property based on District, Longitude, Latitude, Floor, Unit, Area, Year, and Week Number."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
iface.launch()
|