gpt4 book ai didi

python-3.x - “管道”对象不可订阅

转载 作者:行者123 更新时间:2023-12-04 12:40:47 24 4
gpt4 key购买 nike

我正在尝试运行以下代码,但在执行 pipe['count'] 时出现“Pipeline' object is not subscriptable' 错误。


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)```

最佳答案

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

关于python-3.x - “管道”对象不可订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59507657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com