Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix min_pos when all negative + speed up #19328

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

Merged
merged 6 commits into from
Feb 3, 2021
Merged

Conversation

jeremiedbb
Copy link
Member

When all input elements are <= 0, min_pos returns the max float (resp. double) if input is float (resp.double). However, the max values are switched and it's actually returning the max double for float input and the max float for double input. Unlikely that it already cause an error in practice but that would have been a sneaky bug :)
I added a test that fails on main. I also added a generic test since the function was not tested at all.

I took the opportunity to fuse type the function.

I also noticed that the call to X.dtype.name is not free, whereas X.dtype is. When the inputs are rather small arrays, for instance in dict_learning they have shape ~ (n_components,), the computation can be completely dominated by the name finding:

X = np.random.RandomState(0).randn(100)                                                                                                                        

# main
%timeit min_pos(X)                                                                                                                                             
5.9 µs ± 35.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# pr
%timeit min_pos(X)                                                                                                                                              
297 ns ± 2.41 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Turns out it was taking a significant proportion of the time spent in the sparse coding.

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

Minor comment, otherwise LGTM

"""
Find the minimum value of an array over positive values

Returns a huge value if none of the values are positive
Copy link
Member

Choose a reason for hiding this comment

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

I see you are copying this over from before, but this can be slightly improved:

Suggested change
Returns a huge value if none of the values are positive
Returns maximum representable value of the input dtype if none of the values are positive

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, done.

@jeremiedbb
Copy link
Member Author

jeremiedbb commented Feb 2, 2021

Just realized the previous indentation was 3 spaces 0o :)

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

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

Looks good thanks!

@rth rth merged commit 673f625 into scikit-learn:main Feb 3, 2021
@glemaitre glemaitre added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label Feb 11, 2021
@glemaitre glemaitre added this to the 0.24.2 milestone Feb 11, 2021
@glemaitre glemaitre mentioned this pull request Apr 22, 2021
12 tasks
glemaitre pushed a commit to glemaitre/scikit-learn that referenced this pull request Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:utils To backport PR merged in master that need a backport to a release branch defined based on the milestone.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.