不需要删除 scikit 学习中的一些停用词

Unwanted removal of some stopwords in scikitlearn

我想在向量中保留单个字符。在 scikit-learn CountVectorizer 中,我保留了 stop_word 参数,因为 None 内部实现正在从新创建的向量中删除一些字符。如何处理?

那是因为token_pattern参数默认为'(?u)\b\w\w+\b',过滤所有单词(前提是参数analyzer设置为'word',这是默认的)仅包含一个字符(例如 'a' 或 'i')。如果您将 token_pattern 设置为不同的正则表达式,例如'(?u)\b\w+\b'单字词要保留

示例:

In [71]: from sklearn.feature_extraction.text import CountVectorizer
In [72]: corpus = ['I like my coffee with a shot of rum.']

In [73]: vec = CountVectorizer()
In [74]: vec.fit(corpus)
In [75]: vec.vocabulary_

Out[75]: {'coffee': 0, 'like': 1, 'my': 2, 'of': 3, 'rum': 4, 'shot': 5, 'with': 6}

In [76]: vec = CountVectorizer(token_pattern='(?u)\b\w+\b')
In [77]: vec.fit(corpus) 
In [78]: vec.vocabulary_
Out[78]: {'a': 0, 'coffee': 1, 'i': 2, 'like': 3, 'my': 4, 'of': 5, 'rum': 6, 'shot': 7, 'with': 8}