intro_to_xai / app.py
negarvahid's picture
Upload 5 files
b1ae1b2 verified
import marimo
__generated_with = "0.14.10"
app = marimo.App(width="medium")
@app.cell
def _():
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import altair as alt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.preprocessing import StandardScaler
import joblib
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import warnings
return (
DataLoader,
LinearRegression,
StandardScaler,
TensorDataset,
alt,
fetch_california_housing,
joblib,
mean_absolute_error,
mean_squared_error,
nn,
np,
optim,
pd,
px,
r2_score,
torch,
train_test_split,
)
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _(mo):
mo.md(r"""# eXplainable AI (XAI)""")
return
@app.cell
def _(mo):
mo.md(
"""
## What is XAI?
XAI refers to a set of techniques and methods that make the decision-making process of AI models transparent, understandable, and interpretable to humans.
"""
)
return
@app.cell
def _(mo):
mo.md(r"""When the complexity of a model increases, you get better accuracy, but then explaining how your model makes its decisions becomes harder. The feild of XAI tries to find the best trade-off between explainability and accuracy.""")
return
@app.cell
def _(mo):
mo.image("/Users/negarvahid/Desktop/intorduction_to_xai/increasing_complexity.png")
return
@app.cell
def _(mo):
mo.md(r"""## What is Interpretibility?""")
return
@app.cell
def _(mo):
mo.md(r"""You call a model *interpretable* when you can understand the internal mechanisms of a model.""")
return
@app.cell
def _(mo):
mo.md(r"""Let’s say you’re predicting someone’s risk of heart disease. If you use a linear regression model, you’ll get a clean equation like:""")
return
@app.cell
def _(mo):
mo.md(
r"""
$$
\text{Risk} = 0.3 \cdot \text{Age} + 0.5 \cdot \text{Cholesterol} + 0.7 \cdot \text{SmokingStatus}
$$
"""
)
return
@app.cell
def _(mo):
mo.md(r"""This model is interpretable. You can look at the numbers and instantly understand how each feature contributes to the prediction. You don’t need any extra tools, as **it’s transparent by design**.""")
return
@app.cell
def _(mo):
mo.md(r"""## What is Explainbility?""")
return
@app.cell
def _(mo):
mo.md(r"""Now imagine you use a deep neural network for the same task. The network might give highly accurate predictions, but its internal workings are opaque due to multiple hidden layers and non-linear transformations. You can't just "look" at the model and understand how it made a decision.""")
return
@app.cell
def _(mo):
mo.image("/Users/negarvahid/Desktop/intorduction_to_xai/Screenshot 2025-07-12 at 16.05.55.png")
return
@app.cell
def _(mo):
mo.md(
r"""
To make sense of this black-box model, you might use a tool like SHAP (SHapley Additive exPlanations) to show that for a particular patient, high cholesterol and smoking status increased their predicted risk by specific amounts. This is **explainability**
You're using a tool to externally explain the behavior of a complex, non-interpretable model.
"""
)
return
@app.cell
def _(mo):
mo.md(r"""#The California Housing Prices Dataset""")
return
@app.cell
def _(mo):
mo.md(
r"""
The California Housing Prices Dataset a tabular dataset that contains information collected from the 1990 California census, aimed at predicting the median house value (in USD) for various districts across California, based on features like:
- Median income of residents
- Average number of rooms per household
- Average number of bedrooms
- Population and household size
- Latitude and longitude
- Housing median age
-
Total instances: 20,640 rows (each one represents a housing block group)
Number of features: 8 input features + 1 target value (median house price)
"""
)
return
@app.cell
def _(mo):
mo.md(
r"""
### Load the dataset
Run the following cell to load the dataset:
"""
)
return
@app.cell
def _():
return
@app.cell
def _(fetch_california_housing, pd):
def load_data():
california = fetch_california_housing()
data = pd.DataFrame(california.data, columns=california.feature_names)
data['target'] = california.target
return data
data = load_data()
return data, load_data
@app.cell
def _(data):
data
return
@app.cell
def _(mo):
mo.md(
r"""
### Visualize the Dataset:
It would be great to visualize the dataset!
Run the cell below to get an interactive plot of the dataset:
"""
)
return
@app.cell(hide_code=True)
def _(alt, data, mo):
selection = alt.selection_point(fields=['index'], name='select')
data_sample = data.sample(n=200, random_state=42)
location_chart = mo.ui.altair_chart(
alt.Chart(data_sample.reset_index()).mark_circle(size=60).encode(
x=alt.X('Longitude:Q', title='Longitude', scale=alt.Scale(domain=[-124.5, -114.0])),
y=alt.Y('Latitude:Q', title='Latitude', scale=alt.Scale(domain=[32.0, 42.5])),
color=alt.Color('target:Q',
scale=alt.Scale(scheme='viridis', domain=[0, 5]),
title='Price ($100k)',
legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
size=alt.Size('AveBedrms:Q',
scale=alt.Scale(range=[100, 400], domain=[0, 2]),
title='Avg Bedrooms',
legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
tooltip=[
alt.Tooltip('Longitude:Q', title='Longitude', format='.3f'),
alt.Tooltip('Latitude:Q', title='Latitude', format='.3f'),
alt.Tooltip('target:Q', title='Price ($100k)', format='.2f'),
alt.Tooltip('AveBedrms:Q', title='Avg Bedrooms', format='.2f'),
alt.Tooltip('MedInc:Q', title='Median Income', format='.2f'),
alt.Tooltip('HouseAge:Q', title='House Age', format='.0f'),
alt.Tooltip('AveRooms:Q', title='Avg Rooms', format='.2f'),
alt.Tooltip('Population:Q', title='Population', format='.0f'),
alt.Tooltip('AveOccup:Q', title='Avg Occupancy', format='.2f')
],
opacity=alt.condition(selection, alt.value(1), alt.value(0.7))
).add_params(selection).properties(
title='California Housing: 500 Sample Points - Location, Price & Bedrooms (Click to Select)',
width=900,
height=700
).interactive()
)
price_bedrooms_chart = mo.ui.altair_chart(
alt.Chart(data_sample.reset_index()).mark_circle(size=60).encode(
x=alt.X('AveBedrms:Q', title='Average Bedrooms', scale=alt.Scale(domain=[0, 2])),
y=alt.Y('target:Q', title='Price ($100k)', scale=alt.Scale(domain=[0, 5])),
color=alt.Color('MedInc:Q',
scale=alt.Scale(scheme='plasma', domain=[0, 15]),
title='Median Income',
legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
size=alt.Size('Population:Q',
scale=alt.Scale(range=[100, 400], domain=[0, 3000]),
title='Population',
legend=alt.Legend(orient='top', titleFontSize=14, labelFontSize=12)),
tooltip=[
alt.Tooltip('AveBedrms:Q', title='Avg Bedrooms', format='.2f'),
alt.Tooltip('target:Q', title='Price ($100k)', format='.2f'),
alt.Tooltip('MedInc:Q', title='Median Income', format='.2f'),
alt.Tooltip('Population:Q', title='Population', format='.0f'),
alt.Tooltip('Longitude:Q', title='Longitude', format='.3f'),
alt.Tooltip('Latitude:Q', title='Latitude', format='.3f'),
alt.Tooltip('HouseAge:Q', title='House Age', format='.0f'),
alt.Tooltip('AveRooms:Q', title='Avg Rooms', format='.2f'),
alt.Tooltip('AveOccup:Q', title='Avg Occupancy', format='.2f')
],
opacity=alt.condition(selection, alt.value(1), alt.value(0.7))
).add_params(selection).properties(
title='Price vs Bedrooms: 500 Sample Points - Income & Population (Click to Select)',
width=900,
height=600
).interactive()
)
mo.md(f"""
# California Housing Dataset - 500 Sample Points Analysis
**Dataset Shape:** {data.shape[0]} total rows × {data.shape[1]} columns
**Sample Size:** 500 random points (for better visualization)
**Key Features:**
- **Location**: Longitude & Latitude
- **Price**: Median house value (target) - $0-500k
- **Bedrooms**: Average number of bedrooms (AveBedrms) - 0-2
- **Income**: Median income (MedInc) - $0-15k
- **Population**: Block group population - 0-3000
- **Age**: Median house age (HouseAge)
- **Rooms**: Average rooms per household (AveRooms)
- **Occupancy**: Average household members (AveOccup)
**How to Use:** Click on any point to select it and see its data highlighted. Selected points will appear more opaque.
---
""")
mo.md("## 1. Interactive Map: Location, Price & Bedrooms")
mo.md("**500 sample points** across California. **Color = Price** (viridis scale), **Size = Number of Bedrooms**. **Click on any point to select it** - selected points become more opaque. Hover for detailed information.")
location_chart
mo.md("## 2. Price vs Bedrooms Analysis")
mo.md("**500 sample points** showing price vs bedrooms relationship. **Color = Income** (plasma scale), **Size = Population**. **Click on any point to select it** - the same selection works across both charts.")
price_bedrooms_chart
return
@app.cell(hide_code=True)
def _(data, mo, px):
def create_map_plot():
fig = px.scatter(
data,
x='Longitude',
y='Latitude',
color='target',
color_continuous_scale='viridis',
title='California Housing Prices by Location',
labels={'target': 'Price ($100k)', 'Longitude': 'Longitude', 'Latitude': 'Latitude'},
hover_data=['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup']
)
fig.update_layout(
width=800,
height=600,
title_x=0.5
)
return fig
map_plot = create_map_plot()
interactive_map = mo.ui.plotly(map_plot)
return (interactive_map,)
@app.cell
def _(interactive_map):
interactive_map
return
@app.cell(hide_code=True)
def _(
DataLoader,
LinearRegression,
StandardScaler,
TensorDataset,
fetch_california_housing,
joblib,
mean_absolute_error,
mean_squared_error,
nn,
np,
optim,
pd,
r2_score,
torch,
train_test_split,
):
class NeuralNetwork(nn.Module):
def __init__(self, input_size=8, hidden_size=64, output_size=1):
super(NeuralNetwork, self).__init__()
self.layer1 = nn.Linear(input_size, hidden_size)
self.layer2 = nn.Linear(hidden_size, hidden_size // 2)
self.layer3 = nn.Linear(hidden_size // 2, hidden_size // 4)
self.layer4 = nn.Linear(hidden_size // 4, output_size)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.2)
def forward(self, x):
x = self.relu(self.layer1(x))
x = self.dropout(x)
x = self.relu(self.layer2(x))
x = self.dropout(x)
x = self.relu(self.layer3(x))
x = self.layer4(x)
return x
class PyTorchModel:
def __init__(self, input_size=8, hidden_size=64, epochs=100, lr=0.001):
self.model = NeuralNetwork(input_size, hidden_size)
self.epochs = epochs
self.lr = lr
self.criterion = nn.MSELoss()
self.optimizer = optim.Adam(self.model.parameters(), lr=lr)
self.is_trained = False
def fit(self, X, y):
# Convert to PyTorch tensors
X_tensor = torch.FloatTensor(X)
y_tensor = torch.FloatTensor(y.values if hasattr(y, 'values') else y).reshape(-1, 1)
# Create data loader
dataset = TensorDataset(X_tensor, y_tensor)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
# Training loop
self.model.train()
for epoch in range(self.epochs):
total_loss = 0
for batch_X, batch_y in dataloader:
self.optimizer.zero_grad()
outputs = self.model(batch_X)
loss = self.criterion(outputs, batch_y)
loss.backward()
self.optimizer.step()
total_loss += loss.item()
if (epoch + 1) % 20 == 0:
print(f" Epoch [{epoch+1}/{self.epochs}], Loss: {total_loss/len(dataloader):.4f}")
self.is_trained = True
def predict(self, X):
if not self.is_trained:
raise ValueError("Model not trained yet!")
self.model.eval()
with torch.no_grad():
X_tensor = torch.FloatTensor(X)
predictions = self.model(X_tensor)
return predictions.numpy().flatten()
class CaliforniaHousingKaggleStyle:
def __init__(self):
self.scaler = StandardScaler()
self.feature_names = None
self.X_train = None
self.X_test = None
self.y_train = None
self.y_test = None
def load_data(self):
"""Load California Housing dataset"""
print("Loading California Housing dataset...")
california = fetch_california_housing()
# Create DataFrame
self.data = pd.DataFrame(california.data, columns=california.feature_names)
self.data['target'] = california.target
# Store feature names
self.feature_names = california.feature_names
print(f"Dataset shape: {self.data.shape}")
print(f"Features: {list(self.feature_names)}")
print(f"Target: Median house value (in $100,000s)")
return self.data
def prepare_data(self, test_size=0.2, random_state=42):
"""Prepare data for training"""
print("\n=== Data Preparation ===")
# Separate features and target
X = self.data.drop('target', axis=1)
y = self.data['target']
# Split the data
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
X, y, test_size=test_size, random_state=random_state
)
# Scale the features
self.X_train_scaled = self.scaler.fit_transform(self.X_train)
self.X_test_scaled = self.scaler.transform(self.X_test)
print(f"Training set shape: {self.X_train.shape}")
print(f"Test set shape: {self.X_test.shape}")
return self.X_train_scaled, self.X_test_scaled, self.y_train, self.y_test
def train_models(self):
"""Train Linear Regression and Neural Network"""
print("\n=== Training Models ===")
# Train Linear Regression
print("Training Linear Regression...")
self.linear_model = LinearRegression()
self.linear_model.fit(self.X_train_scaled, self.y_train)
print(" ✓ Linear Regression trained successfully")
# Train Neural Network
print("Training Neural Network...")
self.nn_model = PyTorchModel(epochs=100, lr=0.001)
self.nn_model.fit(self.X_train_scaled, self.y_train)
print(" ✓ Neural Network trained successfully")
def evaluate_models(self):
"""Evaluate both models"""
print("\n=== Model Evaluation ===")
# Linear Regression evaluation
y_train_pred_lr = self.linear_model.predict(self.X_train_scaled)
y_test_pred_lr = self.linear_model.predict(self.X_test_scaled)
lr_train_rmse = np.sqrt(mean_squared_error(self.y_train, y_train_pred_lr))
lr_test_rmse = np.sqrt(mean_squared_error(self.y_test, y_test_pred_lr))
lr_train_mae = mean_absolute_error(self.y_train, y_train_pred_lr)
lr_test_mae = mean_absolute_error(self.y_test, y_test_pred_lr)
lr_train_r2 = r2_score(self.y_train, y_train_pred_lr)
lr_test_r2 = r2_score(self.y_test, y_test_pred_lr)
print("Linear Regression:")
print(f" Test RMSE: {lr_test_rmse:.4f}")
print(f" Test MAE: {lr_test_mae:.4f}")
print(f" Test R²: {lr_test_r2:.4f}")
# Neural Network evaluation
y_train_pred_nn = self.nn_model.predict(self.X_train_scaled)
y_test_pred_nn = self.nn_model.predict(self.X_test_scaled)
nn_train_rmse = np.sqrt(mean_squared_error(self.y_train, y_train_pred_nn))
nn_test_rmse = np.sqrt(mean_squared_error(self.y_test, y_test_pred_nn))
nn_train_mae = mean_absolute_error(self.y_train, y_train_pred_nn)
nn_test_mae = mean_absolute_error(self.y_test, y_test_pred_nn)
nn_train_r2 = r2_score(self.y_train, y_train_pred_nn)
nn_test_r2 = r2_score(self.y_test, y_test_pred_nn)
print("\nNeural Network:")
print(f" Test RMSE: {nn_test_rmse:.4f}")
print(f" Test MAE: {nn_test_mae:.4f}")
print(f" Test R²: {nn_test_r2:.4f}")
return {
'linear_regression': {
'train_rmse': lr_train_rmse,
'test_rmse': lr_test_rmse,
'train_mae': lr_train_mae,
'test_mae': lr_test_mae,
'train_r2': lr_train_r2,
'test_r2': lr_test_r2
},
'neural_network': {
'train_rmse': nn_train_rmse,
'test_rmse': nn_test_rmse,
'train_mae': nn_train_mae,
'test_mae': nn_test_mae,
'train_r2': nn_train_r2,
'test_r2': nn_test_r2
}
}
def save_models(self):
"""Save both models and their parameters"""
print("\n=== Saving Models ===")
# Save Linear Regression
linear_model_data = {
'model': self.linear_model,
'scaler': self.scaler,
'feature_names': self.feature_names,
'parameters': {
'coef_': self.linear_model.coef_.tolist(),
'intercept_': float(self.linear_model.intercept_)
}
}
joblib.dump(linear_model_data, 'linear_regression_model.pkl')
print("Linear Regression model saved to 'linear_regression_model.pkl'")
# Save Neural Network
nn_model_data = {
'model': self.nn_model,
'scaler': self.scaler,
'feature_names': self.feature_names,
'parameters': {
'state_dict': self.nn_model.model.state_dict(),
'input_size': 8,
'hidden_size': 64,
'epochs': 100,
'lr': 0.001
}
}
joblib.dump(nn_model_data, 'neural_network_model.pkl')
print("Neural Network model saved to 'neural_network_model.pkl'")
def predict_with_linear_regression(self, sample_data):
"""Make prediction with Linear Regression"""
if isinstance(sample_data, dict):
sample_df = pd.DataFrame([sample_data])
else:
sample_df = sample_data
# Ensure all features are present
for feature in self.feature_names:
if feature not in sample_df.columns:
raise ValueError(f"Missing feature: {feature}")
# Reorder columns to match training data
sample_df = sample_df[self.feature_names]
# Scale the data
sample_scaled = self.scaler.transform(sample_df)
# Make prediction
prediction = self.linear_model.predict(sample_scaled)[0]
return prediction
def predict_with_neural_network(self, sample_data):
"""Make prediction with Neural Network"""
if isinstance(sample_data, dict):
sample_df = pd.DataFrame([sample_data])
else:
sample_df = sample_data
# Ensure all features are present
for feature in self.feature_names:
if feature not in sample_df.columns:
raise ValueError(f"Missing feature: {feature}")
# Reorder columns to match training data
sample_df = sample_df[self.feature_names]
# Scale the data
sample_scaled = self.scaler.transform(sample_df)
# Make prediction
prediction = self.nn_model.predict(sample_scaled)[0]
return prediction
def main():
"""Main function"""
print("California Housing Price Prediction - Linear Regression & Neural Network")
print("=" * 70)
# Initialize
predictor = CaliforniaHousingKaggleStyle()
# Load data
data = predictor.load_data()
# Prepare data
predictor.prepare_data()
# Train models
predictor.train_models()
# Evaluate models
results = predictor.evaluate_models()
# Save models
predictor.save_models()
# Example predictions
print("\n=== Example Predictions ===")
sample_data = {
'MedInc': 8.3252,
'HouseAge': 41.0,
'AveRooms': 6.984127,
'AveBedrms': 1.023810,
'Population': 322.0,
'AveOccup': 2.555556,
'Latitude': 37.88,
'Longitude': -122.23
}
lr_prediction = predictor.predict_with_linear_regression(sample_data)
nn_prediction = predictor.predict_with_neural_network(sample_data)
print(f"Sample data: {sample_data}")
print(f"Linear Regression prediction: ${lr_prediction * 100000:.2f}")
print(f"Neural Network prediction: ${nn_prediction * 100000:.2f}")
main()
return
@app.cell
def _(mo):
mo.md(r"""You already have two models prepared for you. One is a linear regression, the other is a neural network. Run the followung cell to load them and make a sample prediction:""")
return
@app.cell(hide_code=True)
def _(joblib, pd):
def load_models():
linear_model_data = joblib.load('linear_regression_model.pkl')
nn_model_data = joblib.load('neural_network_model.pkl')
return linear_model_data, nn_model_data
def make_prediction(linear_model_data, nn_model_data, sample_data):
if isinstance(sample_data, dict):
sample_df = pd.DataFrame([sample_data])
else:
sample_df = sample_data
feature_names = linear_model_data['feature_names']
for feature in feature_names:
if feature not in sample_df.columns:
raise ValueError(f"Missing feature: {feature}")
sample_df = sample_df[feature_names]
scaler = linear_model_data['scaler']
sample_scaled = scaler.transform(sample_df)
linear_prediction = linear_model_data['model'].predict(sample_scaled)[0]
nn_model = nn_model_data['model']
nn_prediction = nn_model.predict(sample_scaled)[0]
return linear_prediction, nn_prediction
def predict_with_models():
print("Loading saved models...")
linear_model_data, nn_model_data = load_models()
print("Models loaded successfully!")
sample_data = {
'MedInc': 8.3252,
'HouseAge': 41.0,
'AveRooms': 6.984127,
'AveBedrms': 1.023810,
'Population': 322.0,
'AveOccup': 2.555556,
'Latitude': 37.88,
'Longitude': -122.23
}
print(f"\nSample data: {sample_data}")
linear_pred, nn_pred = make_prediction(linear_model_data, nn_model_data, sample_data)
print(f"Linear Regression prediction: ${linear_pred * 100000:.2f}")
print(f"Neural Network prediction: ${nn_pred * 100000:.2f}")
predict_with_models()
return
@app.cell
def _(mo):
mo.md(r"""# Interpret a Linear Regressor""")
return
@app.cell
def _(mo):
mo.md(r"""## Load Model's Coefficients and Intercept""")
return
@app.cell
def _(mo):
mo.md(r"""As you saw earlier, linear regression models are naturally interpretable. You can directly inspect the coefficients and intercept to understand how the model makes predictions. Go ahead and load the trained model so you can extract and examine those values:""")
return
@app.cell
def _(joblib):
model_data = joblib.load('linear_regression_model.pkl')
model = model_data['model']
feature_names = model_data['feature_names']
# Get coefficients and intercept
coefficients = model.coef_
intercept = model.intercept_
print("Linear Regression Model Interpretation")
print("=" * 50)
print(f"Intercept: {intercept:.4f}")
print("\nFeature Coefficients:")
print("-" * 30)
print(coefficients)
return coefficients, feature_names
@app.cell
def _(mo):
mo.md(rf"""#### Question: In a linear regression model, what does the intercept represent?""")
return
@app.cell
def _(mo):
mo.md(
r"""
The intercept in a linear regression model represents the predicted output when all input features are zero.
$$
y = w_1 x_1 + w_2 x_2 + \cdots + w_n x_n + b
$$
where:
$$
w_1, w_2, \dots, w_n \text{ are the coefficients}
$$
$$
x_1, x_2, \dots, x_n \text{ are the features}
$$
$$
b \text{ is the intercept}
$$
So:
$$
\text{Intercept } b = y \quad \text{when all } x_i = 0
$$
Think of the intercept as the model’s baseline prediction — the value of \( y \) when there’s no influence from any feature.
It anchors the regression line or hyperplane in the output space.
"""
)
return
@app.cell
def _(mo):
mo.md(
r"""
## Examine the most Influential Features
To get a better sense of what the coefficients represent and which features they belong to, you can create a DataFrame that pairs each coefficient with its corresponding feature name. This helps you see which inputs have the strongest impact on the model’s predictions.
"""
)
return
@app.cell
def _(coefficients, feature_names, np, pd):
def interpret_model():
coef_df = pd.DataFrame({
'Feature': feature_names,
'Coefficient': coefficients,
'Abs_Coefficient': np.abs(coefficients)
}).sort_values('Abs_Coefficient', ascending=False)
return coef_df
return (interpret_model,)
@app.cell
def _(interpret_model):
coef_df = interpret_model()
coef_df
return (coef_df,)
@app.cell
def _(mo):
mo.md(r"""According to your model's coefs and intercept, the three most important features are:""")
return
@app.cell
def _(coef_df):
top_features = coef_df.head(3)
print("\nTop 3 Most Important Features:")
for _, row in top_features.iterrows():
effect = "increases" if row['Coefficient'] > 0 else "decreases"
print(f"• {row['Feature']}: {effect} price by {abs(row['Coefficient']):.4f} units")
return
@app.cell
def _(mo):
mo.md(
r"""
The coefficients and their signs tell you a lot about the model.
**Latitude has the strongest negative effect**: moving north (higher latitude) decreases price.
**Longitude is next**: moving east (higher longitude) also decreases price.
**MedInc (median income) strongly increases housing price**: richer areas tend to have more expensive homes.
"""
)
return
@app.cell
def _(mo):
mo.md(r"""🤔 Question: What effect does `AveRooms` have on your model?""")
return
@app.cell
def _(mo):
mo.md(r"""# Explain a Neural Network""")
return
@app.cell
def _(mo):
mo.md(r"""## Understand SHAP""")
return
@app.cell
def _(mo):
mo.md(
r"""
SHAP stands for SHapley Additive exPlanations.
Shapley values come from coalitional game theory. They were introduced by Lloyd Shapley in 1953 as a principled way to distribute total gains fairly among players who form coalitions. The core idea is this:
> Each player’s payout should reflect their average contribution across all possible orders in which players could join the team.
In the context of machine learning:
- Each feature is treated as a player.
- The model's output is the total “reward” to be distributed.
- SHAP assigns each feature a value that reflects how much it contributed to the prediction, across all possible subsets of features.
It’s a way to explain a model’s output that’s consistent, additive, and fair.
"""
)
return
@app.cell
def _(mo):
mo.md(r"""## Use SHAP""")
return
@app.cell
def _(joblib, load_data, np, pd):
import shap
def load_nn_model():
model_data = joblib.load('neural_network_model.pkl')
return model_data['model'], model_data['scaler'], model_data['feature_names']
def perform_nn_shap_analysis():
model, scaler, feature_names = load_nn_model()
data = load_data()
# Use only the features that were used during training (exclude target)
X = data[feature_names]
X_scaled = scaler.transform(X)
# Use a subset for SHAP analysis
X_sample = X_scaled[:1000]
# Create a wrapper function for the neural network
def nn_predict(X):
return model.predict(X)
# Use KernelExplainer for neural networks
explainer = shap.KernelExplainer(nn_predict, X_sample[:100])
shap_values = explainer.shap_values(X_sample[:100])
print("Neural Network SHAP Analysis Results:")
print("=" * 50)
# Calculate feature importance
feature_importance = np.abs(shap_values).mean(0)
importance_df = pd.DataFrame({
'Feature': feature_names,
'SHAP_Importance': feature_importance
}).sort_values('SHAP_Importance', ascending=False)
print("\nFeature Importance (SHAP):")
print(importance_df)
return shap_values, importance_df
perform_nn_shap_analysis()
return
if __name__ == "__main__":
app.run()