Skip to content

Should train_test_split warn or error out on single sample array? #11028

Closed

Description

@vivekk0903

Description

train_test_split splits the single sample data such that train part has 0 samples and test has that sample. Also this behaviour is not affected by setting the test_size to any value.

Steps/Code to Reproduce

import numpy as np
from sklearn.model_selection import train_test_split

data = np.random.normal(0, 1, [1, 100])
print(A.shape)
#Output:  (1, 100)

data_train, data_test = train_test_split(data)
print(data_train.shape, data_test.shape)
#Output:  ((0, 100), (1, 100))

Expected Results

I am not sure of expected results as this seems like an unintended usage. But still think that at-least a warning (if not error) should be given when splitting.

Versions

Linux-3.16.0-77-generic-x86_64-with-Ubuntu-14.04-trusty
('Python', '2.7.6 (default, Nov 23 2017, 15:49:48) \n[GCC 4.8.4]')
('NumPy', '1.14.2')
('SciPy', '1.0.1')
('Scikit-Learn', '0.19.1')

I am sorry if its a duplicate. I tried searching for similar issues but could not find (even though I thought that this would have been discussed somewhere).

Activity

  1. rth commented on Apr 25, 2018

    @rth
    Member

    Thanks for raising this issue!

    I think it would make sense to be consistent with numpy.split here,

    >>> import numpy as np
    >>> from sklearn.model_selection import train_test_split
    >>> x = np.random.RandomState().rand(0, 100)
    >>> x
    array([], shape=(0, 100), dtype=float64)
    >>> np.split(x, 2)
    [array([], shape=(0, 100), dtype=float64), array([], shape=(0, 100), dtype=float64)]
    >>> train_test_split(x)
    [array([], shape=(0, 100), dtype=float64), array([], shape=(0, 100), dtype=float64)]

    The current behavior is consistent with it.

    I agree that it's somewhat unintuitive, but so is any operation of empty array (or in this case an input that would produce an empty array), e.g.

    >>> x.dot(x.T)
    array([], shape=(0, 0), dtype=float64)

    scikit-learn just follows numpy conventions on this, so I'm going to close this. Please comment if you disagree.

  2. rth commented on Apr 25, 2018

    @rth
    Member

    splits the single sample data such that train part has 0 samples and test has that sample. Also this behaviour is not affected by setting the test_size to any value.

    Yes, the repartion between train and test is not satisfactory in this case, but rasing an error/warning for 1 input sample, and then not rasing it for 0 samples to be consistent with numpy is also not very satisfactory. I can't think of a reason why why one would expect a meaningful train / test split with 1 sample and don't think we should spend time fixing this.

  3. jnothman commented on Apr 25, 2018

    @jnothman
    Member

    @vivekk-ezdi, could you explain how you landed up performing train_test_split on a single sample?

  4. vivekk0903 commented on Apr 25, 2018

    @vivekk0903
    ContributorAuthor

    @jnothman It was a beginner question at stackoverflow, in which the user I think by mistake reshaped the data to have one sample.

    I tested and was surprised that train_test_split doesnt complain about it. Since most of other scikit utilities warn or throw error in this case, I just wanted to know the rationale behind it.

  5. jnothman commented on Apr 25, 2018

    @jnothman
    Member
  6. NicolasHug commented on Dec 6, 2018

    @NicolasHug
    Member

    could you explain how you landed up performing train_test_split on a single sample?

    I encountered the same thing in an estimator that does early-stopping. I'm using train_test_split to hold out some validation data.

    When I run check_estimator on my estimator, it fails on check_fit2d_1sample: this tests passes only one sample, and the training set becomes empty.

    I was surprised as well that train_test_split doesn't complain. Basically that means that any estimator that internally uses train_test_split without checking the output cannot pass the check_estimator suite.

  7. jnothman commented on Dec 9, 2018

    @jnothman
    Member
  8. NicolasHug commented on Dec 9, 2018

    @NicolasHug
    Member

    My feeling is that train_test_split should raise an error when one of the output is empty. It makes sense in numpy not to raise an error because np.split is very general, but in the context of scikit learn we're dealing with train/test data and I can't think of a scenario where it's OK to have one of these that's empty (except if you don't want to test, but then you just don't call train_test_split).

    Both check_array and check_X_y default ensure_min_samples to 1, so that's somehow an indication that in general no train/test set should be empty. The only occurrence of ensure_min_samples=0 in scikit-learn code base is in DummyRegressor when method='constant': the prediction is a constant specified by the user.

  9. jnothman commented on Dec 10, 2018

    @jnothman
    Member
  10. NicolasHug commented on Dec 10, 2018

    @NicolasHug
    Member

    Ok I'll submit a PR.

    Should we do the checks upstream in the CV-iterators? train_test_split uses ShuffleSplit, and some other iterators like LeaveOneOut may also return empty train sets.

  11. jnothman commented on Dec 11, 2018

    @jnothman
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions