Skip to content

MAINT Avoid RuntimeWarning about invalid value in cast #29977

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

Conversation

lesteve
Copy link
Member

@lesteve lesteve commented Sep 30, 2024

Fix #29929.

I implemented my suggestion in #29929 (comment) and added a simple unit test.

The unit test seems to fail on main consistently. Note that this relies on the values of np.empty so this is not fully deterministic but on my machine it seems like the following snippet consistently produces a warning:

import numpy as np

np.empty(1000).astype(np.int64)

A more realistic snippet that gives the warning is below. On my machine this produces a RuntimeWarning roughly 3-4 times out of 10 but takes a lot longer than the unit test (each run takes maybe ~2 seconds).

import numpy as np

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_classification

X, y = make_classification(n_samples=10)
lr = LogisticRegression()
grid = GridSearchCV(lr, cv=2, param_grid={'max_iter': range(40, 240)})
grid.fit(X, y)

Copy link

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 82368a6. Link to the linter CI: here

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

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

LGTM. Since it just avoids a warning I'm okay with not adding a changelog entry.

Copy link
Contributor

@OmarManzoor OmarManzoor left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @lesteve

@OmarManzoor OmarManzoor merged commit b0bc138 into scikit-learn:main Oct 7, 2024
41 of 43 checks passed
BenJourdan pushed a commit to gregoryschwartzman/scikit-learn that referenced this pull request Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom estimator's fit() method throws "RuntimeWarning: invalid value encountered in cast" in Linux Python 3.11/3.12
3 participants