首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >给出“ScikitLearn”对象的LocalOutlierFactor模型没有属性“预测”错误

给出“ScikitLearn”对象的LocalOutlierFactor模型没有属性“预测”错误
EN

Stack Overflow用户
提问于 2018-04-25 03:56:48
回答 4查看 3.6K关注 0票数 1

我是机器学习世界的新手,我用ScikitLearn library.It构建并训练了一个ml模型,它在木星笔记本上运行得非常好,但是当我将这个模型部署到Google并尝试使用Python脚本提供服务时,它会抛出一个错误。

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

更新:

代码语言:javascript
运行
复制
from sklearn.metrics import classification_report, accuracy_score
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor

# define a random state
state = 1

classifiers = {
    "Isolation Forest": IsolationForest(max_samples=len(X),
                                       contamination=outlier_fraction,
                                       random_state=state),
    # "Local Outlier Factor": LocalOutlierFactor(
    # n_neighbors = 20,
    # contamination = outlier_fraction)
}

import pickle
# fit the model
n_outliers = len(Fraud)

for i, (clf_name, clf) in enumerate(classifiers.items()):

    # fit te data and tag outliers
    if clf_name == "Local Outlier Factor":
        y_pred = clf.fit_predict(X)
        print("LOF executed")
        scores_pred = clf.negative_outlier_factor_
        # Export the classifier to a file
        with open('model.pkl', 'wb') as model_file:
            pickle.dump(clf, model_file)
    else:
        clf.fit(X)
        scores_pred = clf.decision_function(X)
        y_pred = clf.predict(X)
        print("IF executed")
        # Export the classifier to a file
        with open('model.pkl', 'wb') as model_file:
            pickle.dump(clf, model_file)
    # Reshape the prediction values to 0 for valid and 1 for fraudulent
    y_pred[y_pred == 1] = 0
    y_pred[y_pred == -1] = 1

    n_errors = (y_pred != Y).sum()

# run classification metrics 
print('{}:{}'.format(clf_name, n_errors))
print(accuracy_score(Y, y_pred ))
print(classification_report(Y, y_pred ))

这是木星笔记本的输出:

隔离林:7 0.93 精确召回F1-得分支持0 0.97 0.96 0.96 94 1 0.43 0.50 0.46 6每克/总计0.94 0.93 0.93 100

我已经将这个模型部署到中,然后尝试使用以下python脚本提供服务:

代码语言:javascript
运行
复制
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)

下面是这个脚本的输出:

预测失败:学习过程中的异常预测:“LocalOutlierFactor”对象没有属性“预测”

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-04-25 04:39:39

LocalOutlierFactor没有predict方法,但只有私有_predict方法。以下是消息来源的理由。

代码语言:javascript
运行
复制
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.

https://github.com/scikit-learn/scikit-learn/blob/a24c8b46/sklearn/neighbors/lof.py#L200

票数 3
EN

Stack Overflow用户

发布于 2018-04-25 05:24:08

看起来这可能是Python版本的问题(虽然我不清楚为什么scikit在Python 2和Python 3中的行为有所不同)。我能够在本地--在同一台机器上--验证我的Python 2安装是否在Python 3成功时复制了上面的错误(两者都在使用sci学习0.19.1)。

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

代码语言:javascript
运行
复制
gcloud beta ml-engine versions create $VERSION_NAME \
    --model $MODEL_NAME --origin $DEPLOYMENT_SOURCE \
    --runtime-version="1.5" --framework $FRAMEWORK
    --python-version="3.5"
票数 0
EN

Stack Overflow用户

发布于 2018-04-25 07:17:22

令人惊讶的是,问题是runtime version,当您重新创建模型版本时,它将得到解决,如下所示:

代码语言:javascript
运行
复制
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,至少将其转换为运行模型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50013828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档