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

Add BernoulliNB and Binarizer #306

Merged
merged 7 commits into from
Nov 27, 2024
Merged

Conversation

srzeszut
Copy link
Contributor

I am adding BernoulliNB and tests. I needed a binarizer in it so I added it with tests to preprocessing.

x_binarize =
if opts[:binarize] != nil,
do: Scholar.Preprocessing.Binarizer.fit_transform(x, threshold: opts[:binarize]),
else: x
Copy link
Contributor

Choose a reason for hiding this comment

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

Try to move as much code as possible inside fit_n. For example, could this be moved there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

after change error appeared on this line:
if opts[:binarize] != nil,
and i don't know how to fix it
(Protocol.UndefinedError) protocol Nx.LazyContainer not implemented for nil of type Atom.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe opts[:binarize] != nil inside defn will want to treat those as tensors. Perhaps this may work:

case opts[:binarize] do
  nil -> x
  binarize ->Scholar.Preprocessing.Binarizer.fit_transform(x, threshold: binarize)
end

If it doesn't work we can leave it as is :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it worked, Thanks!

@krstopro
Copy link
Member

I would suggest removing the binarizer from fit and assuming that the features are already binarized. You should mention this in the docs.

I will performed a more detailed review somewhere this week.

Comment on lines 358 to 360
|> Nx.new_axis(1)
|> Nx.broadcast(jll)

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the explicit broadcast necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it


defnp binarize_n(tensor, opts) do
threshold = opts[:threshold]
Nx.select(Nx.greater(tensor, threshold), 1, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Nx.select(Nx.greater(tensor, threshold), 1, 0)
tensor > threshold

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change it

@srzeszut
Copy link
Contributor Author

I would suggest removing the binarizer from fit and assuming that the features are already binarized. You should mention this in the docs.

I will performed a more detailed review somewhere this week.

I can change it and report but I can't check what values were inserted so it will give some random numbers for not correct input instead of an error. Please let me know If you still want to remove this from fit.

@josevalim josevalim merged commit 4de37f1 into elixir-nx:main Nov 27, 2024
2 checks passed
@josevalim
Copy link
Contributor

💚 💙 💜 💛 ❤️

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.

5 participants