Spaces:
Sleeping
Sleeping
ericstrausak commited on
Commit ·
4d95ce6
1
Parent(s): 08cfc1e
added
Browse files- Updated_Apartment_Data.csv +0 -0
- app.py +62 -0
Updated_Apartment_Data.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from geopy.distance import geodesic
|
| 4 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.metrics import mean_absolute_error
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import joblib
|
| 9 |
+
|
| 10 |
+
# Load dataset
|
| 11 |
+
df = pd.read_csv("week2/Updated_Apartment_Data.csv")
|
| 12 |
+
|
| 13 |
+
# Define Zürich city center coordinates
|
| 14 |
+
zurich_center = (47.378177, 8.540192)
|
| 15 |
+
|
| 16 |
+
# Compute distance to city center
|
| 17 |
+
df["distance_to_center_km"] = df.apply(lambda row: geodesic((row["lat"], row["lon"]), zurich_center).km, axis=1)
|
| 18 |
+
|
| 19 |
+
# Define features and target
|
| 20 |
+
features = ["rooms", "area", "pop", "pop_dens", "frg_pct", "emp", "tax_income", "distance_to_center_km"]
|
| 21 |
+
target = "price"
|
| 22 |
+
|
| 23 |
+
# Split data
|
| 24 |
+
X_train, X_test, y_train, y_test = train_test_split(df[features], df[target], test_size=0.2, random_state=42)
|
| 25 |
+
|
| 26 |
+
# Train model
|
| 27 |
+
model = RandomForestRegressor(random_state=42)
|
| 28 |
+
model.fit(X_train, y_train)
|
| 29 |
+
|
| 30 |
+
# Evaluate model
|
| 31 |
+
y_pred = model.predict(X_test)
|
| 32 |
+
print("Mean Absolute Error:", mean_absolute_error(y_test, y_pred))
|
| 33 |
+
|
| 34 |
+
# Save model
|
| 35 |
+
joblib.dump(model, "apartment_price_model.pkl")
|
| 36 |
+
|
| 37 |
+
def predict_price(rooms, area, pop, pop_dens, frg_pct, emp, tax_income, distance_to_center_km):
|
| 38 |
+
model = joblib.load("apartment_price_model.pkl")
|
| 39 |
+
input_data = np.array([[rooms, area, pop, pop_dens, frg_pct, emp, tax_income, distance_to_center_km]])
|
| 40 |
+
prediction = model.predict(input_data)
|
| 41 |
+
return f"Estimated Price: CHF {prediction[0]:,.2f}"
|
| 42 |
+
|
| 43 |
+
# Create Gradio Interface
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=predict_price,
|
| 46 |
+
inputs=[
|
| 47 |
+
gr.Number(label="Rooms"),
|
| 48 |
+
gr.Number(label="Area (sqm)"),
|
| 49 |
+
gr.Number(label="Population"),
|
| 50 |
+
gr.Number(label="Population Density"),
|
| 51 |
+
gr.Number(label="Foreign Percentage"),
|
| 52 |
+
gr.Number(label="Employment"),
|
| 53 |
+
gr.Number(label="Taxable Income"),
|
| 54 |
+
gr.Number(label="Distance to City Center (km)"),
|
| 55 |
+
],
|
| 56 |
+
outputs="text",
|
| 57 |
+
title="Apartment Price Predictor",
|
| 58 |
+
description="Enter apartment details to estimate its price.",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Launch the Gradio App
|
| 62 |
+
iface.launch(share=True)
|