Repository navigation
[32161] Added frequency option to ordinaryEncoder - #33702
vulkomilev wants to merge 3 commits into
Conversation
❌ Linting issuesThis PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling You can see the details of the linting issues under the
|
| cats = result | ||
| elif self.categories == "frequency": | ||
| for element in Xi: | ||
| if hash(element) not in list(X_list_dict.keys()): |
There was a problem hiding this comment.
The hash is unnecessary imo, but maybe open to being convinced if there's an example you're thinking of that requires that.
| for element in Xi: | ||
| if hash(element) not in list(X_list_dict.keys()): | ||
| X_list_dict[hash(element)] = [] | ||
| X_list_dict[hash(element)].append(element) |
There was a problem hiding this comment.
This is maintaining a dictionary of lists? Why do you need the list? Wouldn't this be:
input: 'x', 'x', 'x', 'y'
X_list_dict = {
hash(x): [x, x, x],
hash(y): [y]
}
?
| with pytest.raises(TypeError, match=err_msg): | ||
| enc.get_feature_names_out() | ||
|
|
||
| def test_frequency_same(): |
There was a problem hiding this comment.
Name should be more explicit. Perhaps
test_ordinal_encoding_when_frequency_is_same_
There was a problem hiding this comment.
A concise descriptive docstring for the unt-tests are also nice to have.
|
|
||
| assert_array_equal(X_trans_inv, inv_exp) | ||
|
|
||
| def test_diferent_order(): |
| #encoder dont encode in any particular order . it has auto of alphabetical OrdinalEncoder | ||
| #after this this encoding is fed to the fit functions | ||
| #we have to test if the fit is behaiving normally with the new encoding which will be ordered. | ||
| def test_ordinal_encoder_frequency(): |
| @@ -170,7 +178,7 @@ | |||
| category_counts.append(_get_counts(Xi, cats)) | |||
|
|
|||
| self.categories_.append(cats) | |||
| @@ -182,7 +190,6 @@ | |||
| # `nan` values can only be placed in the latest position | |||
| missing_indices[feature_idx] = categories_for_idx.size - 1 | |||
| output["missing_indices"] = missing_indices | |||
| category_counts = [] | ||
| compute_counts = return_counts or self._infrequent_enabled | ||
|
|
||
| X_list_counter = {} |
There was a problem hiding this comment.
There was a problem hiding this comment.
There was a problem hiding this comment.
Although I'm not sure if the _unique maintains ordinal ordering, which is the goal here.
|
|
||
| self.categories_ = [] | ||
| category_counts = [] | ||
| X_list_dict = {} |
| # I dont think that this can be placed anywhere else | ||
| # because it requires X_list | ||
| for i in range(n_features): | ||
| result = _unique(X_list[i], return_counts=compute_counts) |
There was a problem hiding this comment.
for i in range(n_features):
unique_vals, counts = _unique(X_list[i], return_counts=True)
# construct a two-way dictionary mapping unique values to their ordinal encoding and ordincal encoding to their encoded values
# for example:
# - we have {'A': 10, 'B': 10, 'C': 11} are the unique values and their corresponding counts
# we want to map this as follows:
# {'C': 0, 'A': 1, 'B': 2}
# first apply argsort
count_sort_idx = np.argsort(counts)
sorted_unique_vals = unique_vals[count_sort_idx]
# second construct two-way dictionary mapping
encoder_mapping = dict()
decoder_mapping = dict()
for idx, val in enumerate(sorted_unique_vals):
encoder_mapping[val] = idx
decoder_mapping[idx] = val
# extra stuff to make it work within the scikit-learn design
self.categories_ = set(encoder_mapping.keys())
|
|
||
|
|
||
| def test_ordinal_encoder_tie(): | ||
|
|
There was a problem hiding this comment.
It would be useful to add a sentence in the docstring of each unit-test on what exactly is the behavior you're testing
| X_trans_inv = enc.inverse_transform(X_trans_enc) | ||
| assert_array_equal(X_trans_inv, X_trans) | ||
|
|
||
| def test_ordinal_encoder_tie_inv_fit(): |
There was a problem hiding this comment.
Same comment as above. What's the behavior here that we're trying to test?
| X_trans_inv = enc.inverse_transform(X_trans_enc) | ||
| assert_array_equal(X_trans_inv, X_trans) | ||
|
|
||
| def test_ordinal_encoder_none(): |
There was a problem hiding this comment.
I don't understand this test. What is "ordinal_encoder_none" referring to?
| assert_array_equal(X_trans_enc, exp) | ||
|
|
||
|
|
||
| def test_ordinal_encoder_multy_letters(): |
There was a problem hiding this comment.
Unnecessary test imo. Multiple letters vs characters are both strings.
Reference Issues/PRs
closes #32161
What does this implement/fix? Explain your changes.
I have added an option for encoding of the inputs based on their frequency instead of their alphabetical order
AI usage disclosure
None
Any other comments?
No