Skip to content

FIX IsolationForest accepts sparse matrix with float value contamination - #27645

Merged
OmarManzoor merged 5 commits into
scikit-learn:mainfrom
glemaitre:is/27626
Dec 2, 2023
Merged

OmarManzoor merged 5 commits into
scikit-learn:mainfrom
glemaitre:is/27626

Conversation

@glemaitre

@glemaitre glemaitre commented Oct 23, 2023 •

Copy link
Copy Markdown
Member

closes #27626

This PR makes sure to convert to CSR the validated CSC matrix when contamination is a floating number. We still don't want to call score_samples because it will cost an additional validation, more expensive than the expected X.asformat("csr").

@github-actions

github-actions Bot commented Oct 23, 2023 •

Copy link
Copy Markdown

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: e5cb229. Link to the linter CI: here

Comment thread sklearn/ensemble/_iforest.py Resolved

@adam2392 adam2392 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for me.

"""
X, _ = make_classification(n_samples=50, n_features=4, random_state=0)
X = sparse_container(X)
X.sort_indices()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why do we need to sort the indices?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea. I took back the minimum reproducer but it was probably failing without sorting indices.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting indices allows getting into a canonical representation (e.g. with no duplicates' ambiguity).

Comment thread sklearn/ensemble/tests/test_iforest.py Resolved

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @glemaitre

@OmarManzoor
OmarManzoor enabled auto-merge (squash) December 2, 2023 06:31
@OmarManzoor
OmarManzoor merged commit 9371cec into scikit-learn:main Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Isolation Forest Bug with Sparse Matrix and Contamination as Float

4 participants