Repository navigation
[data/preprocessors] feat: allow hasher to run on append mode - #50632
Conversation
There was a problem hiding this comment.
@richardliaw wondering what your thoughts are on these changes. The FeatureHasher already create new columns, but it drops the original columns from the result dataset.
How can we enable/disable the dropping of the columns?
In these changes I opted for having the output_columns, which should name the transformed columns (instead of naming them hash_{i}) and if the output columns are provided, it will also make it so that the hasher doesn't drop the original columns.
Not super happy with this, there are a few other options we could take, like having an append, that controls whether to drop the original or not, but that won't allow us to name the output columns differently (as we do on other preprocessors)
There was a problem hiding this comment.
Hmm, I think it makes sense to only have a single output_column here, and disable the dropping of columns altogether?
There was a problem hiding this comment.
@richardliaw not sure I follow. Right now the hasher preprocessor outputs num_features new columns. Are you saying that you want to change it so it always returns a single column with a a list of num_features elements?
There was a problem hiding this comment.
yes, this would save us from headache in situations with wider tables
23ff71d to
ec6f311
Compare
97c6a27 to
76b1834
Compare
879239d to
63f94ab
Compare
Head branch was pushed to by a user without write access
63f94ab to
f0fa89c
Compare
… mode (#50847) <!-- Thank you for your contribution! Please review https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? This is part of #48133. Continuing the approach taken in #49426, make all the vectorizers work in append mode. Took similar approach to #50632 where the vectorizers also change to output a single column instead of one column per token or per num_feature ## Related issue number #49426 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Martin Bomio <[email protected]>
f0fa89c to
a20798c
Compare
Signed-off-by: Martin Bomio <[email protected]>
a20798c to
89515e0
Compare
… mode (ray-project#50847) <!-- Thank you for your contribution! Please review https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? This is part of ray-project#48133. Continuing the approach taken in ray-project#49426, make all the vectorizers work in append mode. Took similar approach to ray-project#50632 where the vectorizers also change to output a single column instead of one column per token or per num_feature ## Related issue number ray-project#49426 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Martin Bomio <[email protected]> Signed-off-by: kaihsun <[email protected]>
…oject#50632) <!-- Thank you for your contribution! Please review https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? This is part of ray-project#48133. Continuing the approach taken in ray-project#49426, make all the hashers work in append mode ## Related issue number ray-project#49426 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [ ] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Martin Bomio <[email protected]> Signed-off-by: kaihsun <[email protected]>
… mode (ray-project#50847) <!-- Thank you for your contribution! Please review https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? This is part of ray-project#48133. Continuing the approach taken in ray-project#49426, make all the vectorizers work in append mode. Took similar approach to ray-project#50632 where the vectorizers also change to output a single column instead of one column per token or per num_feature ## Related issue number ray-project#49426 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Martin Bomio <[email protected]>
…oject#50632) <!-- Thank you for your contribution! Please review https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? This is part of ray-project#48133. Continuing the approach taken in ray-project#49426, make all the hashers work in append mode ## Related issue number ray-project#49426 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [ ] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Martin Bomio <[email protected]>
… mode (ray-project#50847) <!-- Thank you for your contribution! Please review https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? This is part of ray-project#48133. Continuing the approach taken in ray-project#49426, make all the vectorizers work in append mode. Took similar approach to ray-project#50632 where the vectorizers also change to output a single column instead of one column per token or per num_feature ## Related issue number ray-project#49426 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Martin Bomio <[email protected]>
…oject#50632) <!-- Thank you for your contribution! Please review https://github.com/ray-project/ray/blob/master/CONTRIBUTING.rst before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? This is part of ray-project#48133. Continuing the approach taken in ray-project#49426, make all the hashers work in append mode ## Related issue number ray-project#49426 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [ ] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [x] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests - [ ] Release tests - [ ] This PR is not tested :( Signed-off-by: Martin Bomio <[email protected]>
… Concatenator, and PowerTransformer (#67483) ## Why are these changes needed? This PR fixes silent `NaN` corruption on non-default `pd.DataFrame` indices in `Concatenator` and `FeatureHasher`, and exceptions on zero-row batches in `FeatureHasher` and `PowerTransformer`: 1. **Preserve `df.index` in `Concatenator._transform_pandas` and `FeatureHasher._transform_pandas` (`TestConcatenator.test_non_default_index`, `test_feature_hasher`):** Both preprocessors previously assigned `df.loc[:, output_col] = pd.Series(list(concatenated))` without `index=df.index`. When `transform_batch` is called on a sliced, filtered, or custom-indexed `pd.DataFrame` whose index is not `RangeIndex(0, len(df))` (e.g., `index=[10, 20]`), pandas aligns the newly created `Series` index (`0..n-1`) against `df.index` and silently replaces non-overlapping index positions with `NaN`. Passing `index=df.index` preserves row alignment. 2. **Handle zero-row batches and hoist column-name hashing in `FeatureHasher._transform_pandas` (`test_feature_hasher`):** - On an empty `pd.DataFrame` (`len(df) == 0`), `df.loc[:, self._columns].apply(row_feature_hasher, axis=1, result_type="expand")` returns a 0-row DataFrame with `self._columns` rather than `hash_0 .. hash_{num_features - 1}`, causing `feature_columns[hash_columns]` to raise `KeyError`. Returning an empty `object`-dtype `Series` indexed by `df.index` when `df.empty` avoids the crash. - Precomputes `simple_hash(column, self._num_features)` once per column per batch rather than recomputing the SHA-256 column-name hash inside `row_feature_hasher` on every row. 3. **Return `pd.Series(result, index=s.index)` in `PowerTransformer._transform_pandas` (`yeo-johnson`, `test_power_transformer_empty_batch`):** Unlike the `box-cox` branch (which returns a `pd.Series`), the `yeo-johnson` branch returned a raw `np.ndarray`. On an empty DataFrame (`len(df) == 0`), `pandas.DataFrame.transform` treats a non-`Series` return value in `apply_empty_result` as a scalar reduction and raises `ValueError: Transform function failed`. Returning `pd.Series(result, index=s.index)` makes `yeo-johnson` consistent with `box-cox` on empty batches. ### Related work & acknowledgements - Builds on the preprocessor null-propagation and batch-handling hardening in #65272 by @AarryaSaraf and `FeatureHasher` append-mode support in #50632 by @martinbomio. ## Related issue number N/A ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API reference. For example, if I added a method in Python, I've added it to `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [x] Unit tests (`python/ray/data/tests/preprocessors/test_concatenator.py`, `python/ray/data/tests/preprocessors/test_hasher.py`, `python/ray/data/tests/preprocessors/test_transformer.py`) - [ ] Release tests - [ ] This PR is not tested :( --------- Signed-off-by: Parth Vadera <[email protected]> Co-authored-by: Copilot <[email protected]>
Why are these changes needed?
This is part of #48133. Continuing the approach taken in #49426, make all the hashers work in append mode
Related issue number
#49426
Checks
git commit -s) in this PR.scripts/format.shto lint the changes in this PR.method in Tune, I've added it in
doc/source/tune/api/under thecorresponding
.rstfile.