【问题标题】:adding words to stop_words list in TfidfVectorizer in sklearn在 sklearn 的 TfidfVectorizer 中将单词添加到 stop_words 列表
【发布时间】:2015-01-05 16:56:58
【问题描述】:

我想在 TfidfVectorizer 中的 stop_words 中添加更多单词。我遵循了Adding words to scikit-learn's CountVectorizer's stop list 中的解决方案。我的停用词列表现在包含“英语”停用词和我指定的停用词。但是 TfidfVectorizer 仍然不接受我的停用词列表,我仍然可以在我的功能列表中看到这些词。下面是我的代码

from sklearn.feature_extraction import text
my_stop_words = text.ENGLISH_STOP_WORDS.union(my_words)

vectorizer = TfidfVectorizer(analyzer=u'word',max_df=0.95,lowercase=True,stop_words=set(my_stop_words),max_features=15000)
X= vectorizer.fit_transform(text)

我还尝试将 TfidfVectorizer 中的 stop_words 设置为 stop_words=my_stop_words 。但它仍然不起作用。请帮忙。

【问题讨论】:

  • 我确实使用了您的代码并以here 运行。我得到了预期的结果。你能提供更多细节吗?
  • 我正在对包含 url 的推文进行分类。现在我使用 SelectKBest 提取的特征包含这些 url。所以我想在我的停用词列表中添加这些网址,以便将其从我的功能集中删除。如上所示,我添加了这些网址。
  • 这是我的停用词列表的样子:frozenset(['', 'wA4qNj2o0b', 'all', 'fai5w3nBgo', 'Ikq7p9ElUW', '9W6GbM0MjL', 'four', 'WkOI43bsVj ','x88VDFBzkO','谁的','YqoLBzajjo','NVXydiHKSC','HdjXav51vI','q0YoiC0QCD','to','cTIYpRLarr','nABIG7dAlr','under','6JF33FZIYU','非常', 'AVFWjAWsbF'])
  • 这是我的功能集的样子:[u'bcvjby2owk', u'cases bcvjby2owk', u'cases dgvsrqaw7p', u'dgvsrqaw7p', u'8dsto3yxi2', u'guardianafrica' , u'guardianafrica 监护人\xe2', u'guardianafrica 监护人\xe2 nickswicks']
  • 我可以看到功能列表中没有出现任何停用词。因此,报告的行为是预期的。在这里,用于过滤这些哈希的方法是错误的。如果您将随机字符串作为停用词传递给矢量化器,它不会智能地过滤相似的字符串。停用词是要过滤的精确/硬编码字符串。或者,您可以使用正则表达式(在将文本块传递给矢量化器之前)过滤所有不需要的 url。这可能会解决您的网址问题。

标签: python scikit-learn classification stop-words text-classification


【解决方案1】:

你可以这样做:

from sklearn.feature_extraction import text
from sklearn.feature_extraction.text import TfidfVectorizer

my_stop_words = text.ENGLISH_STOP_WORDS.union(["book"])

vectorizer = TfidfVectorizer(ngram_range=(1,1), stop_words=my_stop_words)

X = vectorizer.fit_transform(["this is an apple.","this is a book."])

idf_values = dict(zip(vectorizer.get_feature_names(), vectorizer.idf_))

# printing the tfidf vectors
print(X)

# printing the vocabulary
print(vectorizer.vocabulary_)

在本例中,我为两个示例文档创建了 tfidf 向量:

"This is a green apple."
"This is a machine learning book."

默认情况下,thisisaan 都在 ENGLISH_STOP_WORDS 列表中。而且,我还在停用词列表中添加了book。这是输出:

(0, 1)  0.707106781187
(0, 0)  0.707106781187
(1, 3)  0.707106781187
(1, 2)  0.707106781187
{'green': 1, 'machine': 3, 'learning': 2, 'apple': 0}

正如我们所见,book 这个词也从功能列表中删除,因为我们将其列为停用词。结果,tfidfvectorizer 确实接受了手动添加的词作为停用词,并在创建向量时忽略了该词。

【讨论】:

  • 有没有办法从 ENGLISH_STOP_WORDS 中删除停用词,而不是添加它们,例如删除“不”?
  • @StamatisTiniakos 应该有。 ENGLISH_STOP_WORDS 的类型为:<class 'frozenset'>,因此仅作为示例,您可以使用此集合创建一个新列表并从列表中添加或删除单词,然后将其传递给您的矢量化器。
【解决方案2】:

为了与 scikit-learn 一起使用,您也可以随时使用列表:

from nltk.corpus import stopwords
stop = list(stopwords.words('english'))
stop.extend('myword1 myword2 myword3'.split())


vectorizer = TfidfVectorizer(analyzer = 'word',stop_words=set(stop))
vectors = vectorizer.fit_transform(corpus)
...

此方法的唯一缺点是,您的列表可能最终包含重复项,这就是为什么我在将其用作 TfidfVectorizer 的参数时将其转换回来的原因

【讨论】:

    【解决方案3】:

    这里有答案:https://stackoverflow.com/a/24386751/732396

    即使sklearn.feature_extraction.text.ENGLISH_STOP_WORDS 是一个frozenset,您也可以复制它并添加自己的单词,然后将该变量作为列表传递给stop_words 参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2012-05-09
      相关资源
      最近更新 更多