I have created a ml model for fraud detection as:
A little snippet of the actual model code as:
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 ))
I have created a storage bucket, ml model, and a version successfully on Google Cloud Platform. But as a beginner to ml world, I'm confused, how I can pass the input to this model to get a real prediction as this model is deployed on Google's ML-Engine now.
Update: As stated in the N3da's answer, now I'm using this code for online prediction:
import os
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
PROJECT_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)
But it returns access error as:
googleapiclient.errors.HttpError: https://ml.googleapis.com/v1/projects/PROJECT_ID/models/MODEL_NAME/versions/VERSION:predict?alt=json returned "Access to model denied.">
Help me, please!
Once you have successfully created a Version, you can either use gcloud tool or send http requests to get online predictions. From this, here is an example of sending http requests from python code:
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 in the above example would be a list where each element is an instance that your model accepts. Here is more info about the prediction request and response.
Update:
For the permission issue you mentioned, it would help to know how/where you created the model and versions from initially (via gcloud, UI console, on your laptop, etc.) The error message suggests that your user has access to your project, but not the model. Try running gcloud auth login from wherever you are running your python code and confirm that the project it shows as the default project matches your PROJECT_ID.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With