8000 API Deprecates support for np.matrix in check_array (#20165) · scikit-learn/scikit-learn@6850c04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6850c04

Browse files
authored
API Deprecates support for np.matrix in check_array (#20165)
* API Deprecates support for np.matrix in check_array * DOC Adds whats new * ENH Adds link to numpy.matrix
1 parent 07a0cf3 commit 6850c04

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

doc/whats_new/v1.0.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change 8000
@@ -123,6 +123,9 @@ Changelog
123123
- For :class:`tree.ExtraTreeRegressor`, `criterion="mae"` is deprecated,
124124
use `"absolute_error"` instead.
125125

126+
- |API| `np.matrix` usage is deprecated in 1.0 and will raise a `TypeError` in
127+
1.2. :pr:`20165` by `Thomas Fan`_.
128+
126129
:mod:`sklearn.base`
127130
...................
128131

@@ -512,6 +515,13 @@ Changelog
512515
precision of the computed variance was very poor when the real variance is
513516
exactly zero. :pr:`19766` by :user:`Jérémie du Boisberranger <jeremiedbb>`.
514517

518+
:mod:`sklearn.validation`
519+
.........................
520+
521+
- |Fix| Support for `np.matrix` is deprecated in
522+
:func:`~sklearn.utils.check_array` in 1.0 and will raise a `TypeError` in
523+
1.2. :pr:`20165` by `Thomas Fan`_.
524+
515525
Code and Documentation Contributors
516526
-----------------------------------
517527

sklearn/utils/tests/test_validation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
from sklearn.utils._testing import TempMemmap
5858

5959

60+
# TODO: Remove np.matrix usage in 1.2
61+
@pytest.mark.filterwarnings(
62+
"ignore:np.matrix usage is deprecated in 1.0:FutureWarning")
6063
@pytest.mark.filterwarnings(
6164
"ignore:the matrix subclass:PendingDeprecationWarning")
6265
def test_as_float_array():
@@ -115,6 +118,9 @@ def test_as_float_array_nan(X):
115118
assert_allclose_dense_sparse(X_converted, X)
116119

117120

121+
# TODO: Remove np.matrix usage in 1.2
122+
@pytest.mark.filterwarnings(
123+
"ignore:np.matrix usage is deprecated in 1.0:FutureWarning")
118124
@pytest.mark.filterwarnings(
119125
"ignore:the matrix subclass:PendingDeprecationWarning")
120126
def test_np_matrix():
@@ -1379,3 +1385,16 @@ def test_num_features_errors_scalars(X):
13791385
)
13801386
with pytest.raises(TypeError, match=msg):
13811387
_num_features(X)
1388+
1389+
1390+
# TODO: Remove in 1.2
1391+
@pytest.mark.filterwarnings(
1392+
"ignore:the matrix subclass:PendingDeprecationWarning")
1393+
def test_check_array_deprecated_matrix():
1394+
"""Test that matrix support is deprecated in 1.0."""
1395+
1396+
X = np.matrix(np.arange(5))
1397+
msg = ("np.matrix usage is deprecated in 1.0 and will raise a TypeError "
1398+
"in 1.2. Please convert to a numpy array with np.asarray.")
1399+
with pytest.warns(FutureWarning, match=msg):
1400+
check_array(X)

sklearn/utils/validation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,14 @@ def check_array(array, accept_sparse=False, *, accept_large_sparse=True,
543543
array_converted : object
544544
The converted and validated array.
545545
"""
546+
if isinstance(array, np.matrix):
547+
warnings.warn(
548+
"np.matrix usage is deprecated in 1.0 and will raise a TypeError "
549+
"in 1.2. Please convert to a numpy array with np.asarray. For "
550+
"more information see: "
551+
"https://numpy.org/doc/stable/reference/generated/numpy.matrix.html", # noqa
552+
FutureWarning)
553+
546554
# store reference to original array to check if copy is needed when
547555
# function returns
548556
array_orig = array

0 commit comments

Comments
 (0)
0