【问题标题】:'Pipeline' object is not subscriptable“管道”对象不可下标
【发布时间】: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']。

标签: python-3.x scikit-learn


【解决方案1】:

您可以尝试pipe.named_steps['count'],而不是pipe['count']。要访问您的'tfidf' 步骤,请尝试pipe.named_steps['tfid']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2013-09-22
    • 2020-02-17
    • 2021-11-10
    • 2019-05-16
    • 2016-07-20
    相关资源
    最近更新 更多