2015-10-21 10 views
5

Dlaczego CountVectorizer w sklearn ignoruje zaimek "I"?CountVectorizer ignorując "I"

ngram_vectorizer = CountVectorizer(analyzer = "word", ngram_range = (2,2), min_df = 1) 
ngram_vectorizer.fit_transform(['HE GAVE IT TO I']) 
<1x3 sparse matrix of type '<class 'numpy.int64'>' 
ngram_vectorizer.get_feature_names() 
['gave it', 'he gave', 'it to'] 

Odpowiedz

7

Domyślny tokenizer uwzględnia tylko 2-znakowe (lub więcej) słowa.

Możesz zmienić to zachowanie, przekazując odpowiednie token_pattern do swojego CountVectorizer.

Domyślny wzór jest (patrz the signature in the docs):

'token_pattern': u'(?u)\\b\\w\\w+\\b' 

Można uzyskać CountVectorizer że nie spadnie jeden list słów poprzez zmianę domyślnego, na przykład:

from sklearn.feature_extraction.text import CountVectorizer 
ngram_vectorizer = CountVectorizer(analyzer="word", ngram_range=(2,2), 
            token_pattern=u"(?u)\\b\\w+\\b",min_df=1) 
ngram_vectorizer.fit_transform(['HE GAVE IT TO I']) 
print(ngram_vectorizer.get_feature_names()) 

Który daje :

['gave it', 'he gave', 'it to', 'to i']