App / src /streamlit_app.py
Aghiless's picture
Update src/streamlit_app.py
2d47c91 verified
import streamlit as st
import pandas as pd
# ---------------------------------------------------
# PAGE SETTINGS
# ---------------------------------------------------
st.set_page_config(
page_title="Housing Price Estimator",
page_icon="🏠",
layout="centered"
)
# ---------------------------------------------------
# LOAD NEIGHBORHOOD SCORES
# ---------------------------------------------------
postal_scores = pd.read_csv("idf_postal_codes_neighborhood_scores.csv")
postal_scores["postal_code"] = postal_scores["postal_code"].astype(str)
# ---------------------------------------------------
# MODEL COEFFICIENTS (from your R model)
# ---------------------------------------------------
INTERCEPT = -467418.10
COEF_SURFACE = 4932.35
COEF_ROOMS = -29954.53
COEF_NEIGHBORHOOD = 79383.79
def predict_price(surface, rooms, neighborhood_score):
price = (
INTERCEPT
+ COEF_SURFACE * surface
+ COEF_ROOMS * rooms
+ COEF_NEIGHBORHOOD * neighborhood_score
)
return price
# ---------------------------------------------------
# TITLE
# ---------------------------------------------------
st.title("🏠 Île-de-France Housing Price Estimator 2")
st.write(
"""
Estimate the price of a property using a **linear regression model**
trained on real estate data from Île-de-France.
"""
)
# ---------------------------------------------------
# USER INPUTS
# ---------------------------------------------------
st.sidebar.header("Property Information")
postal_code = st.sidebar.text_input(
"Postal code",
value="75001"
)
surface = st.sidebar.slider(
"Surface (m²)",
20,
300,
70
)
rooms = st.sidebar.slider(
"Number of rooms",
1,
10,
3
)
# ---------------------------------------------------
# FIND NEIGHBORHOOD SCORE
# ---------------------------------------------------
def get_neighborhood_score(postal_code):
result = postal_scores[
postal_scores["postal_code"] == postal_code
]
if len(result) == 0:
return None
return result["neighborhood_score"].values[0]
# ---------------------------------------------------
# PREDICTION
# ---------------------------------------------------
if st.sidebar.button("Estimate price"):
score = get_neighborhood_score(postal_code)
if score is None:
st.error("Postal code not found in the dataset.")
else:
price = predict_price(surface, rooms, score)
st.subheader("Estimated Property Price")
st.success(f"{int(price):,} €")
st.write("Neighborhood score:", round(score, 2))
# ---------------------------------------------------
# MODEL FORMULA
# ---------------------------------------------------
st.write("---")
st.write("### Model Formula")
st.latex(
r"""
Price =
-467418
+ 4932 \times Surface
- 29954 \times Rooms
+ 79383 \times NeighborhoodScore
"""
)