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+
from sklearn.linear_model import LinearRegression
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.metrics import mean_absolute_error, r2_score
|
| 7 |
+
import plotly.express as px
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
+
|
| 10 |
+
# Configuration de la page
|
| 11 |
+
st.set_page_config(
|
| 12 |
+
page_title="IA Prédilo - Immobilier",
|
| 13 |
+
page_icon="🏠",
|
| 14 |
+
layout="wide"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Style CSS pour une interface premium
|
| 18 |
+
st.markdown("""
|
| 19 |
+
<style>
|
| 20 |
+
.prediction-box {
|
| 21 |
+
background-color: #ffffff;
|
| 22 |
+
padding: 25px;
|
| 23 |
+
border-radius: 15px;
|
| 24 |
+
border-left: 10px solid #007bff;
|
| 25 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
|
| 26 |
+
margin: 20px 0;
|
| 27 |
+
}
|
| 28 |
+
</style>
|
| 29 |
+
""", unsafe_allow_html=True)
|
| 30 |
+
|
| 31 |
+
# ==========================================================
|
| 32 |
+
# 1. LE JEU DE DONNÉES (Dataset - 30 exemples exacts)
|
| 33 |
+
# ==========================================================
|
| 34 |
+
@st.cache_data
|
| 35 |
+
def load_data():
|
| 36 |
+
donnees = {
|
| 37 |
+
'm2': [30, 50, 70, 90, 110, 130, 30, 50, 70, 50, 45, 85, 120, 60, 95, 55, 75, 100, 65, 80, 40, 90, 115, 140, 35, 60, 95, 105, 125, 150],
|
| 38 |
+
'dist': [1, 2, 3, 5, 8, 10, 15, 20, 25, 2, 4, 6, 12, 5, 8, 3, 4, 7, 2, 5, 1, 6, 9, 11, 18, 3, 7, 8, 10, 13],
|
| 39 |
+
'neuf': [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],
|
| 40 |
+
'pieces': [1, 2, 3, 4, 4, 5, 1, 2, 3, 2, 2, 3, 4, 3, 4, 2, 3, 4, 3, 3, 1, 4, 5, 6, 1, 2, 4, 4, 5, 6],
|
| 41 |
+
'parking': [0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1],
|
| 42 |
+
'etage': [0, 2, 1, 3, 0, 1, 0, 1, 0, 4, 2, 3, 0, 2, 5, 3, 1, 2, 5, 4, 0, 3, 1, 0, 2, 4, 6, 2, 1, 3],
|
| 43 |
+
'balcon': [0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1],
|
| 44 |
+
'jardin': [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1],
|
| 45 |
+
'ascenseur':[0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1],
|
| 46 |
+
'dpe': [4, 3, 4, 3, 5, 4, 6, 5, 6, 2, 4, 3, 1, 4, 3, 2, 4, 2, 1, 3, 5, 2, 3, 4, 6, 2, 3, 1, 3, 1],
|
| 47 |
+
'annee': [1985, 2000, 1995, 2005, 1980, 1990, 1975, 1988, 1970, 2020, 1998, 2008, 2022, 2002, 2010, 2018, 1992, 2015, 2021, 2005, 1982, 2012, 2003, 1995, 1978, 2019, 2011, 2023, 2007, 2024],
|
| 48 |
+
'etat': [2, 2, 2, 2, 1, 2, 1, 1, 1, 3, 2, 2, 3, 2, 2, 3, 2, 3, 3, 2, 1, 3, 2, 2, 1, 3, 2, 3, 2, 3],
|
| 49 |
+
'prix': [180000, 275000, 362000, 418000, 485000, 515000, 115000, 182000, 238000, 315000, 220000, 380000, 540000, 290000, 430000, 298000, 355000, 475000, 325000, 395000, 195000, 445000, 520000, 580000, 145000, 310000, 460000, 495000, 535000, 625000]
|
| 50 |
+
}
|
| 51 |
+
return pd.DataFrame(donnees)
|
| 52 |
+
|
| 53 |
+
df = load_data()
|
| 54 |
+
|
| 55 |
+
# ==========================================================
|
| 56 |
+
# 2. ENTRAÎNEMENT DE L'IA (Train / Test Split)
|
| 57 |
+
# ==========================================================
|
| 58 |
+
features = ['m2', 'dist', 'neuf', 'pieces', 'parking', 'etage', 'balcon', 'jardin', 'ascenseur', 'dpe', 'annee', 'etat']
|
| 59 |
+
X = df[features]
|
| 60 |
+
y = df['prix']
|
| 61 |
+
|
| 62 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 63 |
+
model = LinearRegression()
|
| 64 |
+
model.fit(X_train, y_train)
|
| 65 |
+
|
| 66 |
+
y_pred_test = model.predict(X_test)
|
| 67 |
+
mae_test = mean_absolute_error(y_test, y_pred_test)
|
| 68 |
+
score_r2 = r2_score(y_test, y_pred_test)
|
| 69 |
+
|
| 70 |
+
# ==========================================================
|
| 71 |
+
# 3. INTERFACE (Sidebar & Menu)
|
| 72 |
+
# ==========================================================
|
| 73 |
+
st.sidebar.header("🔧 Paramètres du bien")
|
| 74 |
+
|
| 75 |
+
surface = st.sidebar.slider("Surface (m2)", 20, 300, 75)
|
| 76 |
+
pieces = st.sidebar.number_input("Nombre de pièces", 1, 10, 3)
|
| 77 |
+
distance = st.sidebar.slider("Distance centre (km)", 0, 50, 5)
|
| 78 |
+
neuf_o_n = st.sidebar.selectbox("Est-ce neuf ?", ["NON", "OUI"])
|
| 79 |
+
annee = st.sidebar.number_input("Année de construction", 1900, 2025, 2010)
|
| 80 |
+
etat = st.sidebar.select_slider("État général", options=["A rénover", "Bon", "Excellent"], value="Bon")
|
| 81 |
+
dpe_label = st.sidebar.select_slider("Classe DPE", options=["A", "B", "C", "D", "E", "F", "G"], value="C")
|
| 82 |
+
|
| 83 |
+
st.sidebar.subheader("Équipements")
|
| 84 |
+
parking = st.sidebar.checkbox("Parking / Garage", value=True)
|
| 85 |
+
balcon = st.sidebar.checkbox("Balcon / Terrasse")
|
| 86 |
+
jardin = st.sidebar.checkbox("Jardin")
|
| 87 |
+
ascenseur = st.sidebar.checkbox("Ascenseur")
|
| 88 |
+
|
| 89 |
+
prix_vendeur = st.sidebar.number_input("Prix du vendeur (€)", 50000, 1000000, 350000)
|
| 90 |
+
|
| 91 |
+
# Conversion des entrées pour l'IA
|
| 92 |
+
code_neuf = 1 if neuf_o_n == "OUI" else 0
|
| 93 |
+
code_etat = {"A rénover": 1, "Bon": 2, "Excellent": 3}[etat]
|
| 94 |
+
code_dpe = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7}[dpe_label]
|
| 95 |
+
code_park = 1 if parking else 0
|
| 96 |
+
code_balc = 1 if balcon else 0
|
| 97 |
+
code_jard = 1 if jardin else 0
|
| 98 |
+
code_asc = 1 if ascenseur else 0
|
| 99 |
+
|
| 100 |
+
# ==========================================================
|
| 101 |
+
# 4. CALCULS ET RÉSULTATS
|
| 102 |
+
# ==========================================================
|
| 103 |
+
st.title("🚀 Prédilo IA : Expert Immobilier")
|
| 104 |
+
st.markdown("---")
|
| 105 |
+
|
| 106 |
+
# Prédiction en temps réel
|
| 107 |
+
input_data = [[surface, distance, code_neuf, pieces, code_park, 0, code_balc, code_jard, code_asc, code_dpe, annee, code_etat]]
|
| 108 |
+
estimation = model.predict(input_data)[0]
|
| 109 |
+
|
| 110 |
+
# Affichage des métriques clés
|
| 111 |
+
c1, c2, c3, c4 = st.columns(4)
|
| 112 |
+
with c1: st.metric("Estimation IA", f"{estimation:,.0f} €")
|
| 113 |
+
with c2: st.metric("Prix Vendeur", f"{prix_vendeur:,.0f} €")
|
| 114 |
+
with c3: st.metric("Précision IA", f"{score_r2:.1%}")
|
| 115 |
+
with c4: st.metric("Erreur (Test)", f"± {mae_test:,.0f} €")
|
| 116 |
+
|
| 117 |
+
# Verdict visuel
|
| 118 |
+
st.markdown('<div class="prediction-box">', unsafe_allow_html=True)
|
| 119 |
+
if prix_vendeur < (estimation - mae_test):
|
| 120 |
+
st.success("🎯 **VERDICT : EXCELLENTE AFFAIRE !**")
|
| 121 |
+
st.write(f"Ce bien est sous-évalué d'environ **{estimation - prix_vendeur:,.0f} €**.")
|
| 122 |
+
elif prix_vendeur > (estimation + mae_test):
|
| 123 |
+
st.error("❌ **VERDICT : TROP CHER !**")
|
| 124 |
+
st.write(f"Le prix est supérieur à la réalité du marché de **{prix_vendeur - estimation:,.0f} €**.")
|
| 125 |
+
else:
|
| 126 |
+
st.info("⚖️ **VERDICT : PRIX CORRECT !**")
|
| 127 |
+
st.write("Le prix est parfaitement aligné avec les tendances actuelles.")
|
| 128 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 129 |
+
|
| 130 |
+
# ==========================================================
|
| 131 |
+
# 5. GRAPHIQUES PLOTLY
|
| 132 |
+
# ==========================================================
|
| 133 |
+
t1, t2 = st.tabs(["📊 Distribution du Marché", "📈 Relation Surface/Prix"])
|
| 134 |
+
|
| 135 |
+
with t1:
|
| 136 |
+
fig_hist = px.histogram(df, x="prix", title="Où se situe votre bien ?", color_discrete_sequence=['#007bff'])
|
| 137 |
+
fig_hist.add_vline(x=estimation, line_dash="dash", line_color="red", annotation_text="VOTRE ESTIMATION")
|
| 138 |
+
st.plotly_chart(fig_hist, use_container_width=True)
|
| 139 |
+
|
| 140 |
+
with t2:
|
| 141 |
+
fig_scat = px.scatter(df, x="m2", y="prix", title="Prix en fonction de la Surface", labels={"m2": "Surface (m2)", "prix": "Prix (€)"})
|
| 142 |
+
fig_scat.add_trace(go.Scatter(x=[surface], y=[estimation], mode='markers', name='Votre Bien', marker=dict(size=15, color='red', symbol='star')))
|
| 143 |
+
st.plotly_chart(fig_scat, use_container_width=True)
|
| 144 |
+
|
| 145 |
+
st.divider()
|
| 146 |
+
st.caption("Développé avec ❤️ pour votre apprentissage de l'IA.")
|