问题
I'm new to machine learning world and working on a project in which I have trained a model for fraud detection using SKlearn.I'm training the model in this way 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)
}
# 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)
scores_pred = clf.negative_outlier_factor_
else:
clf.fit(X)
scores_pred = clf.decision_function(X)
y_pred = clf.predict(X)
# 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 ))
And it returns the following output:
Isolation Forest:7
0.93
precision recall f1-score support
0 0.97 0.96 0.96 95
1 0.33 0.40 0.36 5
avg / total 0.94 0.93 0.93 100
Local Outlier Factor:9
0.91
precision recall f1-score support
0 0.96 0.95 0.95 95
1 0.17 0.20 0.18 5
avg / total 0.92 0.91 0.91 100
Now the confusing part is it's deployment, after struggling a lot I have decided to deploy and serve it with the help of a flask web service, but don't understand how I can pass the input to my predict
method here to get a prediction?
Is there something have been done in a wrong way?
How can I pass the input to my predict
method here?
Help me, please! Any resource for step by step guide to its deployment to google cloud will be very appreciated.
Thanks in advance!
来源:https://stackoverflow.com/questions/49839270/prepare-sklearn-model-with-isolation-forest-to-deploy-on-google-cloud