Python ScikitLearn模型给出';局部异常因子';对象没有属性';预测';错误

Python ScikitLearn模型给出';局部异常因子';对象没有属性';预测';错误,python,scikit-learn,google-cloud-platform,google-cloud-ml,Python,Scikit Learn,Google Cloud Platform,Google Cloud Ml,我是机器学习领域的新手,我使用ScikitLearn库构建并训练了一个ml模型。它在Jupyter笔记本中运行得非常好,但当我将此模型部署到Google Cloud ml并尝试使用Python脚本为其提供服务时,它抛出了一个错误 下面是我的模型代码的一个片段: 更新: 以下是Jupyter笔记本中的输出: 隔离林:7 0.93 precision recall f1-score support 0 0.97 0

我是机器学习领域的新手,我使用ScikitLearn库构建并训练了一个ml模型。它在Jupyter笔记本中运行得非常好,但当我将此模型部署到Google Cloud ml并尝试使用Python脚本为其提供服务时,它抛出了一个错误

下面是我的模型代码的一个片段:

更新:

以下是Jupyter笔记本中的输出:

隔离林:7

0.93

               precision    recall  f1-score   support


         0       0.97      0.96      0.96        94
         1       0.43      0.50      0.46         6

  avg / total    0.94      0.93      0.93       100
我已将此模型部署到Google Cloud ML引擎,然后尝试使用以下python脚本为其提供服务:

import os
from googleapiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials
credentials = ServiceAccountCredentials.from_json_keyfile_name('Machine Learning 001-dafe42dfb46f.json')

PROJECT_ID = "machine-learning-001-201312"
VERSION_NAME = "v1"
MODEL_NAME = "mlfd"
service = discovery.build('ml', 'v1', credentials=credentials)
name = 'projects/{}/models/{}'.format(PROJECT_ID, MODEL_NAME)
name += '/versions/{}'.format(VERSION_NAME)

data = [[265580, 7, 68728, 8.36, 4.76, 84.12, 79.36, 3346, 1, 11.99, 1.14,655012, 0.65, 258374, 0, 84.12] ]

response = service.projects().predict(
    name=name,
    body={'instances': data}
).execute()

if 'error' in response:
  print (response['error'])
else:
  online_results = response['predictions']
  print(online_results)
以下是此脚本的输出:

预测失败:sklearn预测期间出现异常:“LocalOutlierFactor”对象没有属性“predict”


LocalOutlierFactor
没有
predict
方法,只有private
\u predict
方法。以下是来源的理由

def _predict(self, X=None):
    """Predict the labels (1 inlier, -1 outlier) of X according to LOF.
    If X is None, returns the same as fit_predict(X_train).
    This method allows to generalize prediction to new observations (not
    in the training set). As LOF originally does not deal with new data,
    this method is kept private.

看起来这可能是Python版本的事情(尽管我不清楚为什么scikit learn在Python 2和Python 3中的行为不同)。我能够在同一台机器上本地验证我的Python2安装是否复制了上述错误,而Python3成功了(两者都使用sci kit learn 0.19.1)

解决方案是在部署模型时指定python版本(注意最后一行,如果省略,则默认为“2.7”):


令人惊讶的是,问题在于运行时版本,当您将模型版本重新创建为:

gcloud beta ml-engine versions create $VERSION_NAME  --model $MODEL_NAME --origin $DEPLOYMENT_SOURCE --runtime-version="1.6" --framework $FRAMEWORK --python-version="3.5"
使用运行时版本1.6而不是1.5,至少将其转换为运行模型


我从事的一个项目似乎非常相似。我也犯了同样的错误。我的问题是if语句中的一个输入错误

问候
Lorenz

Hi@Bert,你能给我提供源代码的链接吗?请那么,我该如何将其集成到我的模型中呢?嗨@Bert,这里还有一个让我困惑的地方,我正在使用
隔离林
分类器和它的
预测
方法,那么为什么它指向
局部异常因子
?对不起,我不熟悉Google Cloud ML引擎。可能您有以前运行的遗留LOF模型或代码,或者没有完全部署IF。不过,它确实试图在LocalOutlierFactor对象上调用predict()。嗨@Bert,现在我已经从我的模型中删除了te=he“Local Outlier Factor”,并生成了一个新的
pickle
文件并再次上传它,但我仍然得到了相同的错误。上面显示的python脚本,在任何地方都不要使用
LocalOutlierFactor
。你确定你在谷歌上使用的是同一个脚本吗?为什么要更改代码?只需在google ml上发布内容。google ml仍然返回相同的错误。我已经用
Python 3.5
创建了我的模型版本,也用
Python 3.6
编写了我的模型代码,只是为了验证:您使用--Python version=“3.5”创建了模型并且仍然可以得到报告的错误消息?有可能您更新的代码实际上并不是部署在Google Cloud ML engine中的代码(主要是因为错误消息指的是在代码中注释掉的类)。您是否可以尝试将您的新模型(仅具有
IsolationForest
分类器)上载到
gcs
并尝试使用上述命令重新部署它?这并不提供问题的答案,如果您可以识别特定的打字错误,请在您的答案中指出。
gcloud beta ml-engine versions create $VERSION_NAME \
    --model $MODEL_NAME --origin $DEPLOYMENT_SOURCE \
    --runtime-version="1.5" --framework $FRAMEWORK
    --python-version="3.5"
gcloud beta ml-engine versions create $VERSION_NAME  --model $MODEL_NAME --origin $DEPLOYMENT_SOURCE --runtime-version="1.6" --framework $FRAMEWORK --python-version="3.5"