Return number of samples in array-like x.
(x)
| 363 | |
| 364 | |
| 365 | def _num_samples(x): |
| 366 | """Return number of samples in array-like x.""" |
| 367 | message = "Expected sequence or array-like, got %s" % type(x) |
| 368 | if hasattr(x, "fit") and callable(x.fit): |
| 369 | # Don't get num_samples from an ensembles length! |
| 370 | raise TypeError(message) |
| 371 | |
| 372 | if hasattr(x, "shape") and x.shape is not None: |
| 373 | if len(x.shape) == 0: |
| 374 | raise TypeError( |
| 375 | "Input should have at least 1 dimension i.e. satisfy " |
| 376 | f"`len(x.shape) > 0`, got scalar `{x!r}` instead." |
| 377 | ) |
| 378 | # Check that shape is returning an integer or default to len |
| 379 | # Dask dataframes may not return numeric shape[0] value |
| 380 | if isinstance(x.shape[0], numbers.Integral): |
| 381 | return x.shape[0] |
| 382 | |
| 383 | if _nw_into_df_or_series(x): |
| 384 | return nw.from_native(x, allow_series=True).shape[0] |
| 385 | |
| 386 | if not hasattr(x, "__len__") and not hasattr(x, "shape"): |
| 387 | if hasattr(x, "__array__"): |
| 388 | xp, _ = get_namespace(x) |
| 389 | x = xp.asarray(x) |
| 390 | else: |
| 391 | raise TypeError(message) |
| 392 | |
| 393 | try: |
| 394 | return len(x) |
| 395 | except TypeError as type_error: |
| 396 | raise TypeError(message) from type_error |
| 397 | |
| 398 | |
| 399 | def check_memory(memory): |
searching dependent graphs…