Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RFC] Always convert lists of lists of numbers to numpy arrays during input validation. #24745

Closed
@jjerphan

Description

@jjerphan

Describe the workflow you want to enable

Transformers and Estimators accept list of lists of numbers as valid for inputs like X.

Yet, when it comes to access to some basic attributes of the datasets (like the shape and the dtype which are present for numpy array) or to reach the best performances (e.g. be able to use Cython implementation which only operates on continuous buffers of memory), list of lists of numbers structure is inconvenient.

Also lists of lists really are used for simple examples (such as doctests) but are unlikely used in practice.

Describe your proposed solution

I propose changing inputs validation to always convert list of list of numbers to their associated natural numpy array.

In this context:

  • lists of lists of Python int will be converted to 2D numpy array of np.int64
  • lists of lists of Python float will be converted to 2D numpy array of np.float64
  • a RuntimeError will be raised if leaf element aren't numbers
  • a RuntimeError will be raised if internals list have different length (the case of ragged array)

There might be some cost and maintenance complexity in converting list of lists to numpy array.

Changes mostly need be made in:

  • BaseEstimator._validate_data:

    def _validate_data(
    self,
    X="no_validation",
    y="no_validation",
    reset=True,
    validate_separately=False,
    **check_params,
    ):

  • sklearn.utils.check_array:

    def check_array(
    array,
    accept_sparse=False,
    *,
    accept_large_sparse=True,
    dtype="numeric",
    order=None,
    copy=False,
    force_all_finite=True,
    ensure_2d=True,
    allow_nd=False,
    ensure_min_samples=1,
    ensure_min_features=1,
    estimator=None,
    input_name="",
    ):

Describe alternatives you've considered, if relevant

Continue supporting list of lists of numbers and introduce utility functions to be able to get basic attributes of the datasets which this structure.

Additional context

Listing references as I find them:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions