|
12 | 12 | recall_score, |
13 | 13 | f1_score, |
14 | 14 | roc_auc_score, |
| 15 | + matthews_corrcoef, |
15 | 16 | ) |
16 | 17 |
|
17 | 18 |
|
@@ -312,6 +313,89 @@ def test_roc_auc_errors(self, y_true: list, y_pred: list, error_type: type | tup |
312 | 313 | roc_auc_score(y_true=y_true, y_pred=y_pred) |
313 | 314 |
|
314 | 315 |
|
| 316 | +class TestMatthewsCorrcoef: |
| 317 | + @pytest.fixture(scope="class") |
| 318 | + def sk_mcc(self): |
| 319 | + from sklearn.metrics import matthews_corrcoef |
| 320 | + |
| 321 | + return matthews_corrcoef |
| 322 | + |
| 323 | + def test_binary_classification(self, sk_mcc): |
| 324 | + y_true = [1, 1, 0, 0] |
| 325 | + y_pred = [1, 0, 0, 0] |
| 326 | + |
| 327 | + expected = sk_mcc(y_true, y_pred) |
| 328 | + result = matthews_corrcoef(y_true, y_pred) |
| 329 | + |
| 330 | + assert result == pytest.approx(expected) |
| 331 | + |
| 332 | + def test_multiclass_classification(self, sk_mcc): |
| 333 | + y_true = [0, 1, 2, 0, 1, 2] |
| 334 | + y_pred = [0, 1, 1, 0, 2, 2] |
| 335 | + |
| 336 | + expected = sk_mcc(y_true, y_pred) |
| 337 | + result = matthews_corrcoef(y_true, y_pred) |
| 338 | + |
| 339 | + assert result == pytest.approx(expected) |
| 340 | + |
| 341 | + def test_numpy_arrays(self, sk_mcc): |
| 342 | + y_true = np.array([1, 1, 0, 0]) |
| 343 | + y_pred = np.array([1, 0, 0, 0]) |
| 344 | + |
| 345 | + expected = sk_mcc(y_true, y_pred) |
| 346 | + result = matthews_corrcoef(y_true, y_pred) |
| 347 | + |
| 348 | + assert result == pytest.approx(expected) |
| 349 | + |
| 350 | + def test_pandas_series(self, sk_mcc): |
| 351 | + y_true = pd.Series([1, 1, 0, 0]) |
| 352 | + y_pred = pd.Series([1, 0, 0, 0]) |
| 353 | + |
| 354 | + expected = sk_mcc(y_true, y_pred) |
| 355 | + result = matthews_corrcoef(y_true, y_pred) |
| 356 | + |
| 357 | + assert result == pytest.approx(expected) |
| 358 | + |
| 359 | + def test_sample_weight(self, sk_mcc): |
| 360 | + y_true = [1, 1, 0, 0] |
| 361 | + y_pred = [1, 0, 0, 0] |
| 362 | + weights = [1.0, 2.0, 1.0, 1.0] |
| 363 | + |
| 364 | + expected = sk_mcc( |
| 365 | + y_true, |
| 366 | + y_pred, |
| 367 | + sample_weight=weights, |
| 368 | + ) |
| 369 | + |
| 370 | + result = matthews_corrcoef( |
| 371 | + y_true, |
| 372 | + y_pred, |
| 373 | + sample_weight=weights, |
| 374 | + ) |
| 375 | + |
| 376 | + assert result == pytest.approx(expected) |
| 377 | + |
| 378 | + def test_mismatched_lengths_raises_value_error(self): |
| 379 | + y_true = [1, 0, 1] |
| 380 | + y_pred = [1, 0] |
| 381 | + |
| 382 | + with pytest.raises(ValueError): |
| 383 | + matthews_corrcoef(y_true, y_pred) |
| 384 | + |
| 385 | + @pytest.mark.parametrize( |
| 386 | + "y_true,y_pred", |
| 387 | + [ |
| 388 | + ("invalid", [1, 0, 1]), |
| 389 | + ([1, 0, 1], "invalid"), |
| 390 | + (123, [1, 0, 1]), |
| 391 | + ([1, 0, 1], 123), |
| 392 | + ], |
| 393 | + ) |
| 394 | + def test_invalid_types_raise_error(self, y_true, y_pred): |
| 395 | + with pytest.raises(Exception): |
| 396 | + matthews_corrcoef(y_true, y_pred) |
| 397 | + |
| 398 | + |
315 | 399 | class TestInputTypes: |
316 | 400 | """Test that metrics work with different input types (lists, numpy, pandas).""" |
317 | 401 |
|
|
0 commit comments