Cloud ML Engine和Scikit-Learn:“ LatentDirichletAllocation”对象没有属性“ predict”

时间:2018-07-23 14:03:17

标签: python machine-learning scikit-learn text-classification google-cloud-ml

我正在实现简单的Scikit-Learn Pipeline,以便在Google Cloud ML Engine中执行LatentDirichletAllocation。目标是根据新数据预测主题。这是生成管道的代码:

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没有属性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

现在的问题是:要走的路是什么?如何在带有Google Cloud ML Engine的Scikit-Learn管道中使用LatentDirichletAllocation(或类似名称)?

1 个答案:

答案 0 :(得分:3)

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