2015-03-06 14 views
10

Mam zainstalowany python 2.7, numpy 1.9.0, scipy 0.15.1 i scikit-learn 0.15.2. Teraz, kiedy należy wykonać następujące czynności w Pythonie:CountVectorizer nie drukuje słówek

train_set = ("The sky is blue.", "The sun is bright.") 
test_set = ("The sun in the sky is bright.", 
"We can see the shining sun, the bright sun.") 

from sklearn.feature_extraction.text import CountVectorizer 
vectorizer = CountVectorizer() 

print vectorizer 


    CountVectorizer(analyzer=u'word', binary=False, charset=None, 
    charset_error=None, decode_error=u'strict', 
    dtype=<type 'numpy.int64'>, encoding=u'utf-8', input=u'content', 
    lowercase=True, max_df=1.0, max_features=None, min_df=1, 
    ngram_range=(1, 1), preprocessor=None, stop_words=None, 
    strip_accents=None, token_pattern=u'(?u)\\b\\w\\w+\\b', 
    tokenizer=None, vocabulary=None) 

    vectorizer.fit_transform(train_set) 
    print vectorizer.vocabulary 

    None. 

Właściwie to powinno być wydrukowane następujące:

CountVectorizer(analyzer__min_n=1, 
analyzer__stop_words=set(['all', 'six', 'less', 'being', 'indeed', 'over',  
'move', 'anyway', 'four', 'not', 'own', 'through', 'yourselves', (...) --->  
For count vectorizer 

{'blue': 0, 'sun': 1, 'bright': 2, 'sky': 3} ---> for vocabulary 

Powyższy kod pochodzą z bloga: http://blog.christianperone.com/?p=1589

mógłbyś pomóc mi, dlaczego mam taki błąd. Ponieważ słownictwo nie jest odpowiednio indeksowane, nie jestem w stanie posunąć naprzód zrozumienia pojęcia TF-IDF. Jestem początkującym dla Pythona, więc każda pomoc byłaby doceniana.

Łuk.

Odpowiedz

13

Brakuje podkreślenia, spróbuj w ten sposób:

from sklearn.feature_extraction.text import CountVectorizer 
train_set = ("The sky is blue.", "The sun is bright.") 
test_set = ("The sun in the sky is bright.", 
    "We can see the shining sun, the bright sun.") 

vectorizer = CountVectorizer(stop_words='english') 
document_term_matrix = vectorizer.fit_transform(train_set) 
print vectorizer.vocabulary_ 
# {u'blue': 0, u'sun': 3, u'bright': 1, u'sky': 2} 

Jeśli używasz powłoki ipython, można użyć zakończenie kartę, i można znaleźć łatwiejsze metody i atrybuty obiektów.

+0

Tak. Dzięki. Spróbuję użyć powłoki ipython, aby nie przegapić takiego uzupełnienia. Mam powłokę ipython nigdy o tym nie wiedziałem. Dzięki za informację. – Archana

+0

W powyższym pytaniu zapytałem także, dlaczego moje słowa stop = Brak w CountVectorize, które nie powinny mieć miejsca. – Archana

+1

Domyślną wartością dla stop_words jest Brak. Możesz stworzyć vectorizer w ten sposób: vectorizer = CountVectorizer (stop_words = 'english'), jeśli chcesz używać wbudowanych angielskich słów stop. –

2

Spróbuj użyć metody vectorizer.get_feature_names(). Podaje nazwy kolumn w kolejności, w jakiej pojawiają się w document_term_matrix.

from sklearn.feature_extraction.text import CountVectorizer 
train_set = ("The sky is blue.", "The sun is bright.") 
test_set = ("The sun in the sky is bright.", 
    "We can see the shining sun, the bright sun.") 

vectorizer = CountVectorizer(stop_words='english') 
document_term_matrix = vectorizer.fit_transform(train_set) 
vectorizer.get_feature_names() 
#> ['blue', 'bright', 'sky', 'sun']