使用Google Cloud ML-Engine部署的SCIKITLEARN模型进行预测

我已经创建了一个用于欺诈检测的机器学习模型,代码片段如下:

实际模型代码的一个小片段如下:

from sklearn.metrics import classification_report, accuracy_scorefrom sklearn.ensemble import IsolationForestfrom sklearn.neighbors import LocalOutlierFactor# 定义一个随机状态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# 拟合模型n_outliers = len(Fraud)for i, (clf_name, clf) in enumerate(classifiers.items()):    # 拟合数据并标记异常值    if clf_name == "Local Outlier Factor":        y_pred = clf.fit_predict(X)        # 将分类器导出到文件        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)        # 将分类器导出到文件        with open('model.pkl', 'wb') as model_file:            pickle.dump(clf, model_file)    # 将预测值重塑为0表示有效,1表示欺诈    y_pred[y_pred == 1] = 0    y_pred[y_pred == -1] = 1    n_errors = (y_pred != Y).sum()    # 运行分类指标    print('{}:{}'.format(clf_name, n_errors))    print(accuracy_score(Y, y_pred ))    print(classification_report(Y, y_pred ))

我已经在Google Cloud Platform上成功创建了存储桶、机器学习模型和版本。但是,作为机器学习的新手,我感到困惑的是,现在模型已经部署在Google的ML-Engine上,我该如何向模型传递输入以获得真实的预测结果呢?

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

import osfrom googleapiclient import discoveryfrom oauth2client.client import GoogleCredentialsPROJECT_ID = "PROJECT_ID"VERSION_NAME = "VERSION"MODEL_NAME = "MODEL_NAME"credentials = GoogleCredentials.get_application_default()service = discovery.build('ml', 'v1', credentials=credentials)name = 'projects/{}/models/{}'.format(PROJECT_ID, MODEL_NAME)name += '/versions/{}'.format(VERSION_NAME)data = {    "instances": [      [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)

但是它返回访问错误,如下所示:

googleapiclient.errors.HttpError: https://ml.googleapis.com/v1/projects/PROJECT_ID/models/MODEL_NAME/versions/VERSION:predict?alt=json returned “Access to model denied.”>

请帮助我!


回答:

一旦您成功创建了一个版本,您可以使用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,并确认显示为默认项目的项目与您的PROJECT_ID匹配。

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注