Repository navigation
FIX add missing random states and cloning in test_response.py - #33221
Conversation
| y = np.array([[0, 1], [1, 0]] * 5) | ||
|
|
||
| clf = clone(estimator).fit(X, y) | ||
| estimator = clone(estimator).fit(X, y) # clone to make test execution thread-safe |
There was a problem hiding this comment.
Unrelated small clean-up: changed clf toestimator because different estimators are tested here
And I added the comment on explaining the cloning here as well, because I didn't know this, and it probably doesn't hurt to be expressive on this.
| (KMeans(n_clusters=2, n_init=1), "predict", "binary", (10,)), | ||
| (KMeans(n_clusters=2, n_init=1), "predict", "multiclass", (10,)), | ||
| (KMeans(n_clusters=2), "predict", "binary", (10,)), | ||
| (KMeans(n_clusters=4), "predict", "multiclass", (10,)), |
There was a problem hiding this comment.
Unrelated small clean-up: remove redundant parameter and make test case more consistent
There was a problem hiding this comment.
Note: Here we also just check response shapes, so random states don't matter for this test.
There was a problem hiding this comment.
is changing to n_clusters=4 on purpose ?
There was a problem hiding this comment.
Yes, to make explicit that the output shape also doesn't change in the multiclass (or rather, "multicluster") case. I didn't see this in the PR where it was added, but I can also make a separate PR for the cleanu-ps if this is becoming too cluttered now.
There was a problem hiding this comment.
Thanks for the clarification. It's fine to keep it in this PR
| (DBSCAN(), "predict"), | ||
| (IsolationForest(random_state=0), "predict_proba"), | ||
| (IsolationForest(random_state=0), ["predict_proba", "score"]), | ||
| (IsolationForest(), "predict_proba"), |
There was a problem hiding this comment.
Here we just check the attributes, so setting the random_state has no impact. Not sure if we should consistently add or remove it in such cases?
There was a problem hiding this comment.
I think it's fine not to have random_state in this case.
| "estimator, response_method", | ||
| [ | ||
| (LinearRegression(), "predict"), | ||
| (KMeans(n_clusters=2, n_init=1), "predict"), |
There was a problem hiding this comment.
n_init=1 is the default value in this case anyways
| """Check the error message with not supported response method.""" | ||
| X, y = np.random.RandomState(0).randn(10, 2), np.array([0, 1] * 5) | ||
| estimator.fit(X, y) | ||
| estimator = clone(estimator).fit(X, y) # clone to make test execution thread-safe |
There was a problem hiding this comment.
Just curious, you are doing this for free-threaded because you have noticed issues when running the test locally with pytest-run-parallel?
There was a problem hiding this comment.
No, I saw that this was done like this with this comment on line 240 (added by Olivier) and adapted it for the rest.
|
Not sure why, but I am still getting a failure with free-threaded locally with the following command: Details for one of the errors: |
For me this passes locally, but the code on |
|
Oh, hang on, I also get the following messages when setting
Do you know how I can overwrite this to test it locally? |
So |
|
Another option would be to see if it is enough to replace |
I guess upgrading to Python 3.14 is required, right? I'll give that a try. |
In general with free-threaded it's better to use Python 3.14 indeed. The warning you get does ring a bell and I think you will not get it if you use Python 3.14 and you will get the failure instead. |
|
Actually, it does not seem to be related to pytest-run-parallel I do get occasional failures with free-threaded With pytest-repeat you can run the test multiple times and it will fail at one point: |
|
To be honest, using The relative difference is close to 1e-16 and so it is close to the float64 resolution, and I don't think we should spend too much time on this. Why it only happens for free-threaded, I don't know, there may be something fishy there, hard to tell ... |
|
This is not free-threaded specific I do see a similar thing as #33221 (comment) with vanilla Python 3.14. |
|
Should we still keep the random state and cloning changes or just merge #33225 instead? |
|
Let's keep this one I think, because I think it also fixes free-threaded problems. |
lesteve
left a comment
There was a problem hiding this comment.
LGTM, thanks!
This fixes all the failures on free-threaded locally for me and cleans up a few things.
Reference Issues/PRs
Towards #32393 and possibly also #33216
What does this implement/fix? Explain your changes.
I added the missing
random_seedto theKMeanstest cases and also addedclonebeforefit(as it is done for example in line 240) where it was missing.@lesteve
AI usage disclosure
I used AI assistance for:
Any other comments?