2015-10-21 11 views
5

Perché CountVectorizer in sklearn ignora il pronome "I"?CountVectorizer che ignora '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'] 

risposta

7

Il tokenizzatore predefinito considera solo parole di 2 caratteri (o più).

È possibile modificare questo comportamento passando uno token_pattern appropriato al numero CountVectorizer.

Il modello di default è (vedi the signature in the docs):

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

È possibile ottenere un CountVectorizer che non scende a una lettera le parole modificando l'impostazione predefinita, per esempio:

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()) 

che dà :

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