python - Cloud ML Engine 和 Scikit-Learn:“LatentDirichletAllocation”对象没有“predict”属性
问题描述
我正在实现简单的 Scikit-LearnPipeline
以LatentDirichletAllocation
在 Google Cloud ML Engine 中执行。目标是从新数据中预测主题。这是生成管道的代码:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.datasets import fetch_20newsgroups
dataset = fetch_20newsgroups(shuffle=True, random_state=1,
remove=('headers', 'footers', 'quotes'))
train, test = train_test_split(dataset.data[:2000])
pipeline = Pipeline([
('CountVectorizer', CountVectorizer(
max_df = 0.95,
min_df = 2,
stop_words = 'english')),
('LatentDirichletAllocation', LatentDirichletAllocation(
n_components = 10,
learning_method ='online'))
])
pipeline.fit(train)
现在(如果我理解正确的话)预测我可以运行的测试数据的主题:
pipeline.transform(test)
但是,当将管道上传到 Google Cloud Storage 并尝试使用它通过 Google Cloud ML Engine 生成本地预测时,我收到错误提示LatentDirichletAllocation
has no attribute predict
。
gcloud ml-engine local predict \
--model-dir=$MODEL_DIR \
--json-instances $INPUT_FILE \
--framework SCIKIT_LEARN
...
"Exception during sklearn prediction: " + str(e)) cloud.ml.prediction.prediction_utils.PredictionError: Failed to run the provided model: Exception during sklearn prediction: 'LatentDirichletAllocation' object has no attribute 'predict' (Error code: 2)
从文档中也可以看出缺乏预测方法,所以我想这不是解决这个问题的方法。 http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.LatentDirichletAllocation.html
现在的问题是:要走的路是什么?如何LatentDirichletAllocation
在 Scikit-Learn Pipelines 中与 Google Cloud ML Engine 一起使用(或类似的)?
解决方案
目前,管道的最后一个估计器必须实现该predict
方法。
推荐阅读
- python - 如何在pygame中存储对象的先前x位置(坐标)?
- linux - 如何在 Ubuntu 18.04 WSL 中安装最新版本的 NuGet.exe CLI?
- javascript - 移动视图 jquery 上的 Bootstrap 3 Sidebar Navgivator
- python - 想要对 while 循环进行矢量化以提高性能
- c# - 如何放置 Hybrunner 对象以加快代码速度
- python - test_small_numbers:失败
- python - 在 IF 语句中使用正则表达式列表中的单词
- powershell - 用正则表达式和查找表匹配替换
- cypress - 如何从没有 id 或类的选择表单中 cy.get
- ruby-on-rails-4 - 带有rails 6和webpacker的HTML Partials未加载