Update my_pages/rashomon_effect.py
Browse files- my_pages/rashomon_effect.py +65 -82
my_pages/rashomon_effect.py
CHANGED
|
@@ -1,93 +1,76 @@
|
|
| 1 |
-
# pages/rashomon_effect.py
|
| 2 |
import streamlit as st
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
st.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
st.
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 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?")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
def render():
|
| 6 |
st.title("Rashomon Effect")
|
| 7 |
+
|
| 8 |
# Generate synthetic data
|
| 9 |
np.random.seed(42)
|
| 10 |
n_points = 100
|
| 11 |
+
income = np.random.normal(50, 15, n_points)
|
| 12 |
+
credit = np.random.normal(50, 15, n_points)
|
| 13 |
+
labels = (income + credit > 100).astype(int) # 1 = paid back, 0 = default
|
| 14 |
+
|
| 15 |
+
colors = ['green' if label == 1 else 'red' for label in labels]
|
| 16 |
+
|
| 17 |
+
# Function to plot scatter
|
| 18 |
+
def plot_scatter(x, y, colors, title="", decision_boundary=None, boundary_type=None, highlight_point=None):
|
| 19 |
+
fig, ax = plt.subplots(figsize=(5, 5))
|
| 20 |
+
ax.scatter(x, y, c=colors, alpha=0.6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
ax.set_xlabel("Annual Income")
|
| 22 |
ax.set_ylabel("Credit Score")
|
| 23 |
+
ax.set_title(title)
|
| 24 |
+
|
| 25 |
+
# Decision boundary
|
| 26 |
+
if decision_boundary is not None:
|
| 27 |
+
if boundary_type == "vertical":
|
| 28 |
+
ax.axvline(decision_boundary, color='blue', linestyle='--')
|
| 29 |
+
ax.fill_betweenx(np.arange(min(y), max(y)), decision_boundary, max(x), alpha=0.1, color='green')
|
| 30 |
+
ax.fill_betweenx(np.arange(min(y), max(y)), min(x), decision_boundary, alpha=0.1, color='red')
|
| 31 |
+
elif boundary_type == "horizontal":
|
| 32 |
+
ax.axhline(decision_boundary, color='blue', linestyle='--')
|
| 33 |
+
ax.fill_between(np.arange(min(x), max(x)), decision_boundary, max(y), alpha=0.1, color='green')
|
| 34 |
+
ax.fill_between(np.arange(min(x), max(x)), min(y), decision_boundary, alpha=0.1, color='red')
|
| 35 |
+
|
| 36 |
+
# Highlight specific point
|
| 37 |
+
if highlight_point is not None:
|
| 38 |
+
ax.scatter(*highlight_point, c='yellow', edgecolors='black', s=200, zorder=5)
|
| 39 |
+
|
| 40 |
return fig
|
| 41 |
+
|
| 42 |
+
# Top scatter plot (centered to match smaller width)
|
| 43 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 44 |
+
with col2:
|
| 45 |
+
st.pyplot(plot_scatter(income, credit, colors, title="Original Data"))
|
| 46 |
+
|
| 47 |
+
# Side-by-side decision boundary plots
|
| 48 |
+
col_left, col_right = st.columns(2)
|
| 49 |
+
with col_left:
|
| 50 |
+
st.pyplot(plot_scatter(income, credit, colors, title="Vertical Boundary",
|
| 51 |
+
decision_boundary=55, boundary_type="vertical"))
|
| 52 |
+
left_selected = st.button("Choose Vertical Boundary")
|
| 53 |
+
with col_right:
|
| 54 |
+
st.pyplot(plot_scatter(income, credit, colors, title="Horizontal Boundary",
|
| 55 |
+
decision_boundary=55, boundary_type="horizontal"))
|
| 56 |
+
right_selected = st.button("Choose Horizontal Boundary")
|
| 57 |
+
|
| 58 |
+
# Show new individual based on selection
|
| 59 |
+
if left_selected:
|
| 60 |
+
new_point = (40, 80) # High credit score, low income
|
| 61 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 62 |
+
with col2:
|
| 63 |
+
st.pyplot(plot_scatter(income, credit, colors,
|
| 64 |
+
title="Vertical Boundary + New Individual",
|
| 65 |
+
decision_boundary=55, boundary_type="vertical",
|
| 66 |
+
highlight_point=new_point))
|
| 67 |
+
st.warning("This individual was rejected by your chosen model. Why not choose a model that helps them?")
|
| 68 |
+
elif right_selected:
|
| 69 |
+
new_point = (80, 40) # Low credit score, high income
|
| 70 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 71 |
+
with col2:
|
| 72 |
+
st.pyplot(plot_scatter(income, credit, colors,
|
| 73 |
+
title="Horizontal Boundary + New Individual",
|
| 74 |
+
decision_boundary=55, boundary_type="horizontal",
|
| 75 |
+
highlight_point=new_point))
|
| 76 |
+
st.warning("This individual was rejected by your chosen model. Why not choose a model that helps them?")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|