首页 > 解决方案 > Cloud ML Engine 和 Scikit-Learn:“LatentDirichletAllocation”对象没有“predict”属性

问题描述

我正在实现简单的 Scikit-LearnPipelineLatentDirichletAllocation在 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 生成本地预测时,我收到错误提示LatentDirichletAllocationhas 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 一起使用(或类似的)?

标签: pythonmachine-learningscikit-learntext-classificationgoogle-cloud-ml

解决方案


目前,管道的最后一个估计器必须实现该predict方法。


推荐阅读