Python 从Google Cloud ML引擎上部署的SCIKITLEARN模型进行预测

Python 从Google Cloud ML引擎上部署的SCIKITLEARN模型进行预测,python,machine-learning,scikit-learn,prediction,google-cloud-ml,Python,Machine Learning,Scikit Learn,Prediction,Google Cloud Ml,我创建了欺诈检测的ml模型,如下所示: 实际模型代码的一小部分如下: from sklearn.metrics import classification_report, accuracy_score from sklearn.ensemble import IsolationForest from sklearn.neighbors import LocalOutlierFactor # define a random state state = 1 # define the outlie

我创建了欺诈检测的ml模型,如下所示:

实际模型代码的一小部分如下:

from sklearn.metrics import classification_report, accuracy_score
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor

# define a random state
state = 1

# define the outlier detection method
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)
        # Export the classifier to a file
        with open('model.pkl', 'wb') as model_file:
            pickle.dump(clf, model_file)
        scores_pred = clf.negative_outlier_factor_
    else:
        clf.fit(X)
        scores_pred = clf.decision_function(X)
        y_pred = clf.predict(X)
        # 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 ))
我已经在Google云平台上成功创建了一个存储桶、ml模型和一个版本。 但是作为一个ml世界的初学者,我很困惑,当这个模型现在部署在Google的ml引擎上时,我如何将输入传递给这个模型以获得真正的预测

更新:正如N3da的回答中所述,现在我使用以下代码进行在线预测:

但它返回的访问错误为:

GoogleAppClient.errors.HttpError:https://ml.googleapis.com/v1/projects/PROJECT_ID/models/MODEL_NAME/versions/VERSION:predict?alt=json 返回“拒绝访问模型”。>


请帮帮我

成功创建
版本后,您可以使用
gcloud
工具或发送http请求以获取在线预测。从中,以下是从python代码发送http请求的示例:

service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(PROJECT_ID, MODEL_NAME)
name += '/versions/{}'.format(VERSION_NAME)

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

if 'error' in response:
    print (response['error'])
else:
  online_results = response['predictions']
上述示例中的
data
是一个列表,其中每个元素都是模型接受的实例。是有关预测请求和响应的更多信息

更新:
对于您提到的权限问题,了解您最初创建模型和版本的方式/位置(通过gcloud、UI控制台、笔记本电脑等)会有所帮助。错误消息表明您的用户可以访问您的项目,但不能访问模型。尝试从运行python代码的任何位置运行
gcloud auth login
,并确认显示为默认项目的项目与您的项目ID匹配。

您能否验证模型是否存在于gcs存储桶中,并且您的用户可以访问该模型?附加(和不相关的)注意:在上面的代码中,您可能只需要执行:
data=[[265580,768728,8.36,4.76,84.12,79.36,3346,1,11.99,1.14655012,0.65,258374,0,84.12]
下面几行(
body={'instances':data}
)所以你不需要两次。请看下面的n3das更新@rhaertel80,你是对的,我已经更新了我的代码。嗨@N3da,你能看看这个问题吗:,请?回答(希望如此)
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(PROJECT_ID, MODEL_NAME)
name += '/versions/{}'.format(VERSION_NAME)

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

if 'error' in response:
    print (response['error'])
else:
  online_results = response['predictions']