alibi icon indicating copy to clipboard operation
alibi copied to clipboard

ALE Plot dimension mismatch

Open pyaf opened this issue 4 years ago • 2 comments

I'm trying to run plot_ale like this:

proba_ale = ale_analysis(model, train_dataframe, features_list)
plot_ale(proba_ale, n_cols=2, fig_kw={'figwidth': 12, 'figheight': 15}, sharey=None);

Here, model is my trained model, train_dataframe is the training set dataframe, features_list is the list of features for which I want to do the ALE analysis.

I'm getting this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-84-19ac825bda1b> in <module>
----> 1 plot_ale(proba_ale, n_cols=2, fig_kw={'figwidth': 12, 'figheight': 15}, sharey=None);

~/miniconda3/envs/tv-pa/lib/python3.7/site-packages/alibi/explainers/ale.py in plot_ale(exp, features, targets, n_cols, sharey, constant, ax, line_kw, fig_kw)
    469                               ax=ax_ravel,
    470                               legend=not ix,  # only one legend
--> 471                               line_kw=line_kw)
    472 
    473     # if explicit labels passed, handle the legend here as the axis passed might be repeated

~/miniconda3/envs/tv-pa/lib/python3.7/site-packages/alibi/explainers/ale.py in _plot_one_ale_num(exp, feature, targets, constant, ax, legend, line_kw)
    503         exp.feature_values[feature],
    504         exp.ale_values[feature][:, targets] + constant * exp.constant_value,
--> 505         **line_kw
    506     )
    507 

~/miniconda3/envs/tv-pa/lib/python3.7/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1741         """
   1742         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1744         for line in lines:
   1745             self.add_line(line)

~/miniconda3/envs/tv-pa/lib/python3.7/site-packages/matplotlib/axes/_base.py in __call__(self, data, *args, **kwargs)
    271                 this += args[0],
    272                 args = args[1:]
--> 273             yield from self._plot_args(this, kwargs)
    274 
    275     def get_next_color(self):

~/miniconda3/envs/tv-pa/lib/python3.7/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    397 
    398         if x.shape[0] != y.shape[0]:
--> 399             raise ValueError(f"x and y must have same first dimension, but "
    400                              f"have shapes {x.shape} and {y.shape}")
    401         if x.ndim > 2 or y.ndim > 2:

ValueError: x and y must have same first dimension, but have shapes (6,) and (5, 2)

A little debugging tells me that this line breaks. The number of values in a particular features column is 6, and the number of corresponding ALE values generated for that feature after ALE analysis is 5 (per class, 2 here).

This snippet confirms this:

for i in range(len(proba_ale.__dict__['ale_values'])):
    print(proba_ale.__dict__['feature_values'][i].shape, proba_ale.__dict__['ale_values'][i].shape)

Output:

(27,) (27, 2)
(10,) (10, 2)
(4,) (4, 2)
(7,) (7, 2)
(6,) (5, 2)
(8,) (8, 2)
(3,) (3, 2)

So, changing the length of feature_values arrays before plotting them solves the issue. But I think this is a bug and should be fiixed.

The code snippet below solves the problem.

for i in range(len(proba_ale.__dict__['ale_values'])):
    proba_ale.__dict__['feature_values'][i] = proba_ale.__dict__['feature_values'][i][:len(proba_ale.__dict__['ale_values'][i])]

pyaf avatar Dec 31 '20 06:12 pyaf

@jklaise I'd love to fix this bug, let me know if you'd like me to do that.

pyaf avatar Jan 07 '21 08:01 pyaf

@pyaf we should find out the root cause why sometimes there's a mismatch between the number of feature values and ALE values, it's likely some edge case that's not considered in the implementation. I haven't had a chance to poke around yet, but if you have any ideas what's causing it then feel free to share!

jklaise avatar Jan 07 '21 09:01 jklaise

@jklaise I encountered the dimension mismatch error when using alibi 0.6.2.

After updating to alibi 0.8.0, the problem no longer occurs.

I think this issue can be closed with the recommendation to use 0.8.0 to solve this issue.

jimthompson5802 avatar Nov 12 '22 12:11 jimthompson5802

Hey guys, good to know that this issue is resolved now. Closing the issue.

pyaf avatar Dec 22 '22 09:12 pyaf