|
24 | 24 | roc_curve,
|
25 | 25 | top_k_accuracy_score,
|
26 | 26 | )
|
27 |
| -fromsklearn.metrics._rankingimport_dcg_sample_scores,_ndcg_sample_scores |
| 27 | +fromsklearn.metrics._rankingimport ( |
| 28 | +_dcg_sample_scores, |
| 29 | +_ndcg_sample_scores, |
| 30 | +_roc_collinear_free_mask_xp, |
| 31 | +) |
28 | 32 | fromsklearn.model_selectionimporttrain_test_split
|
29 | 33 | fromsklearn.preprocessingimportlabel_binarize
|
30 | 34 | fromsklearn.random_projectionimport_sparse_random_matrix
|
@@ -2268,3 +2272,47 @@ def test_roc_curve_with_probablity_estimates(global_random_seed):
|
2268 | 2272 | y_score=rng.rand(10)
|
2269 | 2273 | _,_,thresholds=roc_curve(y_true,y_score)
|
2270 | 2274 | assertnp.isinf(thresholds[0])
|
| 2275 | + |
| 2276 | + |
| 2277 | +deftest_roc_collinear_free_mask_small_arrays(): |
| 2278 | +"""Test the collinear mask function with small arrays (edge cases). |
| 2279 | +
|
| 2280 | + This test verifies the behavior of _roc_collinear_free_mask_xp with: |
| 2281 | + - Empty arrays (length 0) |
| 2282 | + - Single-element arrays (length 1) |
| 2283 | + - Two-element arrays (length 2) |
| 2284 | +
|
| 2285 | + These cases test the early return paths in the function where no collinearity |
| 2286 | + checking is needed because there aren't enough points to form segments. |
| 2287 | + """ |
| 2288 | +# Use numpy as our array API module for testing |
| 2289 | +# (In actual usage, this could be any array API compatible library) |
| 2290 | +xp=np |
| 2291 | +# Device is None for numpy, but would be specified for other array APIs |
| 2292 | +device=None |
| 2293 | + |
| 2294 | +# Test empty array case - should return empty array of indices |
| 2295 | +# This verifies the function handles zero-length input gracefully |
| 2296 | +fps=xp.asarray([],device=device) |
| 2297 | +tps=xp.asarray([],device=device) |
| 2298 | +result=_roc_collinear_free_mask_xp(fps,tps,xp,device) |
| 2299 | +# Expected: array of indices for empty input (length 0) |
| 2300 | +assertxp.array_equal(result,xp.arange(0,device=device)) |
| 2301 | + |
| 2302 | +# Test single element array - should return single index [0] |
| 2303 | +# Verifies handling of minimal non-empty input |
| 2304 | +fps=xp.asarray([0.0],device=device) |
| 2305 | +tps=xp.asarray([0.0],device=device) |
| 2306 | +result=_roc_collinear_free_mask_xp(fps,tps,xp,device) |
| 2307 | +# Expected: single index array [0] |
| 2308 | +assertxp.array_equal(result,xp.arange(1,device=device)) |
| 2309 | + |
| 2310 | +# Test two element array - should return both indices [0, 1] |
| 2311 | +# Verifies handling of smallest case where collinearity could theoretically |
| 2312 | +# be checked, but the function should still return all indices since endpoints |
| 2313 | +# are always preserved |
| 2314 | +fps=xp.asarray([0.0,1.0],device=device) |
| 2315 | +tps=xp.asarray([0.0,1.0],device=device) |
| 2316 | +result=_roc_collinear_free_mask_xp(fps,tps,xp,device) |
| 2317 | +# Expected: both indices [0, 1] |
| 2318 | +assertxp.array_equal(result,xp.arange(2,device=device)) |