UCS2014 commited on
Commit
39899e8
·
verified ·
1 Parent(s): fdd615a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -14,7 +14,7 @@ import matplotlib.pyplot as plt
14
  from matplotlib.ticker import FuncFormatter
15
 
16
  import plotly.graph_objects as go
17
- from sklearn.metrics import mean_squared_error, mean_absolute_error
18
 
19
  # =========================
20
  # Constants (Ym variant)
@@ -220,6 +220,12 @@ def _excel_safe_name(name: str) -> str:
220
  safe = ''.join('_' if ch in bad else ch for ch in str(name))
221
  return safe[:31]
222
 
 
 
 
 
 
 
223
  def _round_numeric(df: pd.DataFrame, ndigits: int = 3) -> pd.DataFrame:
224
  out = df.copy()
225
  for c in out.columns:
@@ -772,12 +778,12 @@ if st.session_state.app_step == "dev":
772
  st.session_state.results["m_train"]={
773
  "R": pearson_r(tr[TARGET], tr[PRED_COL]),
774
  "RMSE": rmse(tr[TARGET], tr[PRED_COL]),
775
- "MAE": mean_absolute_error(tr[TARGET], tr[PRED_COL])
776
  }
777
  st.session_state.results["m_test"]={
778
  "R": pearson_r(te[TARGET], te[PRED_COL]),
779
  "RMSE": rmse(te[TARGET], te[PRED_COL]),
780
- "MAE": mean_absolute_error(te[TARGET], te[PRED_COL])
781
  }
782
 
783
  tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
@@ -786,15 +792,15 @@ if st.session_state.app_step == "dev":
786
 
787
  def _dev_block(df, m):
788
  c1,c2,c3 = st.columns(3)
789
- c1.metric("R", f"{m['R']:.2f}")
790
  c2.metric("RMSE", f"{m['RMSE']:.2f}")
791
- c3.metric("MAE", f"{m['MAE']:.2f}")
792
 
793
  st.markdown("""
794
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
795
  <strong>R:</strong> Pearson Correlation Coefficient<br>
796
  <strong>RMSE:</strong> Root Mean Square Error<br>
797
- <strong>MAE:</strong> Mean Absolute Error
798
  </div>
799
  """, unsafe_allow_html=True)
800
 
@@ -860,7 +866,7 @@ if st.session_state.app_step == "validate":
860
  st.session_state.results["m_val"]={
861
  "R": pearson_r(df[TARGET], df[PRED_COL]),
862
  "RMSE": rmse(df[TARGET], df[PRED_COL]),
863
- "MAE": mean_absolute_error(df[TARGET], df[PRED_COL])
864
  }
865
  st.session_state.results["sv_val"]={"n":len(df), "pred_min":float(df[PRED_COL].min()), "pred_max":float(df[PRED_COL].max()), "oor":oor_pct}
866
  st.session_state.results["oor_tbl"]=tbl
@@ -868,15 +874,15 @@ if st.session_state.app_step == "validate":
868
  if "Validate" in st.session_state.results:
869
  m = st.session_state.results["m_val"]
870
  c1,c2,c3 = st.columns(3)
871
- c1.metric("R", f"{m['R']:.2f}")
872
  c2.metric("RMSE", f"{m['RMSE']:.2f}")
873
- c3.metric("MAE", f"{m['MAE']:.2f}")
874
 
875
  st.markdown("""
876
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
877
  <strong>R:</strong> Pearson Correlation Coefficient<br>
878
  <strong>RMSE:</strong> Root Mean Square Error<br>
879
- <strong>MAE:</strong> Mean Absolute Error
880
  </div>
881
  """, unsafe_allow_html=True)
882
 
 
14
  from matplotlib.ticker import FuncFormatter
15
 
16
  import plotly.graph_objects as go
17
+ from sklearn.metrics import mean_squared_error
18
 
19
  # =========================
20
  # Constants (Ym variant)
 
220
  safe = ''.join('_' if ch in bad else ch for ch in str(name))
221
  return safe[:31]
222
 
223
+ def mape(y_true, y_pred, eps: float = 1e-8) -> float:
224
+ a = np.asarray(y_true, dtype=float)
225
+ p = np.asarray(y_pred, dtype=float)
226
+ denom = np.where(np.abs(a) < eps, np.nan, np.abs(a)) # ignore near-zero actuals
227
+ return float(np.nanmean(np.abs(a - p) / denom) * 100.0) # percent
228
+
229
  def _round_numeric(df: pd.DataFrame, ndigits: int = 3) -> pd.DataFrame:
230
  out = df.copy()
231
  for c in out.columns:
 
778
  st.session_state.results["m_train"]={
779
  "R": pearson_r(tr[TARGET], tr[PRED_COL]),
780
  "RMSE": rmse(tr[TARGET], tr[PRED_COL]),
781
+ "MAPE": mape(tr[TARGET], tr[PRED_COL])
782
  }
783
  st.session_state.results["m_test"]={
784
  "R": pearson_r(te[TARGET], te[PRED_COL]),
785
  "RMSE": rmse(te[TARGET], te[PRED_COL]),
786
+ "MAPE": mape(te[TARGET], te[PRED_COL])
787
  }
788
 
789
  tr_min = tr[FEATURES].min().to_dict(); tr_max = tr[FEATURES].max().to_dict()
 
792
 
793
  def _dev_block(df, m):
794
  c1,c2,c3 = st.columns(3)
795
+ c1.metric("R", f"{m['R']:.3f}")
796
  c2.metric("RMSE", f"{m['RMSE']:.2f}")
797
+ c3.metric("MAPE (%)", f"{m['MAPE']:.2f}")
798
 
799
  st.markdown("""
800
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
801
  <strong>R:</strong> Pearson Correlation Coefficient<br>
802
  <strong>RMSE:</strong> Root Mean Square Error<br>
803
+ <strong>MAPE:</strong> Mean Absolute Percentage Error
804
  </div>
805
  """, unsafe_allow_html=True)
806
 
 
866
  st.session_state.results["m_val"]={
867
  "R": pearson_r(df[TARGET], df[PRED_COL]),
868
  "RMSE": rmse(df[TARGET], df[PRED_COL]),
869
+ "MAPE": mape(df[TARGET], df[PRED_COL])
870
  }
871
  st.session_state.results["sv_val"]={"n":len(df), "pred_min":float(df[PRED_COL].min()), "pred_max":float(df[PRED_COL].max()), "oor":oor_pct}
872
  st.session_state.results["oor_tbl"]=tbl
 
874
  if "Validate" in st.session_state.results:
875
  m = st.session_state.results["m_val"]
876
  c1,c2,c3 = st.columns(3)
877
+ c1.metric("R", f"{m['R']:.3f}")
878
  c2.metric("RMSE", f"{m['RMSE']:.2f}")
879
+ c3.metric("MAPE (%)", f"{m['MAPE']:.2f}")
880
 
881
  st.markdown("""
882
  <div style='text-align: left; font-size: 0.8em; color: #6b7280; margin-top: -16px; margin-bottom: 8px;'>
883
  <strong>R:</strong> Pearson Correlation Coefficient<br>
884
  <strong>RMSE:</strong> Root Mean Square Error<br>
885
+ <strong>MAPE:</strong> Mean Absolute Percentage Error
886
  </div>
887
  """, unsafe_allow_html=True)
888