Подтвердить что ты не робот

CountVectorizer не печатает словарный запас

Я установил python 2.7, numpy 1.9.0, scipy 0.15.1 и scikit-learn 0.15.2. Теперь, когда я делаю следующее в python:

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.

На самом деле он должен был напечатать следующее:

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

Вышеприведенный код приведен в блоге: http://blog.christianperone.com/?p=1589

Не могли бы вы помочь мне в том, почему я получаю такую ​​ошибку. Поскольку словарь не проиндексирован должным образом, я не могу продвинуться в понимании концепции TF-IDF. Я новичок в python, поэтому любая помощь будет оценена.

Arc.

4b9b3361

Ответ 1

Вам не хватает подчеркивания, попробуйте следующим образом:

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}

Если вы используете оболочку ipython, вы можете использовать табуляцию, и вы можете легче найти методы и атрибуты объектов.

Ответ 2

Попробуйте использовать метод vectorizer.get_feature_names(). Он дает имена столбцов в порядке, указанном в 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']