|
20 | 20 | from scipy.sparse import issparse, dok_matrix, lil_matrix |
21 | 21 | from scipy.sparse.base import spmatrix |
22 | 22 | from collections.abc import Sequence |
| 23 | +from numbers import Integral |
23 | 24 |
|
24 | 25 |
|
25 | 26 | class DataConversionWarning(UserWarning): |
@@ -141,26 +142,6 @@ def _check_X_y(X, y, dtype="numeric", accept_sparse=False, order=None, copy=Fals |
141 | 142 | return X, y |
142 | 143 |
|
143 | 144 |
|
144 | | -def _check_is_fitted(estimator, attributes=None, *, msg=None): |
145 | | - if msg is None: |
146 | | - msg = ("This %(name)s instance is not fitted yet. Call 'fit' with " |
147 | | - "appropriate arguments before using this estimator.") |
148 | | - |
149 | | - if not hasattr(estimator, 'fit'): |
150 | | - raise TypeError("%s is not an estimator instance." % (estimator)) |
151 | | - |
152 | | - if attributes is not None: |
153 | | - if not isinstance(attributes, (list, tuple)): |
154 | | - attributes = [attributes] |
155 | | - attrs = all([hasattr(estimator, attr) for attr in attributes]) |
156 | | - else: |
157 | | - attrs = [v for v in vars(estimator) |
158 | | - if v.endswith("_") and not v.startswith("__")] |
159 | | - |
160 | | - if not attrs: |
161 | | - raise AttributeError(msg % {'name': type(estimator).__name__}) |
162 | | - |
163 | | - |
164 | 145 | def _check_classification_targets(y): |
165 | 146 | y_type = _type_of_target(y) |
166 | 147 | if y_type not in ['binary', 'multiclass', 'multiclass-multioutput', |
@@ -329,3 +310,31 @@ def _num_features(X): |
329 | 310 | return len(first_sample) |
330 | 311 | except Exception as err: |
331 | 312 | raise TypeError(message) from err |
| 313 | + |
| 314 | + |
| 315 | +def _num_samples(x): |
| 316 | + message = "Expected sequence or array-like, got %s" % type(x) |
| 317 | + if hasattr(x, "fit") and callable(x.fit): |
| 318 | + # Don't get num_samples from an ensembles length! |
| 319 | + raise TypeError(message) |
| 320 | + |
| 321 | + if not hasattr(x, "__len__") and not hasattr(x, "shape"): |
| 322 | + if hasattr(x, "__array__"): |
| 323 | + x = np.asarray(x) |
| 324 | + else: |
| 325 | + raise TypeError(message) |
| 326 | + |
| 327 | + if hasattr(x, "shape") and x.shape is not None: |
| 328 | + if len(x.shape) == 0: |
| 329 | + raise TypeError( |
| 330 | + "Singleton array %r cannot be considered a valid collection." % x |
| 331 | + ) |
| 332 | + # Check that shape is returning an integer or default to len |
| 333 | + # Dask dataframes may not return numeric shape[0] value |
| 334 | + if isinstance(x.shape[0], Integral): |
| 335 | + return x.shape[0] |
| 336 | + |
| 337 | + try: |
| 338 | + return len(x) |
| 339 | + except TypeError as type_error: |
| 340 | + raise TypeError(message) from type_error |
0 commit comments