Skip to content

Commit

Permalink
Merge pull request #563 from guillaume-vignal/feature/interaction_plo…
Browse files Browse the repository at this point in the history
…t_improvment

Feature/interaction plot improvment
  • Loading branch information
guillaume-vignal authored Jul 4, 2024
2 parents 14a9bc8 + 5fdca91 commit 12c4f3e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
29 changes: 10 additions & 19 deletions shapash/explainer/smart_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2150,15 +2150,6 @@ def _plot_interactions_scatter(self, x_name, y_name, col_name, x_values, y_value
-------
go.Figure
"""
# add break line to X label if necessary
max_len_by_row = max([round(50 / self.explainer.features_desc[x_values.columns.values[0]]), 8])
x_values.iloc[:, 0] = x_values.iloc[:, 0].apply(
add_line_break,
args=(
max_len_by_row,
120,
),
)

data_df = pd.DataFrame(
{
Expand Down Expand Up @@ -2209,16 +2200,6 @@ def _plot_interactions_violin(self, x_name, y_name, col_name, x_values, y_values

fig = go.Figure()

# add break line to X label
max_len_by_row = max([round(50 / self.explainer.features_desc[x_values.columns.values[0]]), 8])
x_values.iloc[:, 0] = x_values.iloc[:, 0].apply(
add_line_break,
args=(
max_len_by_row,
120,
),
)

uniq_l = list(pd.unique(x_values.values.flatten()))
uniq_l.sort()

Expand Down Expand Up @@ -2444,6 +2425,16 @@ def interactions_plot(
if col_id1 != col_id2:
interaction_values = interaction_values * 2

# add break line to X label if necessary
max_len_by_row = max([round(50 / self.explainer.features_desc[feature_values1.columns.values[0]]), 8])
feature_values1.iloc[:, 0] = feature_values1.iloc[:, 0].apply(
add_line_break,
args=(
max_len_by_row,
120,
),
)

# selecting the best plot : Scatter, Violin?
if col_value_count1 > violin_maxf:
fig = self._plot_interactions_scatter(
Expand Down
13 changes: 8 additions & 5 deletions shapash/report/project_report.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.metadata
import logging
import os
import sys
Expand Down Expand Up @@ -219,11 +220,13 @@ def display_model_analysis(self):
print_md(f"**Library :** {self.explainer.model.__class__.__module__}")

for _, module in sorted(sys.modules.items()):
if (
hasattr(module, "__version__")
and self.explainer.model.__class__.__module__.split(".")[0] == module.__name__
):
print_md(f"**Library version :** {module.__version__}")
module_name = module.__name__.split(".")[0]
if self.explainer.model.__class__.__module__.split(".")[0] == module_name:
try:
version = importlib.metadata.version(module_name)
print_md(f"**Library version :** {version}")
except importlib.metadata.PackageNotFoundError:
print_md(f"**Library version :** not found for {module_name}")

print_md("**Model parameters :** ")
model_params = self.explainer.model.__dict__
Expand Down
9 changes: 7 additions & 2 deletions shapash/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,16 @@ def compute_digit_number(value):
int
number of digits
"""
if isinstance(value, np.ndarray):
scalar_value = value.item()
else:
scalar_value = value

# fix for 0 value
if value == 0:
if scalar_value == 0:
first_nz = 1
else:
first_nz = int(math.log10(abs(value)))
first_nz = int(math.log10(abs(scalar_value)))
digit = abs(min(3, first_nz) - 3)
return digit

Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/explainer/test_smart_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,9 @@ def test_plot_line_comparison_1(self):
name=f"Id: <b>{index[i]}</b>",
hovertext=[
f"Id: <b>{index[i]}</b><br /><b>X1</b> <br />Contribution: {contributions[0][i]:.4f} <br />"
+ f"Value: {data.iloc[i][0]}",
+ f"Value: {data.iloc[i,0]}",
f"Id: <b>{index[i]}</b><br /><b>X2</b> <br />Contribution: {contributions[1][i]:.4f} <br />"
+ f"Value: {data.iloc[i][1]}",
+ f"Value: {data.iloc[i,1]}",
],
marker={"color": colors[i]},
)
Expand Down

0 comments on commit 12c4f3e

Please sign in to comment.