prakharg24 commited on
Commit
bb39a0f
·
verified ·
1 Parent(s): 0d3001e

Update my_pages/rashomon_effect.py

Browse files
Files changed (1) hide show
  1. my_pages/rashomon_effect.py +88 -87
my_pages/rashomon_effect.py CHANGED
@@ -3,90 +3,91 @@ import streamlit as st
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
 
6
- st.title("Rashomon Effect")
7
-
8
- # Generate synthetic data
9
- np.random.seed(42)
10
- n_points = 100
11
-
12
- # Mostly correlated data
13
- annual_income = np.random.normal(50, 15, n_points) # x-axis
14
- credit_score = annual_income + np.random.normal(0, 10, n_points) # y-axis
15
-
16
- # Default labels: 1 = paid back (green), 0 = default (red)
17
- labels = (annual_income + credit_score > 120).astype(int)
18
- colors = np.where(labels == 1, 'green', 'red')
19
-
20
- # --- Function to draw scatter with optional decision boundary ---
21
- def draw_scatter(x, y, colors, boundary=None, boundary_type=None, extra_point=None):
22
- fig, ax = plt.subplots()
23
-
24
- # Background for decision boundary
25
- if boundary is not None and boundary_type:
26
- x_min, x_max = ax.get_xlim()
27
- y_min, y_max = ax.get_ylim()
28
-
29
- if boundary_type == "vertical":
30
- ax.axvline(boundary, color="black", linestyle="--")
31
- ax.fill_betweenx([y_min, y_max], x_min, boundary, color="red", alpha=0.1)
32
- ax.fill_betweenx([y_min, y_max], boundary, x_max, color="green", alpha=0.1)
33
-
34
- elif boundary_type == "horizontal":
35
- ax.axhline(boundary, color="black", linestyle="--")
36
- ax.fill_between([x_min, x_max], y_min, boundary, color="red", alpha=0.1)
37
- ax.fill_between([x_min, x_max], boundary, y_max, color="green", alpha=0.1)
38
-
39
- ax.scatter(x, y, c=colors, edgecolors='black')
40
-
41
- if extra_point:
42
- ax.scatter(*extra_point["coords"], c=extra_point["color"], s=150, edgecolors="black", marker="o")
43
-
44
- ax.set_xlabel("Annual Income")
45
- ax.set_ylabel("Credit Score")
46
-
47
- return fig
48
-
49
- # --- Layout ---
50
- col_top = st.container()
51
- col_bottom = st.columns(2)
52
-
53
- # Draw top scatter (raw data)
54
- with col_top:
55
- fig_top = draw_scatter(annual_income, credit_score, colors)
56
- st.pyplot(fig_top)
57
- st.markdown("⬇️ Data split into two different models ⬇️")
58
-
59
- # Session state to store choice
60
- if "chosen_boundary" not in st.session_state:
61
- st.session_state.chosen_boundary = None
62
-
63
- # Bottom left: vertical decision boundary
64
- with col_bottom[0]:
65
- fig_left = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="vertical")
66
- st.pyplot(fig_left)
67
- if st.button("Choose Vertical Model"):
68
- st.session_state.chosen_boundary = "vertical"
69
-
70
- # Bottom right: horizontal decision boundary
71
- with col_bottom[1]:
72
- fig_right = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="horizontal")
73
- st.pyplot(fig_right)
74
- if st.button("Choose Horizontal Model"):
75
- st.session_state.chosen_boundary = "horizontal"
76
-
77
- # After choice: add extra individual
78
- if st.session_state.chosen_boundary:
79
- st.markdown("---")
80
- st.subheader("Your chosen model in action")
81
-
82
- if st.session_state.chosen_boundary == "vertical":
83
- extra = {"coords": (40, 80), "color": "blue"}
84
- fig_choice = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="vertical", extra_point=extra)
85
- st.pyplot(fig_choice)
86
- st.warning("This person has a high credit score but low income. You rejected them. Why not choose a model that would approve them?")
87
-
88
- elif st.session_state.chosen_boundary == "horizontal":
89
- extra = {"coords": (80, 40), "color": "blue"}
90
- fig_choice = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="horizontal", extra_point=extra)
91
- st.pyplot(fig_choice)
92
- st.warning("This person has high income but low credit score. You rejected them. Why not choose a model that would approve them?")
 
 
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
 
