sklearn 0.20-0.23版本下CountVectorizer报'object has no attribute get_feature_names_out'错误的解决咨询
Hey there! I see you're hitting an error with your text feature extraction code, let's break down why this is happening and how to fix it quickly.
Why the Error Happens
The get_feature_names_out() method was introduced in scikit-learn version 1.0, but you're using versions 0.20 to 0.23—those older releases don't include this method yet. That's exactly why Python is throwing that attribute error at you.
Two Simple Solutions
1. Upgrade scikit-learn to a newer version
If you're okay updating your sklearn installation, this is the cleanest long-term fix. Run one of these commands depending on whether you use pip or conda:
- Using pip:
pip install --upgrade scikit-learn - Using conda:
conda update scikit-learn
Once upgraded to version 1.0 or higher, your original code with get_feature_names_out() will work as expected.
2. Use the older method compatible with your current version
If you can't or don't want to upgrade sklearn, just replace get_feature_names_out() with the older equivalent method that exists in 0.20-0.23: get_feature_names().
Here's the modified line in your code:
ngrams = pd.DataFrame(frequencies, index=vectorizer.get_feature_names(), columns=['frequency'])
This method does the exact same thing for your use case—retrieving the feature names from the CountVectorizer—so your code will run without errors.
Either of these approaches should resolve your issue right away!
内容的提问来源于stack exchange,提问作者A. Omag




