gdleds commited on
Commit
ad3b8ea
ยท
1 Parent(s): 8d58d54

grossemodif

Browse files
Files changed (1) hide show
  1. app.py +129 -29
app.py CHANGED
@@ -130,10 +130,18 @@ def load_raw_data() -> pd.DataFrame:
130
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
131
  # 2) FONCTION Dโ€™ENTRAรŽNEMENT + PRร‰DICTIONS
132
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
133
- @st.cache_resource(show_spinner="โš™๏ธ Entraรฎnement du modรจleโ€ฆ", ttl=None)
 
 
 
 
 
 
 
134
  def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
135
  """Retourne df_map prรชt pour la carte avec les colonnes
136
  proba_7j, proba_30j, โ€ฆ, proba_180j."""
 
137
  # a) Nettoyage
138
  df = df_raw.copy()
139
  df = df.rename(columns={"Feu prรฉvu": "event", "dรฉcompte": "duration"})
@@ -153,35 +161,14 @@ def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
153
  ]
154
  features = [f for f in features if f in df.columns]
155
 
156
- # c) split + Surv
157
- y_struct = Surv.from_dataframe("event", "duration", df)
158
- X_train, X_test, y_train, y_test = train_test_split(
159
- df[features], y_struct, test_size=0.3, random_state=42
160
- )
161
- ev_train, du_train = y_train["event"], y_train["duration"]
162
- ev_test, du_test = y_test["event"], y_test["duration"]
163
-
164
- # d) Pipeline XGBSurv
165
- pipe = Pipeline([
166
- ("imputer", SimpleImputer(strategy="median")),
167
- ("scaler", StandardScaler()),
168
- ("xgb", XGBRegressor(
169
- objective="survival:cox",
170
- n_estimators=100,
171
- learning_rate=0.05,
172
- max_depth=3,
173
- tree_method="hist",
174
- random_state=42,
175
- )),
176
- ])
177
- pipe.fit(X_train, du_train, xgb__sample_weight=ev_train)
178
 
179
- # e) Affiche C-index dans la sidebar
180
- log_hr_test = pipe.predict(X_test)
181
- c_index = concordance_index_censored(ev_test, du_test, log_hr_test)[0]
182
- st.sidebar.write(f"**C-index (test)** : {c_index:.3f}")
183
 
