我已经创建了一个用于欺诈检测的机器学习模型,代码片段如下:
实际模型代码的一个小片段如下:
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匹配。