0
# Visualization and Interaction
1
2
Interactive visualization system with multiple backends, preservation capabilities, and server management for dashboard applications.
3
4
## Capabilities
5
6
### Core Visualization Functions
7
8
Primary functions for displaying and managing explanations with interactive visualizations.
9
10
```python { .api }
11
def show(explanation, name=None):
12
"""
13
Display interactive visualization for explanation.
14
15
Parameters:
16
explanation: Explanation object to visualize
17
name (str, optional): Custom name for display
18
"""
19
20
def preserve(explanation, file_path=None, name=None):
21
"""
22
Save explanation for later viewing.
23
24
Parameters:
25
explanation: Explanation object to preserve
26
file_path (str, optional): Path to save file
27
name (str, optional): Custom name for preservation
28
"""
29
```
30
31
### Server Management
32
33
Functions for managing the visualization server for dashboard applications.
34
35
```python { .api }
36
def init_show_server(addr=None, base_url=None, use_relative_links=False):
37
"""
38
Initialize visualization server.
39
40
Parameters:
41
addr (tuple, optional): (host, port) for server
42
base_url (str, optional): Base URL for server
43
use_relative_links (bool): Use relative links in output
44
"""
45
46
def shutdown_show_server():
47
"""Shutdown the visualization server."""
48
49
def status_show_server():
50
"""
51
Get status of visualization server.
52
53
Returns:
54
dict: Server status information
55
"""
56
57
def get_show_addr():
58
"""
59
Get current server address.
60
61
Returns:
62
tuple: (host, port) of current server
63
"""
64
65
def set_show_addr(addr):
66
"""
67
Set server address.
68
69
Parameters:
70
addr (tuple): (host, port) for server
71
"""
72
73
def show_link(explanation, name=None, title=None):
74
"""
75
Generate shareable link for explanation.
76
77
Parameters:
78
explanation: Explanation object
79
name (str, optional): Custom name
80
title (str, optional): Link title
81
82
Returns:
83
str: Shareable URL
84
"""
85
```
86
87
### Provider Management
88
89
Functions for configuring visualization backends and computation providers.
90
91
```python { .api }
92
def get_visualize_provider():
93
"""
94
Get current visualization provider.
95
96
Returns:
97
VisualizeProvider: Current provider
98
"""
99
100
def set_visualize_provider(provider):
101
"""
102
Set visualization provider.
103
104
Parameters:
105
provider (str or VisualizeProvider): Provider to use
106
('auto', 'dash', 'inline', 'preserve')
107
"""
108
```
109
110
## Usage Examples
111
112
### Basic Visualization
113
114
```python
115
from interpret.glassbox import ExplainableBoostingClassifier
116
from interpret import show, preserve
117
from sklearn.datasets import load_breast_cancer
118
from sklearn.model_selection import train_test_split
119
120
# Train model and get explanations
121
data = load_breast_cancer()
122
X_train, X_test, y_train, y_test = train_test_split(
123
data.data, data.target, test_size=0.2, random_state=42
124
)
125
126
ebm = ExplainableBoostingClassifier(random_state=42)
127
ebm.fit(X_train, y_train)
128
129
global_exp = ebm.explain_global()
130
local_exp = ebm.explain_local(X_test[:5], y_test[:5])
131
132
# Show explanations
133
show(global_exp, name="Global Feature Importance")
134
show(local_exp, name="Local Explanations")
135
136
# Preserve for later viewing
137
preserve(global_exp, name="EBM Global")
138
preserve(local_exp, name="EBM Local")
139
```
140
141
### Server Management
142
143
```python
144
from interpret import init_show_server, status_show_server, shutdown_show_server
145
146
# Start server on specific port
147
init_show_server(addr=('localhost', 7001))
148
149
# Check server status
150
status = status_show_server()
151
print(f"Server running: {status}")
152
153
# Show explanations (will use server)
154
show(global_exp)
155
156
# Shutdown when done
157
shutdown_show_server()
158
```
159
160
### Provider Configuration
161
162
```python
163
from interpret import set_visualize_provider, get_visualize_provider
164
165
# Check current provider
166
current_provider = get_visualize_provider()
167
print(f"Current provider: {current_provider}")
168
169
# Set to inline for Jupyter notebooks
170
set_visualize_provider('inline')
171
show(global_exp) # Will display inline
172
173
# Set to dash for web dashboard
174
set_visualize_provider('dash')
175
show(global_exp) # Will open in web browser
176
177
# Set to preserve mode (save without displaying)
178
set_visualize_provider('preserve')
179
show(global_exp) # Will save but not display
180
181
# Auto mode selects best provider for environment
182
set_visualize_provider('auto')
183
```
184
185
### Shareable Links
186
187
```python
188
from interpret import show_link, init_show_server
189
190
# Start server
191
init_show_server()
192
193
# Generate shareable links
194
global_link = show_link(global_exp, name="Global", title="EBM Global Explanation")
195
local_link = show_link(local_exp, name="Local", title="EBM Local Explanations")
196
197
print(f"Global explanation: {global_link}")
198
print(f"Local explanations: {local_link}")
199
200
# Links can be shared with others who have access to the server
201
```
202
203
### Dashboard Integration
204
205
```python
206
from interpret import init_show_server, show
207
from interpret.glassbox import ExplainableBoostingClassifier
208
from interpret.blackbox import LimeTabular, PartialDependence
209
210
# Initialize dashboard server
211
init_show_server(addr=('0.0.0.0', 7001), base_url='/dashboard')
212
213
# Train multiple models and create explanations
214
models = {
215
'EBM': ExplainableBoostingClassifier(random_state=42),
216
'RF': RandomForestClassifier(random_state=42)
217
}
218
219
explanations = {}
220
for name, model in models.items():
221
model.fit(X_train, y_train)
222
223
if hasattr(model, 'explain_global'):
224
explanations[f"{name}_global"] = model.explain_global()
225
explanations[f"{name}_local"] = model.explain_local(X_test[:5], y_test[:5])
226
else:
227
# Use LIME for blackbox models
228
lime = LimeTabular(predict_fn=model.predict_proba, data=X_train)
229
explanations[f"{name}_lime"] = lime.explain_local(X_test[:5], y_test[:5])
230
231
# Display all explanations in dashboard
232
for name, exp in explanations.items():
233
show(exp, name=name)
234
235
print("Dashboard available at http://localhost:7001/dashboard")
236
```