Skip to content

Cross validation module should allow the user to provide its own (X_train, X_test) generators #5915

Closed

Description

@arthurmensch

The whole cross validation / parameter search relies on sample masks rather than train / test physical datasets. This is not convenient if we need for example to remove some features /sample from a sparse dataset to evaluate reconstruction. More flexibility would be possible if we were able to provide an iterator of (X_train, X_test) to GridSearchCV / cross_val_score.

Activity

  1. raghavrv commented on Nov 24, 2015

    @raghavrv
    Member

    We can do that with PredefinedSplit right?

  2. arthurmensch commented on Nov 24, 2015

    @arthurmensch
    ContributorAuthor

    I have been doing some hacks to be able to run recommender systems within sklearn. Cross validation implies taking a sparse matrix X (n_samples, n_features), with missing values, and to remove e.g. 20% of the features (different for each sample) for the training set, keeping them for the test test. Working with sparse matrix require specific construction of (X_test, X_train) for efficiency, so it would be great to be able to bypass automatic construction of these two matrices from provided splits. This is probably too early for it be useful for the end user, just writing it there to keep it in mind.

  3. raghavrv commented on Nov 25, 2015

    @raghavrv
    Member

    Ah! You want cross validation with missing values... sounds interesting...

    You can do something like...

    class CustomCrossValidator:
        def __init__(self, train_indices, test_indices):
            self.train_indices = train_indices
            self.test_indices = test_indices
    
        def split(self, X=None, y=None):
            return self.train_indices, self.test_indices
    

    And in the train_indices you could specify you train matrix indexing

    Since internally GridSearchCV trains on X[train], you can specify train_indices to be anything that is acceptable by numpy's slice notation...

    i.e X[train_indices] will be the matrix with 20% missing values and X[test_indices] would be the full matrix...

    Is this what you had wanted?

  4. raghavrv commented on Dec 9, 2015

    @raghavrv
    Member

    Could you confirm if I had understood your comment correctly?

  5. amueller commented on Dec 9, 2015

    @amueller
    Member

    I am hacking scikit learn to be able to do recommender systems.

    That's where the problem lies. Recommender systems don't really fit in the sklearn API ;)

  6. arthurmensch commented on Dec 11, 2015

    @arthurmensch
    ContributorAuthor

    The way I finally went was to use OneHotEncoder, split data, and go back to CSR matrix. @rvraghav93 the problem with this approach is that we work with sparse matrix, for which such indexing breaks the sparse structure

  7. punyidea commented on Jul 21, 2016

    @punyidea

    I'd like to mention that the PredefinedSplit code currently is not well-suited for anomaly detection. This is because the abnormal set must not be trained on, but still must be used for validation. At the moment, it's only possible to make one fold (because each member of test_code can only be the member of one validation fold) but it is indeed possible to reuse the validation set.

  8. amueller commented on Jul 27, 2016

    @amueller
    Member

    @punyidea can you open a separate issue providing a more detailed explanation and maybe some code? What would a better interface look like?

  9. amueller commented on Jul 27, 2016

    @amueller
    Member

    @arthurmensch I recommend we close this issue. In particular because of "and to remove e.g. 20% of the features (different for each sample) for the training set, keeping them for the test test"
    You want to treat each sample differently. That's not really what cross-validation iterators are for.

    Also, which part of grid-search do you actually want to use?

  10. amueller commented on Oct 27, 2016

    @amueller
    Member

    closing as @arthurmensch didn't object ;)

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