2015-10-12 20 views
5

Tôi đang sử dụng một đường ống rất giống với một trao in this example:lấy tính năng trung gian từ một đường ống dẫn trong Scikit (Python)

>>> text_clf = Pipeline([('vect', CountVectorizer()), 
...      ('tfidf', TfidfTransformer()), 
...      ('clf', MultinomialNB()), 
... ]) 

qua mà tôi sử dụng GridSearchCV để tìm các ước lượng tốt nhất trên một mạng lưới tham số.

Tuy nhiên, tôi muốn lấy tên cột của bộ đào tạo của mình bằng phương pháp get_feature_names() từ CountVectorizer(). Điều này có thể thực hiện được nếu không triển khai CountVectorizer() ngoài đường ống không?

Trả lời

6

Sử dụng chức năng get_params(), bạn có thể truy cập ở các phần khác nhau của đường ống và các thông số nội bộ tương ứng của chúng. Dưới đây là một ví dụ về việc tiếp cận 'vect'

text_clf = Pipeline([('vect', CountVectorizer()), 
        ('tfidf', TfidfTransformer()), 
        ('clf', MultinomialNB())] 
print text_clf.get_params()['vect'] 

năng suất (đối với tôi)

CountVectorizer(analyzer=u'word', binary=False, 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) 

Tôi chưa lắp đường ống để bất kỳ dữ liệu trong ví dụ này, vì vậy gọi get_feature_names() vào thời điểm này sẽ trả về một lỗi.

2

chỉ để tham khảo

The estimators of a pipeline are stored as a list in the steps attribute: 
>>> 

>>> clf.steps[0] 
('reduce_dim', PCA(copy=True, n_components=None, whiten=False)) 

and as a dict in named_steps: 
>>> 

>>> clf.named_steps['reduce_dim'] 
PCA(copy=True, n_components=None, whiten=False) 

từ http://scikit-learn.org/stable/modules/pipeline.html

Các vấn đề liên quan