8000 TST Test check_consistent_length and TypeError with ensemble arg · scikit-learn/scikit-learn@69827c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 69827c4

Browse files
jnothmanogrisel
authored andcommitted
TST Test check_consistent_length and TypeError with ensemble arg
1 parent 97d8341 commit 69827c4

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

sklearn/utils/tests/test_validation.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
"""Tests for input validation functions"""
22

33
from tempfile import NamedTemporaryFile
4+
from itertools import product
5+
46
import numpy as np
57
from numpy.testing import assert_array_equal, assert_warns
68
import scipy.sparse as sp
79
from nose.tools import assert_raises, assert_true, assert_false, assert_equal
8-
from itertools import product
910

11+
from sklearn.utils.testing import assert_raises_regexp
1012
from sklearn.utils import as_float_array, check_array, check_symmetric
1113
from sklearn.utils import check_X_y
12-
1314
from sklearn.utils.estimator_checks import NotAnArray
14-
1515
from sklearn.random_projection import sparse_random_matrix
16-
1716
from sklearn.linear_model import ARDRegression
1817
from sklearn.neighbors import KNeighborsClassifier
1918
from sklearn.ensemble import RandomForestRegressor
2019
from sklearn.svm import SVR
21-
2220
from sklearn.datasets import make_blobs
2321
from sklearn.utils.validation import (
2422
NotFittedError,
2523
has_fit_parameter,
26-
check_is_fitted)
24+
check_is_fitted,
25+
check_consistent_length)
2726

2827
from sklearn.utils.testing import assert_raise_message
2928

@@ -337,3 +336,21 @@ def test_check_is_fitted():
337336

338337
assert_equal(None, check_is_fitted(ard, "coef_"))
339338
assert_equal(None, check_is_fitted(svr, "support_"))
339+
340+
341+
def test_check_consistent_length():
342+
check_consistent_length([1], [2], [3], [4], [5])
343+
check_consistent_length([[1, 2], [[1, 2]]], [1, 2], ['a', 'b'])
344+
check_consistent_length([1], (2,), np.array([3]), sp.csr_matrix((1, 2)))
345+
assert_raises_regexp(ValueError, 'inconsistent numbers of samples',
346+
check_consistent_length, [1, 2], [1])
347+
assert_raises_regexp(TypeError, 'got <\w+ \'int\'>',
348+
check_consistent_length, [1, 2], 1)
349+
assert_raises_regexp(TypeError, 'got <\w+ \'object\'>',
350+
check_consistent_length, [1, 2], object())
351+
352+
assert_raises(TypeError, check_consistent_length, [1, 2], np.array(1))
353+
# Despite ensembles having __len__ they must raise TypeError
354+
assert_raises_regexp(TypeError, 'estimator', check_consistent_length,
355+
[1, 2], RandomForestRegressor())
356+
# XXX: We should have a test with a string, but what is correct behaviour?

sklearn/utils/validation.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,16 @@ def _is_arraylike(x):
104104

105105
def _num_samples(x):
106106
"""Return number of samples in array-like x."""
107+
if hasattr(x, 'fit'):
108+
# Don't get num_samples from an ensembles length!
109+
raise TypeError('Expected sequence or array-like, got '
110+
'estimator %s' % x)
107111
if not hasattr(x, '__len__') and not hasattr(x, 'shape'):
108112
if hasattr(x, '__array__'):
109113
x = np.asarray(x)
110114
else:
111-
raise TypeError("Expected sequence or array-like, got %r" % x)
115+
raise TypeError("Expected sequence or array-like, got %s" %
116+
type(x))
112117
if hasattr(x, 'shape'):
113118
if len(x.shape) == 0:
114119
raise TypeError("Singleton array %r cannot be considered"
@@ -165,8 +170,8 @@ def check_consistent_length(*arrays):
165170

166171
uniques = np.unique([_num_samples(X) for X in arrays if X is not None])
167172
if len(uniques) > 1:
168-
raise ValueError("Found arrays with inconsistent numbers of samples: %s"
169-
% str(uniques))
173+
raise ValueError("Found arrays with inconsistent numbers of samples: "
174+
"%s" % str(uniques))
170175

171176

172177
def indexable(*iterables):

0 commit comments

Comments
 (0)
0