2015-03-06 14 views
10

Tôi đã cài đặt python 2.7, gumpy 1.9.0, scipy 0.15.1 và scikit-learn 0.15.2. Bây giờ khi tôi làm như sau trong python:CountVectorizer không in từ vựng

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. 

Trên thực tế nó nên đã in như sau:

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 

Đoạn mã trên là từ blog: http://blog.christianperone.com/?p=1589

Ông có thể vui lòng giúp đỡ tôi là lý do tại sao tôi nhận được một lỗi như vậy. Vì từ vựng không được lập chỉ mục đúng cách nên tôi không thể tiến lên trong việc hiểu khái niệm về TF-IDF. Tôi là một newbie cho python để giúp đỡ bất kỳ sẽ được đánh giá cao.

Arc.

Trả lời

13

Bạn đang thiếu một dấu gạch dưới, hãy thử cách này:

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} 

Nếu bạn sử dụng vỏ ipython, bạn có thể sử dụng hoàn tab, và bạn có thể tìm thấy dễ dàng hơn các phương pháp và các thuộc tính của các đối tượng.

+0

Có. Cảm ơn. Tôi sẽ thử sử dụng vỏ ipython để tôi không bỏ lỡ việc hoàn thành tab đó. Tôi có vỏ ipython không bao giờ biết điều này. Cảm ơn vì thông tin. – Archana

+0

Ở trên tôi cũng hỏi làm thế nào đến từ dừng của tôi = Không có trong CountVectorize mà không phải là trường hợp. – Archana

+1

Giá trị mặc định cho stop_words là None. Bạn có thể tạo vectơ như thế này: vectorizer = CountVectorizer (stop_words = 'english') nếu bạn muốn sử dụng các từ dừng tiếng Anh được xây dựng. –

2

Thử sử dụng phương thức vectorizer.get_feature_names(). Nó cung cấp cho các tên cột theo thứ tự nó xuất hiện trong 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'] 
Các vấn đề liên quan