-1
我想在我的矢量中保留單個字符。在scikit-learn CountVectorizer
甚至,我保持stop_word
參數爲None
內部實現是從新創建的向量中刪除一些字符。如何處理?Scikitlearn中的一些停用詞的無用移除
我想在我的矢量中保留單個字符。在scikit-learn CountVectorizer
甚至,我保持stop_word
參數爲None
內部實現是從新創建的向量中刪除一些字符。如何處理?Scikitlearn中的一些停用詞的無用移除
這是因爲token_pattern
參數默認爲'(?u)\\b\\w\\w+\\b'
,其過濾的所有單詞(提供的參數analyzer
設置爲'word'
,這是默認值),只有由單個字符(例如「一」或「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}