Fit interpretable models and explain blackbox machine learning with comprehensive interpretability tools.
—
Interactive visualization system with multiple backends, preservation capabilities, and server management for dashboard applications.
Primary functions for displaying and managing explanations with interactive visualizations.
def show(explanation, name=None):
"""
Display interactive visualization for explanation.
Parameters:
explanation: Explanation object to visualize
name (str, optional): Custom name for display
"""
def preserve(explanation, file_path=None, name=None):
"""
Save explanation for later viewing.
Parameters:
explanation: Explanation object to preserve
file_path (str, optional): Path to save file
name (str, optional): Custom name for preservation
"""Functions for managing the visualization server for dashboard applications.
def init_show_server(addr=None, base_url=None, use_relative_links=False):
"""
Initialize visualization server.
Parameters:
addr (tuple, optional): (host, port) for server
base_url (str, optional): Base URL for server
use_relative_links (bool): Use relative links in output
"""
def shutdown_show_server():
"""Shutdown the visualization server."""
def status_show_server():
"""
Get status of visualization server.
Returns:
dict: Server status information
"""
def get_show_addr():
"""
Get current server address.
Returns:
tuple: (host, port) of current server
"""
def set_show_addr(addr):
"""
Set server address.
Parameters:
addr (tuple): (host, port) for server
"""
def show_link(explanation, name=None, title=None):
"""
Generate shareable link for explanation.
Parameters:
explanation: Explanation object
name (str, optional): Custom name
title (str, optional): Link title
Returns:
str: Shareable URL
"""Functions for configuring visualization backends and computation providers.
def get_visualize_provider():
"""
Get current visualization provider.
Returns:
VisualizeProvider: Current provider
"""
def set_visualize_provider(provider):
"""
Set visualization provider.
Parameters:
provider (str or VisualizeProvider): Provider to use
('auto', 'dash', 'inline', 'preserve')
"""from interpret.glassbox import ExplainableBoostingClassifier
from interpret import show, preserve
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
# Train model and get explanations
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.2, random_state=42
)
ebm = ExplainableBoostingClassifier(random_state=42)
ebm.fit(X_train, y_train)
global_exp = ebm.explain_global()
local_exp = ebm.explain_local(X_test[:5], y_test[:5])
# Show explanations
show(global_exp, name="Global Feature Importance")
show(local_exp, name="Local Explanations")
# Preserve for later viewing
preserve(global_exp, name="EBM Global")
preserve(local_exp, name="EBM Local")from interpret import init_show_server, status_show_server, shutdown_show_server
# Start server on specific port
init_show_server(addr=('localhost', 7001))
# Check server status
status = status_show_server()
print(f"Server running: {status}")
# Show explanations (will use server)
show(global_exp)
# Shutdown when done
shutdown_show_server()from interpret import set_visualize_provider, get_visualize_provider
# Check current provider
current_provider = get_visualize_provider()
print(f"Current provider: {current_provider}")
# Set to inline for Jupyter notebooks
set_visualize_provider('inline')
show(global_exp) # Will display inline
# Set to dash for web dashboard
set_visualize_provider('dash')
show(global_exp) # Will open in web browser
# Set to preserve mode (save without displaying)
set_visualize_provider('preserve')
show(global_exp) # Will save but not display
# Auto mode selects best provider for environment
set_visualize_provider('auto')from interpret import show_link, init_show_server
# Start server
init_show_server()
# Generate shareable links
global_link = show_link(global_exp, name="Global", title="EBM Global Explanation")
local_link = show_link(local_exp, name="Local", title="EBM Local Explanations")
print(f"Global explanation: {global_link}")
print(f"Local explanations: {local_link}")
# Links can be shared with others who have access to the serverfrom interpret import init_show_server, show
from interpret.glassbox import ExplainableBoostingClassifier
from interpret.blackbox import LimeTabular, PartialDependence
# Initialize dashboard server
init_show_server(addr=('0.0.0.0', 7001), base_url='/dashboard')
# Train multiple models and create explanations
models = {
'EBM': ExplainableBoostingClassifier(random_state=42),
'RF': RandomForestClassifier(random_state=42)
}
explanations = {}
for name, model in models.items():
model.fit(X_train, y_train)
if hasattr(model, 'explain_global'):
explanations[f"{name}_global"] = model.explain_global()
explanations[f"{name}_local"] = model.explain_local(X_test[:5], y_test[:5])
else:
# Use LIME for blackbox models
lime = LimeTabular(predict_fn=model.predict_proba, data=X_train)
explanations[f"{name}_lime"] = lime.explain_local(X_test[:5], y_test[:5])
# Display all explanations in dashboard
for name, exp in explanations.items():
show(exp, name=name)
print("Dashboard available at http://localhost:7001/dashboard")Install with Tessl CLI
npx tessl i tessl/pypi-interpret