184
- # f) Estimation du baseline hazard (Cox factice)
185
  df_fake = pd.DataFrame({
186
  "duration": du_train,
187
  "event": ev_train,
@@ -192,6 +179,7 @@ def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
192
  dmat.set_float_info("label_lower_bound", df_fake["duration"])
193
  dmat.set_float_info("label_upper_bound", df_fake["duration"])
194
  dmat.set_float_info("weight", df_fake["event"])
 
195
  bst_fake = xgb_train(
196
  params={
197
  "objective": "survival:cox",
@@ -212,7 +200,6 @@ def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
212
  })
213
  cph = CoxPHFitter()
214
  cph.fit(df_risque, duration_col="duration", event_col="event", show_progress=False)
215
-
216
  baseline_cumhaz = cph.baseline_cumulative_hazard_
217
 
218
  def S0(t: int) -> float:
@@ -224,6 +211,7 @@ def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
224
  H0 = baseline_cumhaz.loc[idx[idx <= t]].iloc[-1, 0]
225
  return float(np.exp(-H0))
226
 
 
227
  horizons = {7: "proba_7j", 30: "proba_30j", 60: "proba_60j",
228
  90: "proba_90j", 180: "proba_180j"}
229
 
@@ -236,6 +224,118 @@ def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
236
  df_map = df[["latitude", "longitude", "ville"] + list(horizons.values())].copy()
237
  return df_map
238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
240
  # 3) AFFICHAGE SUR LA PAGE ยซ Accueil ยป
241
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
130
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
131
  # 2) FONCTION Dโ€™ENTRAรŽNEMENT + PRร‰DICTIONS
132
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
133
+
134
+
135
+ import mlflow.sklearn
136
+
137
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
138
+ # 2) FONCTION DE PREDICTIONS (sans entraรฎnement)
139
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
140
+ @st.cache_resource(show_spinner="โš™๏ธ Chargement du modรจleโ€ฆ", ttl=None)
141
  def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
142
  """Retourne df_map prรชt pour la carte avec les colonnes
143
  proba_7j, proba_30j, โ€ฆ, proba_180j."""
144
+
145
  # a) Nettoyage
146
  df = df_raw.copy()
147
  df = df.rename(columns={"Feu prรฉvu": "event", "dรฉcompte": "duration"})
 
161
  ]
162
  features = [f for f in features if f in df.columns]
163
 
164
+ # c) Chargement du modรจle depuis MLflow
165
+ model_uri = "models:/fire_survival/Production" # ou /1 pour la v1
166
+ pipe = mlflow.sklearn.load_model(model_uri)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
+ # d) Estimation baseline hazard avec Cox factice (comme avant)
169
+ y_struct = Surv.from_dataframe("event", "duration", df)
170
+ ev_train, du_train = y_struct["event"], y_struct["duration"]
 
171
 
 
172
  df_fake = pd.DataFrame({
173
  "duration": du_train,
174
  "event": ev_train,
 
179
  dmat.set_float_info("label_lower_bound", df_fake["duration"])
180
  dmat.set_float_info("label_upper_bound", df_fake["duration"])
181
  dmat.set_float_info("weight", df_fake["event"])
182
+
183
  bst_fake = xgb_train(
184
  params={
185
  "objective": "survival:cox",
 
200
  })
201
  cph = CoxPHFitter()
202
  cph.fit(df_risque, duration_col="duration", event_col="event", show_progress=False)
 
203
  baseline_cumhaz = cph.baseline_cumulative_hazard_
204
 
205
  def S0(t: int) -> float:
 
211
  H0 = baseline_cumhaz.loc[idx[idx <= t]].iloc[-1, 0]
212
  return float(np.exp(-H0))
213
 
214
+ # e) Calcul proba sur plusieurs horizons
215
  horizons = {7: "proba_7j", 30: "proba_30j", 60: "proba_60j",
216
  90: "proba_90j", 180: "proba_180j"}
217
 
 
224
  df_map = df[["latitude", "longitude", "ville"] + list(horizons.values())].copy()
225
  return df_map
226
 
227
+
228
+
229
+
230
+
231
+
232
+
233
+ # @st.cache_resource(show_spinner="โš™๏ธ Entraรฎnement du modรจleโ€ฆ", ttl=None)
234
+ # def train_model_and_predict(df_raw: pd.DataFrame) -> pd.DataFrame:
235
+ # """Retourne df_map prรชt pour la carte avec les colonnes
236
+ # proba_7j, proba_30j, โ€ฆ, proba_180j."""
237
+ # # a) Nettoyage
238
+ # df = df_raw.copy()
239
+ # df = df.rename(columns={"Feu prรฉvu": "event", "dรฉcompte": "duration"})
240
+ # df["event"] = df["event"].astype(bool)
241
+ # df["duration"] = df["duration"].fillna(0)
242
+
243
+ # # b) Features
244
+ # features = [
245
+ # "moyenne precipitations mois", "moyenne temperature mois",
246
+ # "moyenne evapotranspiration mois", "moyenne vitesse vent annรฉe",
247
+ # "moyenne vitesse vent mois", "moyenne temperature annรฉe",
248
+ # "RR", "UM", "ETPMON", "TN", "TX", "Nombre de feu par an",
249
+ # "Nombre de feu par mois", "jours_sans_pluie", "jours_TX_sup_30",
250
+ # "ETPGRILLE_7j", "compteur jours vers prochain feu",
251
+ # "compteur feu log", "Annรฉe", "Mois",
252
+ # "moyenne precipitations annรฉe", "moyenne evapotranspiration annรฉe",
253
+ # ]
254
+ # features = [f for f in features if f in df.columns]
255
+
256
+ # # c) split + Surv
257
+ # y_struct = Surv.from_dataframe("event", "duration", df)
258
+ # X_train, X_test, y_train, y_test = train_test_split(
259
+ # df[features], y_struct, test_size=0.3, random_state=42
260
+ # )
261
+ # ev_train, du_train = y_train["event"], y_train["duration"]
262
+ # ev_test, du_test = y_test["event"], y_test["duration"]
263
+
264
+ # # d) Pipeline XGBSurv
265
+ # pipe = Pipeline([
266
+ # ("imputer", SimpleImputer(strategy="median")),
267
+ # ("scaler", StandardScaler()),
268
+ # ("xgb", XGBRegressor(
269
+ # objective="survival:cox",
270
+ # n_estimators=100,
271
+ # learning_rate=0.05,
272
+ # max_depth=3,
273
+ # tree_method="hist",
274
+ # random_state=42,
275
+ # )),
276
+ # ])
277
+ # pipe.fit(X_train, du_train, xgb__sample_weight=ev_train)
278
+
279
+ # # e) Affiche C-index dans la sidebar
280
+ # log_hr_test = pipe.predict(X_test)
281
+ # c_index = concordance_index_censored(ev_test, du_test, log_hr_test)[0]
282
+ # st.sidebar.write(f"**C-index (test)** : {c_index:.3f}")
283
+
284
+ # # f) Estimation du baseline hazard (Cox factice)
285
+ # df_fake = pd.DataFrame({
286
+ # "duration": du_train,
287
+ # "event": ev_train,
288
+ # "const": 1,
289
+ # })
290
+ # dmat = DMatrix(df_fake[["const"]])
291
+ # dmat.set_float_info("label", df_fake["duration"])
292
+ # dmat.set_float_info("label_lower_bound", df_fake["duration"])
293
+ # dmat.set_float_info("label_upper_bound", df_fake["duration"])
294
+ # dmat.set_float_info("weight", df_fake["event"])
295
+ # bst_fake = xgb_train(
296
+ # params={
297
+ # "objective": "survival:cox",
298
+ # "eval_metric": "cox-nloglik",
299
+ # "learning_rate": 0.1,
300
+ # "max_depth": 1,
301
+ # "verbosity": 0,
302
+ # },
303
+ # dtrain=dmat,
304
+ # num_boost_round=100,
305
+ # )
306
+ # log_hr_fake = bst_fake.predict(dmat)
307
+
308
+ # df_risque = pd.DataFrame({
309
+ # "duration": du_train,
310
+ # "event": ev_train,
311
+ # "log_risque": log_hr_fake + np.random.normal(0, 1e-4, size=len(log_hr_fake)),
312
+ # })
313
+ # cph = CoxPHFitter()
314
+ # cph.fit(df_risque, duration_col="duration", event_col="event", show_progress=False)
315
+
316
+ # baseline_cumhaz = cph.baseline_cumulative_hazard_
317
+
318
+ # def S0(t: int) -> float:
319
+ # """Survie de base S0(t) = exp(-H0(t))."""
320
+ # idx = baseline_cumhaz.index
321
+ # if t in idx:
322
+ # H0 = baseline_cumhaz.loc[t].values[0]
323
+ # else:
324
+ # H0 = baseline_cumhaz.loc[idx[idx <= t]].iloc[-1, 0]
325
+ # return float(np.exp(-H0))
326
+
327
+ # horizons = {7: "proba_7j", 30: "proba_30j", 60: "proba_60j",
328
+ # 90: "proba_90j", 180: "proba_180j"}
329
+
330
+ # log_hr_all = pipe.predict(df[features])
331
+ # HR = np.exp(log_hr_all)
332
+
333
+ # for t, col in horizons.items():
334
+ # df[col] = 1 - (S0(t) ** HR) # P(event โ‰ค t)
335
+
336
+ # df_map = df[["latitude", "longitude", "ville"] + list(horizons.values())].copy()
337
+ # return df_map
338
+
339
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
340
  # 3) AFFICHAGE SUR LA PAGE ยซ Accueil ยป
341
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€