繁体   English   中英

使用带有scikit-learn的TfidfVectorizer的NLTK停用词时的Unicode警告

[英]Unicode Warning when using NLTK stopwords with TfidfVectorizer of scikit-learn

我试图使用来自sckit-learn的Tf-idf Vectorizer,使用来自NLTK的西班牙语停用词:

from nltk.corpus import stopwords

vectorizer = TfidfVectorizer(stop_words=stopwords.words("spanish"))

问题是我得到以下警告:

/home/---/.virtualenvs/thesis/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py:122: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
tokens = [w for w in tokens if w not in stop_words]

有没有简单的方法来解决这个问题?

实际上问题比我想象的更容易解决。 这里的问题是NLTK不返回unicode对象,而是str对象。 所以我需要在使用之前从utf-8解码它们:

stopwords = [word.decode('utf-8') for word in stopwords.words('spanish')]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM