在scikitlearn中不需要删除一些停用词

时间:2016-01-08 11:44:35

标签: python scikit-learn text-mining text-analysis

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

1 个答案:

答案 0 :(得分:1)

这是因为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}