Multiple123 commited on
Commit
e6a0696
·
verified ·
1 Parent(s): f609caa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -49,18 +49,34 @@ explainer = shap.KernelExplainer(_predict_proba_nd, bg_array)
49
  def _render_force_plot(base_val: float, shap_1d: np.ndarray, feat_1d: np.ndarray, fnames):
50
  """返回 matplotlib Figure(旧接口,服务器端稳定)"""
51
  plt.close('all')
 
 
 
 
 
 
 
 
 
 
 
 
52
  shap.force_plot(
53
  base_val,
54
  np.asarray(shap_1d).reshape(-1),
55
- np.asarray(feat_1d).reshape(-1),
56
  feature_names=list(fnames),
57
- matplotlib=True, show=False
 
58
  )
 
59
  fig = plt.gcf()
60
- fig.set_size_inches(12, 4)
61
- plt.tight_layout()
 
62
  return fig
63
 
 
64
  def _coerce_float(x):
65
  return float(x) if x is not None and x != "" else np.nan
66
 
 
49
  def _render_force_plot(base_val: float, shap_1d: np.ndarray, feat_1d: np.ndarray, fnames):
50
  """返回 matplotlib Figure(旧接口,服务器端稳定)"""
51
  plt.close('all')
52
+
53
+ # ——将特征值按需四舍五入,减少标签长度(可根据需要微调每列小数位)
54
+ feat = np.asarray(feat_1d, dtype=float).copy()
55
+ # 这里给出一套通用规则:ALB/TP/CREA/PNI 一位小数;TBA 两位;AST_ALT 与 AAPR 两位
56
+ round_map = {
57
+ "ALB": 2, "TP": 2, "TBA": 2, "AST_ALT": 2, "CREA": 1, "PNI": 1, "AAPR": 3
58
+ }
59
+ feat_rounded = [
60
+ np.round(val, round_map.get(name, 2)) for val, name in zip(feat, fnames)
61
+ ]
62
+
63
+ # 如需进一步减字,可把特征名缩写或去掉空格,这里保持原名
64
  shap.force_plot(
65
  base_val,
66
  np.asarray(shap_1d).reshape(-1),
67
+ np.asarray(feat_rounded).reshape(-1),
68
  feature_names=list(fnames),
69
+ matplotlib=True,
70
+ show=False
71
  )
72
+
73
  fig = plt.gcf()
74
+ fig.set_size_inches(14, 3.6) # 加宽、适当降低高度
75
+ fig.set_dpi(180) # 提高分辨率
76
+ plt.tight_layout(pad=0.6) # 更紧凑
77
  return fig
78
 
79
+
80
  def _coerce_float(x):
81
  return float(x) if x is not None and x != "" else np.nan
82