Fix SklearnModelWrapper: get_feature_names() removed in sklearn 1.2+ - #838
Merged
qiyanjun merged 1 commit intoAug 14, 2026
Merged
Conversation
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>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SklearnModelWrapper.__call__callsself.tokenizer.get_feature_names()— buttokenizerhere is actually a fittedCountVectorizer/TfidfVectorizer, andget_feature_names()was removed from scikit-learn in 1.2 (Dec 2022) in favor ofget_feature_names_out()(available since 1.0). Any use ofSklearnModelWrapper— including its own demo notebook,docs/2notebook/Example_1_sklearn.ipynb— has been broken with any scikit-learn released in the last several years, raisingAttributeErroron 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 sklearn→scikit-learn— thesklearnPyPI package name is deprecated and its installer now aborts with an explicit error directing users toscikit-learninstead.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
AttributeErrordirectly against the currently pinned scikit-learn (1.7.2):CountVectorizer().get_feature_names()→AttributeError: 'CountVectorizer' object has no attribute 'get_feature_names'get_feature_names_out()works end-to-end through the actual wrapper (fit a realCountVectorizer+LogisticRegression, ran predictions throughSklearnModelWrapper)json.load) after the edit; diff is minimal (5 lines) and doesn't touch stored outputsblack --check/flake8clean;from textattack.models.wrappers import SklearnModelWrapperstill imports cleanly🤖 Generated with Claude Code