-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #680 from dianna-ai/visualization_tabular
662 Visualization module tabular
- Loading branch information
Showing
6 changed files
with
96 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# flake8: noqa: F401 | ||
"""Tools for visualization of model explanations.""" | ||
from .image import plot_image | ||
from .tabular import plot_tabular | ||
from .text import highlight_text | ||
from .timeseries import plot_timeseries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Visualization module for tabular data.""" | ||
from typing import List | ||
from typing import Optional | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
|
||
def plot_tabular( | ||
x: np.ndarray, | ||
y: List[str], | ||
x_label: str = "Importance score", | ||
y_label: str = "Features", | ||
num_features: Optional[int] = None, | ||
show_plot: Optional[bool] = True, | ||
output_filename: Optional[str] = None, | ||
) -> plt.Figure: | ||
"""Plot feature importance with segments highlighted. | ||
Args: | ||
x (np.ndarray): Array of feature importance scores | ||
y (List[str]): List of feature names | ||
x_label (str): Label for the x-axis | ||
y_label (str): Label or list of labels for the y-axis | ||
num_features (Optional[int]): Number of most salient features to display | ||
show_plot (bool, optional): Shows plot if true (for testing or writing | ||
plots to disk instead). | ||
output_filename (str, optional): Name of the file to save | ||
the plot to (optional). | ||
Returns: | ||
plt.Figure | ||
""" | ||
if not num_features: | ||
num_features = len(x) | ||
fig, ax = plt.subplots() | ||
abs_values = [abs(i) for i in x] | ||
top_values = [x for _, x in sorted(zip(abs_values, x), reverse=True)][:num_features] | ||
top_features = [x for _, x in sorted(zip(abs_values, y), reverse=True)][ | ||
:num_features | ||
] | ||
|
||
colors = ["r" if x >= 0 else "b" for x in top_values] | ||
ax.barh(top_features, top_values, color=colors) | ||
ax.set_xlabel(x_label) | ||
ax.set_ylabel(y_label) | ||
|
||
if show_plot: | ||
plt.show() | ||
if output_filename: | ||
plt.savefig(output_filename) | ||
|
||
return fig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.