Update src/streamlit_app.py
Browse files- src/streamlit_app.py +161 -97
src/streamlit_app.py
CHANGED
|
@@ -1,97 +1,161 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
# ---------------------------------------------------
|
| 6 |
+
# PAGE CONFIG
|
| 7 |
+
# ---------------------------------------------------
|
| 8 |
+
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Île-de-France Housing Price Estimator",
|
| 11 |
+
page_icon="🏠",
|
| 12 |
+
layout="wide"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# ---------------------------------------------------
|
| 16 |
+
# LOAD DATA
|
| 17 |
+
# ---------------------------------------------------
|
| 18 |
+
|
| 19 |
+
df = pd.read_csv("housing_analysis_dataset-3.csv")
|
| 20 |
+
|
| 21 |
+
# ---------------------------------------------------
|
| 22 |
+
# MODEL COEFFICIENTS (from R)
|
| 23 |
+
# ---------------------------------------------------
|
| 24 |
+
|
| 25 |
+
INTERCEPT = -467418.10
|
| 26 |
+
COEF_SURFACE = 4932.35
|
| 27 |
+
COEF_ROOMS = -29954.53
|
| 28 |
+
COEF_NEIGHBORHOOD = 79383.79
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def predict_price(surface, rooms, neighborhood_score):
|
| 32 |
+
|
| 33 |
+
price = (
|
| 34 |
+
INTERCEPT
|
| 35 |
+
+ COEF_SURFACE * surface
|
| 36 |
+
+ COEF_ROOMS * rooms
|
| 37 |
+
+ COEF_NEIGHBORHOOD * neighborhood_score
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
return price
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ---------------------------------------------------
|
| 44 |
+
# TITLE
|
| 45 |
+
# ---------------------------------------------------
|
| 46 |
+
|
| 47 |
+
st.title("🏠 Île-de-France Housing Price Estimator")
|
| 48 |
+
|
| 49 |
+
st.write(
|
| 50 |
+
"""
|
| 51 |
+
This dashboard estimates housing prices using a **linear regression model**
|
| 52 |
+
trained on the **DVF real estate dataset**.
|
| 53 |
+
"""
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# ---------------------------------------------------
|
| 57 |
+
# SIDEBAR INPUTS
|
| 58 |
+
# ---------------------------------------------------
|
| 59 |
+
|
| 60 |
+
st.sidebar.header("Property characteristics")
|
| 61 |
+
|
| 62 |
+
surface = st.sidebar.slider(
|
| 63 |
+
"Surface (m²)",
|
| 64 |
+
20,
|
| 65 |
+
300,
|
| 66 |
+
70
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
rooms = st.sidebar.slider(
|
| 70 |
+
"Number of rooms",
|
| 71 |
+
1,
|
| 72 |
+
10,
|
| 73 |
+
3
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
neighborhood_score = st.sidebar.slider(
|
| 77 |
+
"Neighborhood score",
|
| 78 |
+
3.0,
|
| 79 |
+
10.0,
|
| 80 |
+
7.0
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# ---------------------------------------------------
|
| 84 |
+
# PREDICTION
|
| 85 |
+
# ---------------------------------------------------
|
| 86 |
+
|
| 87 |
+
if st.sidebar.button("Estimate price"):
|
| 88 |
+
|
| 89 |
+
price = predict_price(surface, rooms, neighborhood_score)
|
| 90 |
+
|
| 91 |
+
st.subheader("Estimated Property Price")
|
| 92 |
+
|
| 93 |
+
st.success(f"{int(price):,} €")
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# ---------------------------------------------------
|
| 97 |
+
# DATASET OVERVIEW
|
| 98 |
+
# ---------------------------------------------------
|
| 99 |
+
|
| 100 |
+
st.write("---")
|
| 101 |
+
st.header("Dataset Overview")
|
| 102 |
+
|
| 103 |
+
col1, col2, col3 = st.columns(3)
|
| 104 |
+
|
| 105 |
+
col1.metric("Number of properties", len(df))
|
| 106 |
+
col2.metric("Average price", f"{int(df['price'].mean()):,} €")
|
| 107 |
+
col3.metric("Average price per m²", f"{int(df['price_m2'].mean()):,} €")
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# ---------------------------------------------------
|
| 111 |
+
# PRICE DISTRIBUTION
|
| 112 |
+
# ---------------------------------------------------
|
| 113 |
+
|
| 114 |
+
st.write("---")
|
| 115 |
+
st.header("Price Distribution")
|
| 116 |
+
|
| 117 |
+
fig, ax = plt.subplots()
|
| 118 |
+
|
| 119 |
+
ax.hist(df["price"], bins=50)
|
| 120 |
+
|
| 121 |
+
ax.set_xlabel("Price (€)")
|
| 122 |
+
ax.set_ylabel("Number of properties")
|
| 123 |
+
|
| 124 |
+
st.pyplot(fig)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------
|
| 128 |
+
# PRICE VS SURFACE
|
| 129 |
+
# ---------------------------------------------------
|
| 130 |
+
|
| 131 |
+
st.write("---")
|
| 132 |
+
st.header("Price vs Surface")
|
| 133 |
+
|
| 134 |
+
fig2, ax2 = plt.subplots()
|
| 135 |
+
|
| 136 |
+
ax2.scatter(df["surface"], df["price"], alpha=0.3)
|
| 137 |
+
|
| 138 |
+
ax2.set_xlabel("Surface (m²)")
|
| 139 |
+
ax2.set_ylabel("Price (€)")
|
| 140 |
+
|
| 141 |
+
st.pyplot(fig2)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
# ---------------------------------------------------
|
| 145 |
+
# MODEL EXPLANATION
|
| 146 |
+
# ---------------------------------------------------
|
| 147 |
+
|
| 148 |
+
st.write("---")
|
| 149 |
+
st.header("Model")
|
| 150 |
+
|
| 151 |
+
st.write("The prediction is based on the following linear regression model:")
|
| 152 |
+
|
| 153 |
+
st.latex(
|
| 154 |
+
r'''
|
| 155 |
+
Price =
|
| 156 |
+
-467418
|
| 157 |
+
+ 4932 \times Surface
|
| 158 |
+
- 29954 \times Rooms
|
| 159 |
+
+ 79383 \times NeighborhoodScore
|
| 160 |
+
'''
|
| 161 |
+
)
|