Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit7aed937

Browse files
Add tests for collinear point removal in ROC curve with edge cases (empty/small arrays)
1 parenteaa8aed commit7aed937

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

‎sklearn/metrics/_ranking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ def _roc_collinear_free_mask_xp(fps, tps, xp, device, tolerance=1e-12):
10761076
"""Return indices of non-collinear points preserving endpoints using array API."""
10771077
n=fps.shape[0]
10781078
ifn<=2:
1079-
returnxp.arange(n)
1079+
returnxp.arange(n,device=device)
10801080

10811081
# Compute segment vectors
10821082
dx0=fps[1:-1]-fps[:-2]

‎sklearn/metrics/tests/test_ranking.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
roc_curve,
2525
top_k_accuracy_score,
2626
)
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+
)
2832
fromsklearn.model_selectionimporttrain_test_split
2933
fromsklearn.preprocessingimportlabel_binarize
3034
fromsklearn.random_projectionimport_sparse_random_matrix
@@ -2268,3 +2272,47 @@ def test_roc_curve_with_probablity_estimates(global_random_seed):
22682272
y_score=rng.rand(10)
22692273
_,_,thresholds=roc_curve(y_true,y_score)
22702274
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))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp