Marcel0123 commited on
Commit
1cb8d74
Β·
verified Β·
1 Parent(s): a1db69d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import gradio as gr
3
  import numpy as np
4
  import pandas as pd
@@ -8,7 +7,7 @@ from sklearn.preprocessing import StandardScaler
8
  from sklearn.decomposition import PCA
9
 
10
  # ======================
11
- # Labels (NL) voor features
12
  # ======================
13
  FEATURE_LABELS = {
14
  "age": "Leeftijd",
@@ -54,7 +53,7 @@ Doe alsof je een arts bent en kies links bovenin een waarde, bijvoorbeeld **chol
54
  """
55
 
56
  # ======================
57
- # Data
58
  # ======================
59
  def load_diabetes_df():
60
  d = datasets.load_diabetes()
@@ -64,8 +63,8 @@ def load_diabetes_df():
64
  df["target"] = y
65
  return df
66
 
67
- # Overzichtstabel (gemiddelde & % boven/onder mean)
68
  def compute_overview_table(df: pd.DataFrame):
 
69
  keys = ["bmi","bp","s1","s2","s3","s4","s5","s6"]
70
  rows = []
71
  for k in keys:
@@ -73,7 +72,7 @@ def compute_overview_table(df: pd.DataFrame):
73
  if vals.size == 0:
74
  continue
75
  mean = float(vals.mean())
76
- pct_above = float((vals > 0).mean() * 100.0) # standardised: 0 β‰ˆ overall mean
77
  pct_below = float((vals < 0).mean() * 100.0)
78
  rows.append({
79
  "Meting": FEATURE_LABELS.get(k, k),
@@ -90,9 +89,16 @@ def compute_overview_table(df: pd.DataFrame):
90
  return table, note
91
 
92
  # ======================
93
- # Kern: PCA biplot
94
  # ======================
95
- def pca_biplot(color_label="BMI (Body Mass Index)", arrow_scale=2.0, point_size=32, alpha=0.85, n_components=10, standardize=True):
 
 
 
 
 
 
 
96
  df = load_diabetes_df()
97
  overview_df, overview_note = compute_overview_table(df)
98
 
@@ -126,8 +132,11 @@ def pca_biplot(color_label="BMI (Body Mass Index)", arrow_scale=2.0, point_size=
126
  for i, key in enumerate(feature_keys):
127
  x_arrow = loadings[i, 0] * arrow_scale
128
  y_arrow = loadings[i, 1] * arrow_scale
129
- ax.arrow(0, 0, x_arrow, y_arrow, head_width=0.05, head_length=0.08, fc="k", ec="k", length_includes_head=True)
130
- ax.text(x_arrow * 1.08, y_arrow * 1.08, FEATURE_LABELS.get(key, key), fontsize=9, ha="center", va="center")
 
 
 
131
 
132
  ax.axhline(0, color="grey", linewidth=0.6, linestyle=":")
133
  ax.axvline(0, color="grey", linewidth=0.6, linestyle=":")
@@ -201,7 +210,7 @@ with gr.Blocks(title="PCA Biplot β€” Diabetes (NL labels)") as demo:
201
  n_components = gr.Slider(2, 10, value=10, step=1, label="Aantal PCA-componenten (voor variatieplot)")
202
  standardize = gr.Checkbox(value=True, label="Standaardiseer metingen (aanbevolen)")
203
  run_btn = gr.Button("Update visualisaties")
204
- gr.HTML('<div class="callout smallnote">πŸ’‘ *Tip:* kies links een meting (bijv. BMI of cholesterol) en klik daarna op <b>Update visualisaties</b>.</div>')
205
  with gr.Column(scale=2):
206
  plot_biplot = gr.Plot(label="PCA-biplot β€” punten + pijlen")
207
  plot_expl = gr.Plot(label="Uitlegvariantie per component")
@@ -212,8 +221,14 @@ with gr.Blocks(title="PCA Biplot β€” Diabetes (NL labels)") as demo:
212
  overview_note_md = gr.Markdown()
213
 
214
  inputs = [color_feat, arrow_scale, point_size, alpha, n_components, standardize]
215
- run_btn.click(fn=pca_biplot, inputs=inputs, outputs=[plot_biplot, plot_expl, table, summary, overview_tbl, overview_note_md])
216
- demo.load(fn=pca_biplot, inputs=inputs, outputs=[plot_biplot, plot_expl, table, summary, overview_tbl, overview_note_md])
 
 
217
 
218
  if __name__ == "__main__":
 
219
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False, show_api=False)
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import pandas as pd
 
7
  from sklearn.decomposition import PCA
8
 
9
  # ======================
10
+ # NL labels voor features
11
  # ======================
12
  FEATURE_LABELS = {
13
  "age": "Leeftijd",
 
53
  """
54
 
55
  # ======================
56
+ # Data helpers
57
  # ======================
58
  def load_diabetes_df():
59
  d = datasets.load_diabetes()
 
63
  df["target"] = y
64
  return df
65
 
 
66
  def compute_overview_table(df: pd.DataFrame):
67
+ """Gemiddelde + % boven/onder gemiddelde voor kernmetingen (gestandaardiseerd)."""
68
  keys = ["bmi","bp","s1","s2","s3","s4","s5","s6"]
69
  rows = []
70
  for k in keys:
 
72
  if vals.size == 0:
73
  continue
74
  mean = float(vals.mean())
75
+ pct_above = float((vals > 0).mean() * 100.0) # 0 β‰ˆ globaal gemiddelde
76
  pct_below = float((vals < 0).mean() * 100.0)
77
  rows.append({
78
  "Meting": FEATURE_LABELS.get(k, k),
 
89
  return table, note
90
 
91
  # ======================
92
+ # PCA biplot kernel
93
  # ======================
94
+ def pca_biplot(
95
+ color_label="BMI (Body Mass Index)",
96
+ arrow_scale=2.0,
97
+ point_size=32,
98
+ alpha=0.85,
99
+ n_components=10,
100
+ standardize=True,
101
+ ):
102
  df = load_diabetes_df()
103
  overview_df, overview_note = compute_overview_table(df)
104
 
 
132
  for i, key in enumerate(feature_keys):
133
  x_arrow = loadings[i, 0] * arrow_scale
134
  y_arrow = loadings[i, 1] * arrow_scale
135
+ ax.arrow(0, 0, x_arrow, y_arrow, head_width=0.05, head_length=0.08,
136
+ fc="k", ec="k", length_includes_head=True)
137
+ ax.text(x_arrow * 1.08, y_arrow * 1.08,
138
+ FEATURE_LABELS.get(key, key),
139
+ fontsize=9, ha="center", va="center")
140
 
141
  ax.axhline(0, color="grey", linewidth=0.6, linestyle=":")
142
  ax.axvline(0, color="grey", linewidth=0.6, linestyle=":")
 
210
  n_components = gr.Slider(2, 10, value=10, step=1, label="Aantal PCA-componenten (voor variatieplot)")
211
  standardize = gr.Checkbox(value=True, label="Standaardiseer metingen (aanbevolen)")
212
  run_btn = gr.Button("Update visualisaties")
213
+ gr.HTML('<div class="callout smallnote">πŸ’‘ <b>Tip:</b> kies links een meting (bijv. BMI of cholesterol) en klik daarna op <b>Update visualisaties</b>.</div>')
214
  with gr.Column(scale=2):
215
  plot_biplot = gr.Plot(label="PCA-biplot β€” punten + pijlen")
216
  plot_expl = gr.Plot(label="Uitlegvariantie per component")
 
221
  overview_note_md = gr.Markdown()
222
 
223
  inputs = [color_feat, arrow_scale, point_size, alpha, n_components, standardize]
224
+ run_btn.click(fn=pca_biplot, inputs=inputs,
225
+ outputs=[plot_biplot, plot_expl, table, summary, overview_tbl, overview_note_md])
226
+ demo.load(fn=pca_biplot, inputs=inputs,
227
+ outputs=[plot_biplot, plot_expl, table, summary, overview_tbl, overview_note_md])
228
 
229
  if __name__ == "__main__":
230
+ # Belangrijk voor Spaces: queue + ssr_mode uit
231
  demo.queue().launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False, show_api=False)
232
+
233
+
234
+