Update my_pages/rashomon_effect.py
Browse files- 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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
labels = (
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
st.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
st.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
st.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
| 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?")
|