【问题标题】:How to add stop words from Tfidvectorizer?如何从 Tfidfvectorizer 添加停用词?
【发布时间】:2021-07-24 05:34:55
【问题描述】:

我正在尝试将停用词添加到我的停用词列表中,但是,我使用的代码似乎不起作用:

创建停用词列表:

stopwords = nltk.corpus.stopwords.words('english')

CustomListofWordstoExclude = ['rt']

stopwords1 = stopwords.extend(CustomListofWordstoExclude)

在这里,我将文本转换为具有 tfidf 权重的 dtm(文档术语矩阵):

vect = TfidfVectorizer(stop_words = 'english', min_df=150, token_pattern=u'\\b[^\\d\\W]+\\b')

dtm = vect.fit_transform(df['tweets'])

dtm.shape

但是当我这样做时,我得到了这个错误:

FutureWarning:将 input=None 作为关键字参数传递。从 0.25 版开始,将这些作为位置参数传递将导致错误 warnings.warn("将 {} 作为关键字参数传递。从 0.25 版开始"

这是什么意思?有没有更简单的方法来添加停用词?

【问题讨论】:

    标签: python-3.x stop-words


    【解决方案1】:

    我无法重现该警告。但是,请注意,这样的警告并不意味着您的代码没有按预期运行。这意味着在该软件包的未来版本中,它可能无法按预期工作。所以如果你明年用更新的包尝试同样的事情,它可能不会奏效。

    关于您关于使用停用词的问题,需要进行两项更改才能使您的代码按预期工作。

    1. list.extend() 就地扩展列表,但不返回列表。要看到这一点,您可以执行type(stopwords1),它会给出NoneType。要在一行中定义一个新变量并将自定义单词列表添加到stopwords,您可以使用列表的内置+ 运算符功能:
    stopwords = nltk.corpus.stopwords.words('english')
    CustomListofWordstoExclude = ['rt']
    stopwords1 = stopwords + CustomListofWordstoExclude
    
    1. 要在执行 TF-IDF 矢量化时实际使用 stopwords1 作为新的停用词列表,您需要传递 stop_words=stopwords1
    vect = TfidfVectorizer(stop_words=stopwords1,  # Passed stopwords1 here
                           min_df=150, 
                           token_pattern=u'\\b[^\\d\\W]+\\b')
    dtm = vect.fit_transform(df['tweets'])
    dtm.shape
    

    【讨论】:

      猜你喜欢
      • 2012-10-15
      • 2015-01-05
      • 1970-01-01
      • 2014-01-22
      • 2017-07-15
      • 2021-05-11
      • 2018-08-07
      • 2019-07-29
      • 1970-01-01
      相关资源
      最近更新 更多