6
+ def render():
7
+ st.title("Rashomon Effect")
8
+
9
+ # Generate synthetic data
10
+ np.random.seed(42)
11
+ n_points = 100
12
+
13
+ # Mostly correlated data
14
+ annual_income = np.random.normal(50, 15, n_points) # x-axis
15
+ credit_score = annual_income + np.random.normal(0, 10, n_points) # y-axis
16
+
17
+ # Default labels: 1 = paid back (green), 0 = default (red)
18
+ labels = (annual_income + credit_score > 120).astype(int)
19
+ colors = np.where(labels == 1, 'green', 'red')
20
+
21
+ # --- Function to draw scatter with optional decision boundary ---
22
+ def draw_scatter(x, y, colors, boundary=None, boundary_type=None, extra_point=None):
23
+ fig, ax = plt.subplots()
24
+
25
+ # Background for decision boundary
26
+ if boundary is not None and boundary_type:
27
+ x_min, x_max = ax.get_xlim()
28
+ y_min, y_max = ax.get_ylim()
29
+
30
+ if boundary_type == "vertical":
31
+ ax.axvline(boundary, color="black", linestyle="--")
32
+ ax.fill_betweenx([y_min, y_max], x_min, boundary, color="red", alpha=0.1)
33
+ ax.fill_betweenx([y_min, y_max], boundary, x_max, color="green", alpha=0.1)
34
+
35
+ elif boundary_type == "horizontal":
36
+ ax.axhline(boundary, color="black", linestyle="--")
37
+ ax.fill_between([x_min, x_max], y_min, boundary, color="red", alpha=0.1)
38
+ ax.fill_between([x_min, x_max], boundary, y_max, color="green", alpha=0.1)
39
+
40
+ ax.scatter(x, y, c=colors, edgecolors='black')
41
+
42
+ if extra_point:
43
+ ax.scatter(*extra_point["coords"], c=extra_point["color"], s=150, edgecolors="black", marker="o")
44
+
45
+ ax.set_xlabel("Annual Income")
46
+ ax.set_ylabel("Credit Score")
47
+
48
+ return fig
49
+
50
+ # --- Layout ---
51
+ col_top = st.container()
52
+ col_bottom = st.columns(2)
53
+
54
+ # Draw top scatter (raw data)
55
+ with col_top:
56
+ fig_top = draw_scatter(annual_income, credit_score, colors)
57
+ st.pyplot(fig_top)
58
+ st.markdown("⬇️ Data split into two different models ⬇️")
59
+
60
+ # Session state to store choice
61
+ if "chosen_boundary" not in st.session_state:
62
+ st.session_state.chosen_boundary = None
63
+
64
+ # Bottom left: vertical decision boundary
65
+ with col_bottom[0]:
66
+ fig_left = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="vertical")
67
+ st.pyplot(fig_left)
68
+ if st.button("Choose Vertical Model"):
69
+ st.session_state.chosen_boundary = "vertical"
70
+
71
+ # Bottom right: horizontal decision boundary
72
+ with col_bottom[1]:
73
+ fig_right = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="horizontal")
74
+ st.pyplot(fig_right)
75
+ if st.button("Choose Horizontal Model"):
76
+ st.session_state.chosen_boundary = "horizontal"
77
+
78
+ # After choice: add extra individual
79
+ if st.session_state.chosen_boundary:
80
+ st.markdown("---")
81
+ st.subheader("Your chosen model in action")
82
+
83
+ if st.session_state.chosen_boundary == "vertical":
84
+ extra = {"coords": (40, 80), "color": "blue"}
85
+ fig_choice = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="vertical", extra_point=extra)
86
+ st.pyplot(fig_choice)
87
+ st.warning("This person has a high credit score but low income. You rejected them. Why not choose a model that would approve them?")
88
+
89
+ elif st.session_state.chosen_boundary == "horizontal":
90
+ extra = {"coords": (80, 40), "color": "blue"}
91
+ fig_choice = draw_scatter(annual_income, credit_score, colors, boundary=55, boundary_type="horizontal", extra_point=extra)
92
+ st.pyplot(fig_choice)
93
+ st.warning("This person has high income but low credit score. You rejected them. Why not choose a model that would approve them?")