【发布时间】:2020-04-17 19:57:39
【问题描述】:
我正在尝试运行以下代码,但在执行 pipe['count'] 时出现“管道”对象不可下标”错误。
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
import numpy as np
corpus = ['this is the first document',
'this document is the second document',
'and this is the third one',
'is this the first document']
vocabulary = ['this', 'document', 'first', 'is', 'second', 'the',
'and', 'one']
pipe = Pipeline([('count', CountVectorizer(vocabulary=vocabulary)),
('tfid', TfidfTransformer())]).fit(corpus)
pipe['count'].transform(corpus).toarray()
array([[1, 1, 1, 1, 0, 1, 0, 0],
[1, 2, 0, 1, 1, 1, 0, 0],
[1, 0, 0, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 0, 0]])
pipe['tfid'].idf_
array([1. , 1.22314355, 1.51082562, 1. , 1.91629073,
1. , 1.91629073, 1.91629073])
pipe.transform(corpus).shape
(4, 8)```
【问题讨论】:
-
所以你在第 19 行出现错误,
pipe['count']...。第 20 行是什么,是您期望的输出吗? -
是的。但这有点无关紧要。我只想访问 Pipeline 对象的一个组件,在本例中是 pipeline['count']。