Skip to content

Ensure all CSR / CSC matrices that we hack are valid #7762

Closed

Description

@amueller

See scipy/scipy#6719 and #7750.
I think we should start with a git grep for .indices but maybe also add something to the common tests to check the validity of the outputs.

Activity

  1. added
    Moderate
    on Mar 3, 2017
  2. gvacaliuc commented on Jun 15, 2018

    @gvacaliuc
    Contributor

    I'm working on this one.

  3. gvacaliuc commented on Jun 15, 2018

    @gvacaliuc
    Contributor

    This is the grep command and output I'm using to locate usages of potential error:

    EDIT: I realized the issue is only for manual hacking of .indices and .indptr.

    $ grep sklearn/**/*.py -H -Ee "\.(indices|indptr)\s*[^=<>]=\s"
    sklearn/feature_extraction/text.py:        X.indices = map_index.take(X.indices, mode='clip')
    sklearn/model_selection/tests/common.py:        self.indices = iter(KFold(n_splits=n_splits).split(np.ones(n_samples)))
    sklearn/preprocessing/label.py:        yt.indices = np.array(inverse[yt.indices], dtype=yt.indices.dtype,
    sklearn/utils/sparsefuncs.py:    X.indices = np.concatenate([X.indices[:m_start],
    sklearn/utils/tests/test_extmath.py:            Xcsr.indptr = Xcsr.indptr.astype(csr_index_dtype)
    sklearn/utils/tests/test_extmath.py:            Xcsr.indices = Xcsr.indices.astype(csr_index_dtype)
    sklearn/utils/tests/test_sparsefuncs.py:                    X_csr.indptr = X_csr.indptr.astype(index_dtype)
    sklearn/utils/tests/test_sparsefuncs.py:                    X_csr.indices = X_csr.indices.astype(index_dtype)

    Explanation: Searches for lines that directly set indices or indptr, but excludes boolean operators which contain = (<=, >=, ==).

  4. gvacaliuc commented on Jun 15, 2018

    @gvacaliuc
    Contributor

    After looking line by line, these are the ones that could potentially cause issues and need to be addressed:

    • sklearn/feature_extraction/text.py: X.indices = map_index.take(X.indices, mode='clip')
      • will cause an issue if the CountVectorizers sparse feature matrix requires 64-bit indices, as np.int32 is hard coded for the map_index.
  5. amueller commented on Jun 15, 2018

    @amueller
    MemberAuthor

    wow that's some good detective work right there!

  6. gvacaliuc commented on Jun 15, 2018

    @gvacaliuc
    Contributor

    OK, that should be it, the other lines from the grep above either reference indices from CV Folds, are made from the existing sparse matrix's indices, or set both indices and indptr together (which is OK).

  7. cmarmo commented on Apr 1, 2021

    @cmarmo
    Contributor

    If I understand correctly, the only failure reported in this comment has been fixed by #11295. I'm closing this one. Feel free to reopen or open a new one with updated context if I'm wrong.

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

    Moderatehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions