|
27 | 27 | from sklearn import tree
|
28 | 28 | from sklearn.random_projection import _sparse_random_matrix
|
29 | 29 | from sklearn.exceptions import ConvergenceWarning
|
| 30 | +from .._base import _most_frequent |
30 | 31 |
|
31 | 32 |
|
32 | 33 | def _check_statistics(X, X_true,
|
@@ -1474,3 +1475,29 @@ def test_simple_imputation_inverse_transform_exceptions(missing_value):
|
1474 | 1475 | with pytest.raises(ValueError,
|
1475 | 1476 | match=f"Got 'add_indicator={imputer.add_indicator}'"):
|
1476 | 1477 | imputer.inverse_transform(X_1_trans)
|
| 1478 | + |
| 1479 | + |
| 1480 | +def test_most_frequent(): |
| 1481 | + # collections.Counter |
| 1482 | + assert np.isnan(_most_frequent(np.array([]), 'extra_value', 0)) |
| 1483 | + assert 'extra_value' == _most_frequent( |
| 1484 | + np.array(['a', 'b', 'c'], dtype=object), 'extra_value', 2 |
| 1485 | + ) |
| 1486 | + assert 'most_frequent' == _most_frequent( |
| 1487 | + np.array( |
| 1488 | + ['most_frequent_value', 'most_frequent_value', 'value'], |
| 1489 | + dtype=object), 'extra_value', 1 |
| 1490 | + ) |
| 1491 | + assert 'a' == _most_frequent(np.array( |
| 1492 | + ['min_value', 'min_value' 'value'], dtype=object), 'a', 2 |
| 1493 | + ) |
| 1494 | + assert 'min_value' == _most_frequent( |
| 1495 | + np.array(['min_value', 'min_value', 'value'], dtype=object), 'z', 2 |
| 1496 | + ) |
| 1497 | + |
| 1498 | + # scipy.stats.mode |
| 1499 | + assert np.isnan(_most_frequent(np.array([]), 10, 0)) |
| 1500 | + assert 10 == _most_frequent(np.array([1, 2, 3]), 10, 2) |
| 1501 | + assert 1 == _most_frequent(np.array([1, 1, 2]), 10, 1) |
| 1502 | + assert 10 == _most_frequent(np.array([20, 20, 1]), 10, 2) |
| 1503 | + assert 1 == _most_frequent(np.array([1, 1, 20], dtype=object), 10, 2) |
0 commit comments