saviopreis commited on
Commit
a0a3f6e
·
1 Parent(s): 04a4fe0

add prob_% column (probability of predicted class)

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -106,14 +106,21 @@ def _run(train_df, test_df, target_col, task, n_estimators, progress=gr.Progress
106
  clf = TabFMClassifier(model=model, random_state=42, n_estimators=int(n_estimators))
107
  clf.fit(Xtr, ytr)
108
  preds = clf.predict(Xte)
 
 
 
 
109
  else:
110
  reg = TabFMRegressor(model=model, random_state=42, n_estimators=int(n_estimators))
111
  reg.fit(Xtr, ytr)
112
  preds = reg.predict(Xte)
 
113
  infer_s = time.time() - t0
114
 
115
  out = test_df.copy()
116
  out["tabfm_prediction"] = preds
 
 
117
 
118
  summary = f"Loaded weights in {load_s:.1f}s (cached after first run). Inference took {infer_s:.1f}s for {len(Xte)} test rows.\n"
119
  if has_target_in_test:
@@ -187,14 +194,17 @@ def _run_stock_backtest(dataset_key, ticker, n_estimators, progress=gr.Progress(
187
 
188
  progress(0.6, desc=f"Predicting {len(Xva)} test rows for {ticker}...")
189
  clf.fit(data["Xtr"], ytr_bin)
190
- pred_bin = clf.predict(Xva)
191
- pred = np.where(pred_bin == 1, "BUY", "SELL")
 
 
192
 
193
  acc = (pred == yva_str).mean()
194
  out = pd.DataFrame({
195
  "date": [m["date"] for m in meta],
196
  "actual_label": yva_str,
197
  "tabfm_prediction": pred,
 
198
  "actual_5d_fwd_return_%": [round(m["fwd_return"] * 100, 2) for m in meta],
199
  "correct": pred == yva_str,
200
  }).sort_values("date")
 
106
  clf = TabFMClassifier(model=model, random_state=42, n_estimators=int(n_estimators))
107
  clf.fit(Xtr, ytr)
108
  preds = clf.predict(Xte)
109
+ proba = clf.predict_proba(Xte)
110
+ classes = list(clf.classes_)
111
+ pred_idx = [classes.index(p) for p in preds]
112
+ prob_pred = np.array([proba[i, pred_idx[i]] for i in range(len(preds))])
113
  else:
114
  reg = TabFMRegressor(model=model, random_state=42, n_estimators=int(n_estimators))
115
  reg.fit(Xtr, ytr)
116
  preds = reg.predict(Xte)
117
+ prob_pred = None
118
  infer_s = time.time() - t0
119
 
120
  out = test_df.copy()
121
  out["tabfm_prediction"] = preds
122
+ if prob_pred is not None:
123
+ out["prob_%"] = (prob_pred * 100).round(1)
124
 
125
  summary = f"Loaded weights in {load_s:.1f}s (cached after first run). Inference took {infer_s:.1f}s for {len(Xte)} test rows.\n"
126
  if has_target_in_test:
 
194
 
195
  progress(0.6, desc=f"Predicting {len(Xva)} test rows for {ticker}...")
196
  clf.fit(data["Xtr"], ytr_bin)
197
+ proba = clf.predict_proba(Xva)
198
+ pred_bin = (proba[:, 1] > proba[:, 0]).astype(int)
199
+ pred = np.where(pred_bin == 1, "BUY", "SELL")
200
+ prob_pred = np.where(pred_bin == 1, proba[:, 1], proba[:, 0])
201
 
202
  acc = (pred == yva_str).mean()
203
  out = pd.DataFrame({
204
  "date": [m["date"] for m in meta],
205
  "actual_label": yva_str,
206
  "tabfm_prediction": pred,
207
+ "prob_%": (prob_pred * 100).round(1),
208
  "actual_5d_fwd_return_%": [round(m["fwd_return"] * 100, 2) for m in meta],
209
  "correct": pred == yva_str,
210
  }).sort_values("date")