File size: 2,904 Bytes
83ecde8 bb227a6 4eee01d 83ecde8 904814d 83ecde8 904814d 83ecde8 904814d 83ecde8 bb227a6 2d47c91 bb227a6 83ecde8 bb227a6 83ecde8 e3a516c 83ecde8 bb227a6 83ecde8 904814d 83ecde8 904814d 83ecde8 bb227a6 83ecde8 bb227a6 83ecde8 bb227a6 83ecde8 bb227a6 83ecde8 bb227a6 83ecde8 bb227a6 83ecde8 bb227a6 83ecde8 904814d 83ecde8 904814d 83ecde8 904814d 83ecde8 904814d 83ecde8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 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
"""
) |