Repository navigation
Cache class mapping in MultiLabelBinarizer() - #12116
Conversation
|
@TomDLT you mentioned previously that #11719 (comment) ( Could you please elaborate a bit? |
|
also @qinhanmin2014 mentioned that there is no need to set attributes in |
| if self.classes is None: | ||
| classes = sorted(set(itertools.chain.from_iterable(y))) | ||
| if self._classes_cache is None: | ||
| self._classes_cache = sorted(set(itertools.chain.from_iterable(y))) |
| class_to_index = dict(zip(self.classes_, range(len(self.classes_)))) | ||
| if self._indexed_classes_cache is None: | ||
| self._indexed_classes_cache = dict(zip(self.classes_, range(len(self.classes_)))) | ||
| class_to_index = self._indexed_classes_cache |
There was a problem hiding this comment.
No need for this local variable. Combine these two lines
|
|
||
| class_to_index = dict(zip(self.classes_, range(len(self.classes_)))) | ||
| if self._indexed_classes_cache is None: | ||
| self._indexed_classes_cache = dict(zip(self.classes_, range(len(self.classes_)))) |
There was a problem hiding this comment.
The cache needs to be invalidated if fit is called again. Why not just do this caching in fit?
There was a problem hiding this comment.
Ah.. so basically we should take the cached value only in case if self.classes_ is the same, ie if we've already calculated that dict(zip( thing before?
| self._prev_classes = self.classes_ | ||
| self._cached_dict = None | ||
|
|
||
| if (self._prev_classes.__repr__() != self.classes_.__repr__()) \ |
There was a problem hiding this comment.
This __repr__ comparison would miss subtle changes in the classes, wouldn't it?
I would suggest to just reset self._cached_dict at each fit and fit_transform.
There was a problem hiding this comment.
Can you add a test in sklearn/preprocessing/tests/test_label.py, to test that the reset is correct?
You would need to fit an estimator, call the transform, refit it with different classes, call a second time transform, and ensure that the result of the second transform is different from the first one.
Could you also time the performance gain introduced by this change? Just compare the computational time of transform on a few examples, before and after this change.
| return yt | ||
|
|
||
| def _cached_dict_for(self): | ||
| if (self._cached_dict is None): |
|
|
||
| def _cached_dict_for(self): | ||
| if (self._cached_dict is None): | ||
| self._cached_dict = dict(zip(self.classes_, \ |
There was a problem hiding this comment.
The \ is not necessary, since you have an open parenthesis.
You also need to align the arguments to comply with PEP8:
self._cached_dict = dict(zip(self.classes_,
range(len(self.classes_))))|
|
||
| return yt | ||
|
|
||
| def _cached_dict_for(self): |
There was a problem hiding this comment.
the method should be named as a verb, e.g. _build_cache
|
Please add the requested test that fitting multiple times with different classes gets the right results |
|
something like this? f387b8a |
| # first call | ||
| mlb = MultiLabelBinarizer(classes=[1, 3, 2]) | ||
| assert_array_equal(mlb.fit_transform(inp), indicator_mat) | ||
| #second call change class |
| [1, 0, 0], | ||
| [1, 0, 1]]) | ||
|
|
||
| inp2 = [(1, 2), (1,), (2, 3)] |
There was a problem hiding this comment.
This has the same class mapping, so it's not useful for the test.
| [0, 1, 1]]) | ||
|
|
||
| # first call | ||
| mlb = MultiLabelBinarizer(classes=[1, 3, 2]) |
There was a problem hiding this comment.
It would be good to test with and without specifying classes. You can reset classes just by setting mlb.classes =
|
not really sure already 😅 any closer? |
|
Please add an |
|
Thanks @kiote ! |
Fixes #11680
What does this implement/fix? Explain your changes.
I've implemented a simple caching strategy to save previously calculated values in private attributes.
Any other comments?
There was a suggestion in my previous PR to make benchmarks as well but I'd prefer to do it as the separate issue (not really familiar with benchmarking being used here)