python - 检查“已安装的 sk-learn”管道仍会导致“尚未安装 TFIdfVectorizer”
问题描述
这是我对 sk-learn 管道的不安全感。每当我在 sk-learn 中创建一个流水线并用这个流水线做一些预测时,我似乎遇到了一个问题,即我实际上无法检查流水线的中间步骤。预测有效,我得到了我的分数,但是如果我想获得实例的“特征重要性”,或者检查 tf-idf 矢量化器的特征是什么,则声称该管道不适合(尽管它只是最近用于推理,我已经要求对其进行培训)。
举个例子,.fit()
从这里调用 Scikit-learn 文档中的以下片段可用于预测,但是当我想检查管道的 tfidf 时,它声称同样的不合适的问题。
pipeline = Pipeline([
# Extract the subject & body
('subjectbody', SubjectBodyExtractor()),
# Use ColumnTransformer to combine the features from subject and body
('union', ColumnTransformer(
[
# Pulling features from the post's subject line (first column)
('subject', TfidfVectorizer(min_df=50), 0),
# Pipeline for standard bag-of-words model for body (second column)
('body_bow', Pipeline([
('tfidf', TfidfVectorizer()),
('best', TruncatedSVD(n_components=50)),
]), 1),
# Pipeline for pulling ad hoc features from post's body
('body_stats', Pipeline([
('stats', TextStats()), # returns a list of dicts
('vect', DictVectorizer()), # list of dicts -> feature matrix
]), 1),
],
# weight components in ColumnTransformer
transformer_weights={
'subject': 0.8,
'body_bow': 0.5,
'body_stats': 1.0,
}
)),
# Use a SVC classifier on the combined features
('svc', LinearSVC(dual=False)),
], verbose=True)
在数据上拟合管道之后(如链接中所做的那样),当我尝试使用
pipeline.named_steps.union.transformers[1][1].named_steps['tfidf'].get_feature_names()
它声称“未安装或提供词汇表”。
那么,这是我对管道的误解吗?我们不应该访问中间步骤吗?或者可能需要设置一个设置?
解决方案
您需要通过.transformers_
So访问变压器pipeline.named_steps.union.transformers_[1][1].named_steps['tfidf'].get_feature_names()
推荐阅读
- tfs - 字段“状态”包含不在支持值列表中的值“已批准”
- java-8 - Java 8 中的终端操作 sum() 在哪里实现?
- javascript - 如何使用 Kendo.Button() 的 Events 方法将模型的属性发送到 JavaScript 函数
- angular - Angular 2+从视图目标到数据源的一种方式数据绑定
- python - 调用类方法或返回 None
- arm - 什么指令修改了 CPSR 寄存器中的状态标志?
- bash - 使用来自 php 的 shell 将文本附加到文件
- c# - WebRequest.GetResponseAsync() 在 WebRequest.GetResponse() 成功的地方失败
- javascript - 如何在 reactJS 中呈现条件形式?
- java - 如何使用 TouchActions appium 方法滚动页面?