使用Isolation Forest准备SKlearn模型以在Google Cloud上部署

时间:2018-04-15 06:54:25

标签: python machine-learning scikit-learn machine-learning-model

我是机器学习世界的新手,并且正在开展一个项目,我使用SKlearn训练了一个欺诈检测模型。我以这种方式训练模型:

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 ))

它返回以下输出:

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

现在令人困惑的部分是它的部署,经过艰苦的努力后,我决定在烧瓶网络服务的帮助下部署和服务它,但不知道如何将输入传递给我的predict方法可以预测吗?

是否有某些事情以错误的方式完成?

如何在此处将输入传递给我的predict方法?

请帮帮我! 任何资源一步步指导其部署到谷歌云将非常感激。

提前致谢!

0 个答案:

没有答案