Spaces:
Sleeping
Sleeping
Added training the model and update app.py
Browse files- .gitignore +1 -0
- app.py +38 -0
- bfs_municipality_and_tax_data.csv +0 -0
- random_forest_regression_with_custom_feature.pkl +3 -0
- requirements.txt +0 -0
- trainModel.ipynb +76 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv/
|
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Load dataset
|
| 7 |
+
df = pd.read_csv("bfs_municipality_and_tax_data.csv")
|
| 8 |
+
|
| 9 |
+
# Load trained model
|
| 10 |
+
with open("random_forest_regression_with_custom_feature.pkl", "rb") as f:
|
| 11 |
+
model = pickle.load(f)
|
| 12 |
+
|
| 13 |
+
# Function for prediction
|
| 14 |
+
def predict_price(municipality, tax_income):
|
| 15 |
+
# Get population density for the selected municipality
|
| 16 |
+
pop_dens = df[df["bfs_name"] == municipality]["pop_dens"].values
|
| 17 |
+
if len(pop_dens) == 0:
|
| 18 |
+
pop_dens = [1000] # Default value if not found
|
| 19 |
+
|
| 20 |
+
# Prepare input data
|
| 21 |
+
input_data = np.array([[tax_income, pop_dens[0]]])
|
| 22 |
+
predicted_price = model.predict(input_data)[0]
|
| 23 |
+
|
| 24 |
+
return f"Estimated Property Price: CHF {predicted_price:,.2f} (Population Density: {pop_dens[0]} per km²)"
|
| 25 |
+
|
| 26 |
+
# Gradio UI
|
| 27 |
+
gradio_ui = gr.Interface(
|
| 28 |
+
fn=predict_price,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Textbox(label="Municipality"),
|
| 31 |
+
gr.Number(label="Taxable Income (CHF)")
|
| 32 |
+
],
|
| 33 |
+
outputs=gr.Text(label="Predicted Property Price"),
|
| 34 |
+
title="Apartment Price Prediction with Population Density"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
gradio_ui.launch()
|
bfs_municipality_and_tax_data.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
random_forest_regression_with_custom_feature.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cf08a9b15318604c4feb3a94d3c0fb366ee52f4a0f38c72058735421056b2fe7
|
| 3 |
+
size 15516395
|
requirements.txt
ADDED
|
Binary file (5.4 kB). View file
|
|
|
trainModel.ipynb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"name": "stdout",
|
| 10 |
+
"output_type": "stream",
|
| 11 |
+
"text": [
|
| 12 |
+
"✅ Model trained successfully! Mean Absolute Error: 1009.95\n",
|
| 13 |
+
"✅ Model saved as random_forest_regression_with_custom_feature.pkl\n"
|
| 14 |
+
]
|
| 15 |
+
}
|
| 16 |
+
],
|
| 17 |
+
"source": [
|
| 18 |
+
"\n",
|
| 19 |
+
"import pandas as pd\n",
|
| 20 |
+
"import pickle\n",
|
| 21 |
+
"from sklearn.ensemble import RandomForestRegressor\n",
|
| 22 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 23 |
+
"from sklearn.metrics import mean_absolute_error\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"df = pd.read_csv(\"bfs_municipality_and_tax_data.csv\")\n",
|
| 26 |
+
"\n",
|
| 27 |
+
"df[\"tax_income\"] = df[\"tax_income\"].astype(str).str.replace(r\"[^\\d.]\", \"\", regex=True).astype(float)\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"df[\"pop_dens\"] = pd.to_numeric(df[\"pop_dens\"], errors=\"coerce\")\n",
|
| 30 |
+
"\n",
|
| 31 |
+
"df.fillna(df.select_dtypes(include=[\"number\"]).median(), inplace=True)\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"X = df[[\"tax_income\", \"pop_dens\"]]\n",
|
| 34 |
+
"y = df[\"tax_income\"] \n",
|
| 35 |
+
"\n",
|
| 36 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
|
| 37 |
+
"\n",
|
| 38 |
+
"model = RandomForestRegressor(n_estimators=100, random_state=42)\n",
|
| 39 |
+
"model.fit(X_train, y_train)\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"y_pred = model.predict(X_test)\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"# Evaluate performance\n",
|
| 44 |
+
"mae = mean_absolute_error(y_test, y_pred)\n",
|
| 45 |
+
"print(f\"Model trained successfully! Mean Absolute Error: {mae:.2f}\")\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"# Save the trained model\n",
|
| 48 |
+
"with open(\"random_forest_regression_with_custom_feature.pkl\", \"wb\") as f:\n",
|
| 49 |
+
" pickle.dump(model, f)\n",
|
| 50 |
+
"\n",
|
| 51 |
+
"print(\"Model saved as random_forest_regression_with_custom_feature.pkl\")\n"
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
],
|
| 55 |
+
"metadata": {
|
| 56 |
+
"kernelspec": {
|
| 57 |
+
"display_name": "venv",
|
| 58 |
+
"language": "python",
|
| 59 |
+
"name": "python3"
|
| 60 |
+
},
|
| 61 |
+
"language_info": {
|
| 62 |
+
"codemirror_mode": {
|
| 63 |
+
"name": "ipython",
|
| 64 |
+
"version": 3
|
| 65 |
+
},
|
| 66 |
+
"file_extension": ".py",
|
| 67 |
+
"mimetype": "text/x-python",
|
| 68 |
+
"name": "python",
|
| 69 |
+
"nbconvert_exporter": "python",
|
| 70 |
+
"pygments_lexer": "ipython3",
|
| 71 |
+
"version": "3.13.2"
|
| 72 |
+
}
|
| 73 |
+
},
|
| 74 |
+
"nbformat": 4,
|
| 75 |
+
"nbformat_minor": 2
|
| 76 |
+
}
|