-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make OHE deterministic when top_n < no. of categories #1324
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0b0f869
Saving initial random state to make OHE reproducible.
freddyaboulton 97b1b9b
Adding PR 1324 to release notes.
freddyaboulton 340a5d3
Updating test_ohe_top_n_categories_always_the_same
freddyaboulton 9ae0bc5
Create a new random state with seeded with initial state.
freddyaboulton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,18 +224,17 @@ def test_more_top_n_unique_values(): | |
"col_4": [2, 0, 1, 3, 0, 1, 2]}) | ||
|
||
random_seed = 2 | ||
test_random_state = get_random_state(random_seed) | ||
|
||
encoder = OneHotEncoder(top_n=5, random_state=random_seed) | ||
encoder.fit(X) | ||
X_t = encoder.transform(X) | ||
col_1_counts = X["col_1"].value_counts(dropna=False).to_frame() | ||
col_1_counts = col_1_counts.sample(frac=1, random_state=test_random_state) | ||
col_1_counts = col_1_counts.sample(frac=1, random_state=random_seed) | ||
col_1_counts = col_1_counts.sort_values(["col_1"], ascending=False, kind='mergesort') | ||
col_1_samples = col_1_counts.head(encoder.parameters['top_n']).index.tolist() | ||
|
||
col_2_counts = X["col_2"].value_counts(dropna=False).to_frame() | ||
col_2_counts = col_2_counts.sample(frac=1, random_state=test_random_state) | ||
col_2_counts = col_2_counts.sample(frac=1, random_state=random_seed) | ||
col_2_counts = col_2_counts.sort_values(["col_2"], ascending=False, kind='mergesort') | ||
col_2_samples = col_2_counts.head(encoder.parameters['top_n']).index.tolist() | ||
|
||
|
@@ -428,3 +427,17 @@ def test_ohe_features_to_encode_no_col_names(): | |
col_names = set(X_t.columns) | ||
assert (col_names == expected_col_names) | ||
assert ([X_t[col].dtype == "uint8" for col in X_t]) | ||
|
||
|
||
def test_ohe_top_n_categories_always_the_same(): | ||
df = pd.DataFrame({"categories": ["cat_1"] * 5 + ["cat_2"] * 4 + ["cat_3"] * 3 + ["cat_4"] * 3 + ["cat_5"] * 3, | ||
"numbers": range(18)}) | ||
|
||
def check_df_equality(random_state): | ||
ohe = OneHotEncoder(top_n=4, random_state=random_state) | ||
df1 = ohe.fit_transform(df) | ||
df2 = ohe.fit_transform(df) | ||
pd.testing.assert_frame_equal(df1, df2) | ||
|
||
check_df_equality(5) | ||
check_df_equality(get_random_state(5)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test looks good! Have you verified this was failing before the fix was included? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Yes, this test fails on |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decided to define
new_random_state
here rather than at the top of the method because this is the only place it's needed. Happy to change it though!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fine to me!