prakharg24 commited on
Commit
f46f2d9
·
verified ·
1 Parent(s): f3b5d1b

Create rashomon_effect.py

Browse files
Files changed (1) hide show
  1. my_pages/rashomon_effect.py +92 -0
my_pages/rashomon_effect.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pages/rashomon_effect.py
2
+ 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?")