optimizing naive_bayes_classifier #1223
Merged
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.
Describe your changes
This is to help optimize the
naive_bayes_classifier
a bit. I think it is a little more readable as well.The piece of this function that is taking the most time is the calculation of joint probabilities, so i focused on changes in that space. After several iterations, I settled on the approach here where we're grabbing the indices for the PDFs separately from the loops. We are now doing this once per function call instead of for every class and this allows us to use numpy index slicing and array multiplication.
I did all the testing on this using the Naive Bayes Tutorial notebook in the plantcv-binder repo.
After getting all of the changes in, I ran the original code 30x and the new code 30x to get runtimes in milliseconds. The screenshot is pulled from that notebook where we see a speedup of about 4x.
This example contains 4 classes that we're processing here, so I imagine this optimization hangs on number of classes.
Type of update
feature enhancement
Associated issues
#1158
For the reviewer
See this page for instructions on how to review the pull request.
plantcv/mkdocs.yml
updating.md