Skip to content
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

Improvements to OrdinalEncoder, OneHotEncoder, NaiveBayes, LogisticRegression #293

Merged
merged 7 commits into from
Aug 1, 2024

Conversation

krstopro
Copy link
Member

@krstopro krstopro commented Jul 31, 2024

Changelog:

  • Simplified OrdinalEncoder struct; it now contains only :categories field.
  • Fixed a bug inside OrdinalEncoder.fit/2 and now it works with tensors of size 1.
  • Removed opts as 2nd argument from OrdinalEncoder.fit_transform as it is not needed.
  • Simplified OneHotEncoder struct; it now contains only :ordinal_encoder field.
  • Renamed :num_classes option to :num_categories as these encoders can be used to encode features, not only target variables.
  • Added argument validation.
  • Removed ordinal encoding from NaiveBayes.Complement and LogisticRegression.

Fixes #290.

Copy link
Contributor

@josevalim josevalim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Perhaps we just need to remove the commented out code. :)

@krstopro
Copy link
Member Author

krstopro commented Aug 1, 2024

LGTM! Perhaps we just need to remove the commented out code. :)

Yeah, agreed. :)

I would honestly replace the following line https://github.com/elixir-nx/scholar/blob/main/lib/scholar/preprocessing.ex#L179
with

tensor
|> Nx.new_axis(1)
|> Nx.broadcast({num_samples, num_classes})
|> Nx.equal(Nx.iota({num_samples, num_categories}, axis: 1))

i.e. remove the ordinal encoding that is performed as part of OneHotEncoder and assume tensor contains values between 0 and num_categories - 1. These few lines are used over and over again in Scholar (e.g. NaiveBayes, LogisticRegression, etc.).

@josevalim
Copy link
Contributor

I would honestly replace the following line https://github.com/elixir-nx/scholar/blob/main/lib/scholar/preprocessing.ex#L179

I think the preprocessing function should be the same as the module that we invoke (we may remove the functions altogether). But I agree we should probably remove the ordinal encoding from one hot encoding preprocessor module.

@krstopro
Copy link
Member Author

krstopro commented Aug 1, 2024

I would honestly replace the following line https://github.com/elixir-nx/scholar/blob/main/lib/scholar/preprocessing.ex#L179

I think the preprocessing function should be the same as the module that we invoke (we may remove the functions altogether). But I agree we should probably remove the ordinal encoding from one hot encoding preprocessor module.

Very well, we can have a separate pull request for that.

@josevalim
Copy link
Contributor

Definitely, so it all looks good to me!

@krstopro krstopro merged commit 7050d32 into elixir-nx:main Aug 1, 2024
0 of 2 checks passed
@krstopro krstopro deleted the one-hot-encoder-fix branch August 1, 2024 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

one_hot_encode errors with tensor of size 1
2 participants