Skip to content

MAINT Avoid RuntimeWarning about invalid value in cast - #29977

Merged
OmarManzoor merged 1 commit into
scikit-learn:mainfrom
lesteve:fix-warning-invalid-value-in-cast
Oct 7, 2024
Merged

OmarManzoor merged 1 commit into
scikit-learn:mainfrom
lesteve:fix-warning-invalid-value-in-cast

Conversation

@lesteve

@lesteve lesteve commented Sep 30, 2024 •

Copy link
Copy Markdown
Member

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)

@github-actions

Copy link
Copy Markdown

✔️ Linting Passed

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

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

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

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.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Thanks @lesteve

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