text_plain_output_1.png image_output_1.png
from sklearn.ensemble import IsolationForest

from sklearn.metrics import classification_report,accuracy_score

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('../input/creditcard.csv')

fraud_ratio = float(data['Class'][data['Class'] == 1].shape[0]) / data.shape[0]

X = data.iloc[:, :-1]
Y = data['Class']

(Y.shape, X.shape)

classifiers = {'Isolation Forest': IsolationForest(n_estimators=100, max_samples=len(X))}

n_outliers = 492
for i, (clf_name, clf) in enumerate(classifiers.items()):
    if clf_name == 'Local Outlier Factor':
        y_pred = clf.fit_predict(X)
        scores_prediction = clf.negative_outlier_factor_
    elif clf_name == 'Support Vector Machine':
        clf.fit(X)
        y_pred = clf.predict(X)
    else:
        clf.fit(X)
        scores_prediction = clf.decision_function(X)
        y_pred = clf.predict(X_test)
    y_pred[y_pred == 1] = 0
    y_pred[y_pred == -1] = 1
    n_errors = (y_pred != Y_test).sum()
    print('{}: {}'.format(clf_name, n_errors))
    print('Accuracy Score :')
    print(accuracy_score(Y_test, y_pred))
    print('Classification Report :')
    print(classification_report(Y_test, y_pred))