Skip to content

Fix SklearnModelWrapper: get_feature_names() removed in sklearn 1.2+ - #838

Merged
qiyanjun merged 1 commit into
QData:masterfrom
qiyanjun:fix/sklearn-wrapper-get-feature-names
Aug 14, 2026
Merged

Fix SklearnModelWrapper: get_feature_names() removed in sklearn 1.2+#838
qiyanjun merged 1 commit into
QData:masterfrom
qiyanjun:fix/sklearn-wrapper-get-feature-names

Conversation

@qiyanjun

Copy link
Copy Markdown
Member

Summary

SklearnModelWrapper.__call__ calls self.tokenizer.get_feature_names() — but tokenizer here is actually a fitted CountVectorizer/TfidfVectorizer, and get_feature_names() was removed from scikit-learn in 1.2 (Dec 2022) in favor of get_feature_names_out() (available since 1.0). Any use of SklearnModelWrapper — including its own demo notebook, docs/2notebook/Example_1_sklearn.ipynb — has been broken with any scikit-learn released in the last several years, raising AttributeError on the first real prediction call.

Fix

  • textattack/models/wrappers/sklearn_model_wrapper.py: get_feature_names()get_feature_names_out().
  • docs/2notebook/Example_1_sklearn.ipynb: same fix in the 4 places the notebook does its own vectorizer → DataFrame conversion. Also fixed !pip install datasets nltk sklearnscikit-learn — the sklearn PyPI package name is deprecated and its installer now aborts with an explicit error directing users to scikit-learn instead.

The notebook fix was applied as a surgical text substitution (not a full cell replace), so the notebook's existing recorded outputs and execution counts — the demonstrated training accuracy / attack results from a real prior run — are left intact rather than wiped.

Verification

  • Reproduced the AttributeError directly against the currently pinned scikit-learn (1.7.2): CountVectorizer().get_feature_names()AttributeError: 'CountVectorizer' object has no attribute 'get_feature_names'
  • Confirmed get_feature_names_out() works end-to-end through the actual wrapper (fit a real CountVectorizer + LogisticRegression, ran predictions through SklearnModelWrapper)
  • Notebook JSON validated (json.load) after the edit; diff is minimal (5 lines) and doesn't touch stored outputs
  • black --check / flake8 clean; from textattack.models.wrappers import SklearnModelWrapper still imports cleanly

🤖 Generated with Claude Code

self.tokenizer.get_feature_names() (tokenizer here is actually a
fitted CountVectorizer/TfidfVectorizer) was removed from scikit-learn
in 1.2 (Dec 2022) in favor of get_feature_names_out(), added in 1.0.
Any SklearnModelWrapper usage - including its own demo,
docs/2notebook/Example_1_sklearn.ipynb - has been broken with any
scikit-learn released in the last several years, raising
AttributeError on the first real prediction call.

Verified: reproduced the AttributeError against the currently pinned
scikit-learn (1.7.2), confirmed get_feature_names_out() works
end-to-end through the actual wrapper.

Also fixed the notebook's `!pip install ... sklearn` to
`scikit-learn`: the `sklearn` PyPI package name is deprecated and its
installer now aborts with an explicit error directing users to
scikit-learn instead.

Kept the notebook's existing recorded outputs/execution counts intact
(surgical text substitution rather than a full NotebookEdit cell
replace, which would have wiped them).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@qiyanjun
qiyanjun merged commit 1050b1a into QData:master Aug 14, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant