import numpy as np
from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix
from sklearn.decomposition import PCA
from sklearn import random_projection
from sklearn import svm
from sklearn.ensemble import IsolationForest
import matplotlib.pyplot as plt
from keras.layers import Dense, Input, Dropout
from keras.models import Model
from keras import regularizers
from keras.models import Sequential
from keras.optimizers import Adam
from keras.regularizers import l2
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
import xgboost as xgb


def one_class_svm(x_train, x_test, x_attacks, svm_results):

    # SVM Hyper-parameters
    nus = [0.01]
    gammas = ['auto']
    dimensions = [int(i*x_test.shape[1]) for i in [0.25, 0.35, 0.5, 0.75, 0.9, 1]]
    dimensions = list(filter(lambda x: x > 0, dimensions))

    for n in dimensions:

        x_reduced_pca, test_reduced_pca, attack_reduced_pca = reduce_dimensionality(n, x_train, x_test, 'PCA',
                                                                                    attack=x_attacks)
  
        for nu in nus:
            for gamma in gammas:

                # Fit classifier with PCA reduced data
                classifier = svm.OneClassSVM(kernel='rbf', gamma=gamma, nu=nu, cache_size=7000)
                classifier.fit(x_reduced_pca)
                fb, area, tnr, tpr_train, tpr_test = unsupervised_evaluation(classifier, x_reduced_pca,
                                                                             test_reduced_pca,
                                                                             attack_reduced_pca)

                svm_results = svm_results.append({'nu': nu, 'gamma': gamma, 'n_components': n, 'TPR_train': tpr_train,
                                                  'TPR_test': tpr_test, 'TNR': tnr, 'model': 'svm', 'auc': area,
                                                  'f_beta': fb, 'projection': 'PCA'}, ignore_index=True)

                # Fit classifier with RP reduced data
                classifier = svm.OneClassSVM(kernel='rbf', gamma=gamma, nu=nu, cache_size=7000)

  
                classifier.fit(x_train)
                fb, area, tnr, tpr_train, tpr_test = unsupervised_evaluation(classifier, x_train,
                                                                             x_test, x_attacks)

                svm_results = svm_results.append({'nu': nu, 'gamma': gamma, 'n_components': x_test.shape[1],
                                                  'TPR_train': tpr_train,
                                                  'TPR_test': tpr_test, 'TNR': tnr, 'model': 'svm', 'auc': area,
                                                  'f_beta': fb, 'projection': 'None'}, ignore_index=True)

    return svm_results


def isolation_forest(x_train, x_test, x_attacks, isolation_results):

    # Isolation Forest Hyper-parameters
    estimators = [200, 100]
    contaminations = [0.01]
    dimensions = [int(i*x_test.shape[1]) for i in [0.25, 0.5, 0.9, 1]]
    dimensions = list(filter(lambda x: x > 0, dimensions))

    for n in dimensions:

        x_reduced_pca, test_reduced_pca, attack_reduced_pca = reduce_dimensionality(n, x_train, x_test, 'PCA',
                                                                                    attack=x_attacks)
        x_reduced_rp, test_reduced_rp, attack_reduced_rp = reduce_dimensionality(n, x_train, x_test, 'RP',
                                                                                 attack=x_attacks)

        max_features = list(range(1, n + 1, 4))
        for estimator in estimators:
            for contamination in contaminations:
                for max_feature in max_features:
                    classifier = IsolationForest(n_estimators=estimator,
                                                 contamination=contamination,
                                                 max_features=max_feature,
                                                 n_jobs=7)

                    classifier.fit(x_reduced_pca)
                    fb, area, tnr, tpr_train, tpr_test = unsupervised_evaluation(classifier, x_reduced_pca,
                                                                                 test_reduced_pca, attack_reduced_pca)

                    isolation_results = isolation_results.append({'estimators': estimator, 'contamination': contamination,
                                                                  'n_components': n, 'max_features': max_feature,
                                                                  'TPR_train': tpr_train,
                                                                  'TPR_test': tpr_test,
                                                                  'TNR': tnr,
                                                                  'model': 'isolation_forest',
                                                                  'auc': area,
                                                                  'f_beta': fb,
                                                                  'projection': 'PCA'}, ignore_index=True)

                    classifier = IsolationForest(n_estimators=estimator,
                                                 contamination=contamination,
                                                 max_features=max_feature,
                                                 n_jobs=7)
                    classifier.fit(x_reduced_rp)
                    fb, area, tnr, tpr_train, tpr_test = unsupervised_evaluation(classifier, x_reduced_rp,
                                                                                 test_reduced_rp, attack_reduced_rp)

                    isolation_results = isolation_results.append({'estimators': estimator, 'contamination': contamination,
                                                                  'n_components': n, 'max_features': max_feature,
                                                                  'TPR_train': tpr_train,
                                                                  'TPR_test': tpr_test,
                                                                  'TNR': tnr,
                                                                  'model': 'isolation_forest',
                                                                  'auc': area,
                                                                  'f_beta': fb,
                                                                  'projection': 'RP'}, ignore_index=True)
    return isolation_results


def autoencoder(x_train, x_test, x_attacks, ae_svm_results):
    latent_dim = 3
    input_vector = Input(shape=(x_train.shape[1],))
    encoded = Dense(latent_dim, activation='relu')(input_vector)
    decoded = Dense(x_train.shape[1], activity_regularizer=regularizers.l1(10e-5))(encoded)
    autoencoder = Model(input_vector, decoded)
    encoder = Model(input_vector, encoded)
    autoencoder.compile(optimizer=Adam(lr=0.001), loss='mse')
    network_history = autoencoder.fit(x_train, x_train, shuffle=True, batch_size=16, epochs=10,
                                      validation_data=(x_test, x_test), verbose=True)
    plot_history(network_history, 'AE history')
    print('Mean loss on train: {}'.format(autoencoder.evaluate(x_train, x_train, batch_size=8, verbose=False)))
    print('Mean loss on test: {}'.format(autoencoder.evaluate(x_test, x_test, batch_size=8, verbose=False)))
    print('Mean loss on attacks: {}'.format(autoencoder.evaluate(x_attacks, x_attacks, batch_size=8, verbose=False)))
    x_train_red = encoder.predict(x_train, batch_size=8)
    x_test_red = encoder.predict(x_test, batch_size=8)
    x_attacks_red = encoder.predict(x_attacks, batch_size=8)

    nus = [0.01]

    gammas = [x_train_red.shape[1], 2*x_train_red.shape[1], x_train_red.shape[1]/2, 'auto']
    for nu in nus:
        for gamma in gammas:
            classifier = svm.OneClassSVM(kernel='rbf', gamma=gamma, nu=nu, cache_size=7000)
            classifier.fit(x_train_red)

            fb, area, tnr, tpr_train, tpr_test = unsupervised_evaluation(classifier, x_train_red,
                                                                         x_test_red, x_attacks_red)

            ae_svm_results = ae_svm_results.append({'nu': nu, 'gamma': gamma, 'n_components': latent_dim,
                                                    'TPR_train': tpr_train, 'TPR_test': tpr_test, 'TNR': tnr,
                                                    'model': 'ae-svm', 'auc': area, 'f_beta': fb}, ignore_index=True)

    return ae_svm_results


def unsupervised_evaluation(classifier, train_set, test_set, attack_set, beta=20):

    y_pred_train = classifier.predict(train_set)
    y_pred_test = classifier.predict(test_set)
    y_pred_outliers = classifier.predict(attack_set)
    n_accurate_train = y_pred_train[y_pred_train == 1].size
    n_accurate_test = y_pred_test[y_pred_test == 1].size
    n_accurate_outliers = y_pred_outliers[y_pred_outliers == -1].size

    fpr, tpr, _ = roc_curve(np.concatenate([np.ones(y_pred_test.shape[0]), -1*np.ones(y_pred_outliers.shape[0])]),
                            np.concatenate([y_pred_test, y_pred_outliers]), pos_label=1)
    fb = fbeta_score(np.concatenate([np.ones(y_pred_test.shape[0]), -1*np.ones(y_pred_outliers.shape[0])]),
                     np.concatenate([y_pred_test, y_pred_outliers]), beta=beta, pos_label=1)

    tnr = n_accurate_outliers/attack_set.shape[0]
    tpr_test = n_accurate_test/test_set.shape[0]
    tpr_train = n_accurate_train/train_set.shape[0]

    area = auc(fpr, tpr)
    return fb, area, tnr, tpr_train, tpr_test


def neural_network(x_train, y_train, x_test, y_test):
    model = Sequential()

    model.add(Dense(128, input_shape=(x_train.shape[1],), activation='relu', kernel_regularizer=l2(0.01)))
    model.add(Dropout(0.1))

    model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.01)))
    model.add(Dropout(0.2))

    model.add(Dense(128, kernel_initializer='glorot_uniform', activation='sigmoid'))
    model.add(Dropout(0.4))

    model.add(Dense(64, kernel_initializer='glorot_uniform', activation='tanh'))
    model.add(Dropout(0.5))

    model.add(Dense(32, kernel_initializer='glorot_uniform', activation='tanh'))
    model.add(Dropout(0.4))

    model.add(Dense(128, kernel_initializer='glorot_uniform', activation='tanh'))
    model.add(Dropout(0.3))

    model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))

    model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
    network_history = model.fit(x_train, y_train, batch_size=128, epochs=10, verbose=0,
                                validation_data=(x_test, y_test))
    plot_history_with_acc(network_history)
    return model


def random_forest(x_train, y_train, x_test, y_test, random_forest_results):

    # Random forest Hyper-parameters
    estimators = [150, 200]
    dimensions = [int(i*x_test.shape[1]) for i in [1]]

    for estimator in estimators:

        for n in dimensions:

            x_reduced_pca, test_reduced_pca = reduce_dimensionality(n, x_train, x_test, 'PCA')
            x_reduced_rp, test_reduced_rp = reduce_dimensionality(n, x_train, x_test, 'RP')

            classifier = RandomForestClassifier(n_estimators=estimator, n_jobs=7)

            classifier.fit(x_reduced_pca, y_train)
            fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_pca, y_test)

            random_forest_results = random_forest_results.append({'estimators': estimator,
                                                                  'n_components': n,
                                                                  'TPR': tpr,
                                                                  'TNR': tnr,
                                                                  'model': 'random_forest',
                                                                  'auc': area,
                                                                  'f_beta': fb,
                                                                  'projection': 'PCA'}, ignore_index=True)

            classifier = RandomForestClassifier(n_estimators=estimator, n_jobs=7)

            classifier.fit(x_reduced_rp, y_train)
            fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_rp, y_test)

            random_forest_results = random_forest_results.append({'estimators': estimator,
                                                                  'n_components': n,
                                                                  'TPR': tpr,
                                                                  'TNR': tnr,
                                                                  'model': 'random_forest',
                                                                  'auc': area,
                                                                  'f_beta': fb,
                                                                  'projection': 'RP'}, ignore_index=True)

            classifier = RandomForestClassifier(n_estimators=estimator, n_jobs=7)

        classifier.fit(x_train, y_train)
        fb, area, tnr, tpr = supervised_evaluation(classifier, x_test, y_test)

        random_forest_results = random_forest_results.append({'estimators': estimator,
                                                              'n_components': x_test.shape[1],
                                                              'TPR': tpr,
                                                              'TNR': tnr,
                                                              'model': 'random_forest',
                                                              'auc': area,
                                                              'f_beta': fb,
                                                              'projection': 'None'}, ignore_index=True)

    return random_forest_results


def ada_boost(x_train, y_train, x_test, y_test, ada_boost_results):

    # AdaBoost Hyper-parameters
    learning_rates = [0.55]
    dimensions = [int(i*x_test.shape[1]) for i in [1]]

    for n in dimensions:

        x_reduced_pca, test_reduced_pca = reduce_dimensionality(n, x_train, x_test, 'PCA')
        x_reduced_rp, test_reduced_rp = reduce_dimensionality(n, x_train, x_test, 'RP')

        for lr in learning_rates:

            classifier = AdaBoostClassifier(learning_rate=lr)

            classifier.fit(x_reduced_pca, y_train)
            fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_pca, y_test)

            ada_boost_results = ada_boost_results.append({'LR': lr,
                                                          'n_components': n,
                                                          'TPR': tpr,
                                                          'TNR': tnr,
                                                          'model': 'ada_boost',
                                                          'auc': area,
                                                          'f_beta': fb,
                                                          'projection': 'PCA'}, ignore_index=True)

            classifier = AdaBoostClassifier(learning_rate=lr)

            classifier.fit(x_reduced_rp, y_train)
            fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_rp, y_test)

            ada_boost_results = ada_boost_results.append({'LR': lr,
                                                          'n_components': n,
                                                          'TPR': tpr,
                                                          'TNR': tnr,
                                                          'model': 'ada_boost',
                                                          'auc': area,
                                                          'f_beta': fb,
                                                          'projection': 'RP'}, ignore_index=True)
    return ada_boost_results


def svm_classifier(x_train, y_train, x_test, y_test, svm_results):

    # SVC Hyper-parameters
    dimensions = [int(i*x_test.shape[1]) for i in [1]]

    for n in dimensions:

        x_reduced_pca, test_reduced_pca = reduce_dimensionality(n, x_train, x_test, 'PCA')
        x_reduced_rp, test_reduced_rp = reduce_dimensionality(n, x_train, x_test, 'RP')

        classifier = svm.SVC(gamma='auto', cache_size=7000)

        classifier.fit(x_reduced_pca, y_train)
        fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_pca, y_test)

        svm_results = svm_results.append({
                                          'n_components': n,
                                          'TPR': tpr,
                                          'TNR': tnr,
                                          'model': 'svm',
                                          'auc': area,
                                          'f_beta': fb,
                                          'projection': 'PCA'}, ignore_index=True)

        classifier = svm.SVC(gamma='auto', cache_size=7000)

        classifier.fit(x_reduced_rp, y_train)
        fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_rp, y_test)

        svm_results = svm_results.append({
                                          'n_components': n,
                                          'TPR': tpr,
                                          'TNR': tnr,
                                          'model': 'svm',
                                          'auc': area,
                                          'f_beta': fb,
                                          'projection': 'RP'}, ignore_index=True)
    return svm_results


def xg_boost(x_train, y_train, x_test, y_test, xg_boost_results):

    # XGBoost Hyper-parameters
    dimensions = [int(i*x_test.shape[1]) for i in [1]]

    for n in dimensions:

        x_reduced_pca, test_reduced_pca = reduce_dimensionality(n, x_train, x_test, 'PCA')
        x_reduced_rp, test_reduced_rp = reduce_dimensionality(n, x_train, x_test, 'RP')

        classifier = xgb.XGBClassifier()
        grid = {'max_depth': 10}
        classifier.set_params(**grid)

        classifier.fit(x_reduced_pca, y_train)
        fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_pca, y_test)

        xg_boost_results = xg_boost_results.append({
            'n_components': n,
            'TPR': tpr,
            'TNR': tnr,
            'model': 'xgboost',
            'auc': area,
            'f_beta': fb,
            'projection': 'PCA'}, ignore_index=True)

        classifier = xgb.XGBClassifier()
        grid = {'max_depth': 10}
        classifier.set_params(**grid)

        classifier.fit(x_reduced_rp, y_train)
        fb, area, tnr, tpr = supervised_evaluation(classifier, test_reduced_rp, y_test)

        xg_boost_results = xg_boost_results.append({
            'n_components': n,
            'TPR': tpr,
            'TNR': tnr,
            'model': 'xgboost',
            'auc': area,
            'f_beta': fb,
            'projection': 'RP'}, ignore_index=True)

    classifier = xgb.XGBClassifier()
    grid = {'max_depth': 10}
    classifier.set_params(**grid)

    classifier.fit(x_train, y_train)
    fb, area, tnr, tpr = supervised_evaluation(classifier, x_test, y_test)

    xg_boost_results = xg_boost_results.append({
        'n_components': x_test.shape[1],
        'TPR': tpr,
        'TNR': tnr,
        'model': 'xgboost',
        'auc': area,
        'f_beta': fb,
        'projection': 'None'}, ignore_index=True)

    return xg_boost_results


def supervised_evaluation(classifier, x_test, y_test, beta=20, nn=False):

    if not nn:
        y_pred = classifier.predict(x_test)
        confusion_matrix(y_test, y_pred)
        fpr, tpr, _ = roc_curve(y_test, y_pred)
        fb = fbeta_score(y_test, y_pred, beta=beta, pos_label=1)
        area = auc(fpr, tpr)
        tpr = tpr[1]
        tnr = 1 - fpr[1]
        return fb, area, tnr, tpr


def plot_roc(classifier, test, attacks, title):

    y_pred_test = classifier.predict(test)
    y_pred_outliers = classifier.predict(attacks)

    fpr, tpr, _ = roc_curve(np.concatenate([np.ones(y_pred_test.shape[0]),
                                            -1*np.ones(y_pred_outliers.shape[0])]),
                            np.concatenate([y_pred_test, y_pred_outliers]), pos_label=1)
    roc_auc = auc(fpr, tpr)
    plt.figure()
    lw = 2
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic: {}'.format(title))
    plt.legend(loc='lower right')
    plt.show()


def plot_roc_supervised(classifier, x_test, y_test, title, nn=False):

    y_pred = classifier.predict(x_test)
    fpr, tpr, _ = roc_curve(y_test, y_pred)

    if nn:
        y_pred = [round(x[0]) for x in y_pred]
    print(confusion_matrix(y_test, y_pred))

    roc_auc = auc(fpr, tpr)
    plt.figure()
    lw = 2
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic {}'.format(title))
    plt.legend(loc='lower right')
    plt.show()


def plot_history(network_history, title):
    plt.figure(figsize=(10, 5))
    plt.title(title)
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.semilogy(network_history.history['loss'])
    plt.semilogy(network_history.history['val_loss'])
    plt.legend(['Training', 'Validation'])
    plt.show()


def plot_history_with_acc(network_history, title='Loss and Accuracy'):
    plt.figure(figsize=(15, 10))
    plt.subplot(211)
    plt.title(title)
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.semilogy(network_history.history['loss'])
    plt.semilogy(network_history.history['val_loss'])
    plt.legend(['Training', 'Validation'])

    plt.subplot(212)
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.plot(network_history.history['acc'])
    plt.plot(network_history.history['val_acc'])
    plt.legend(['Training', 'Validation'], loc='lower right')
    plt.show()


def reduce_dimensionality(n_components, train, test, method, attack=None):
    if method == 'PCA':
        matrix = PCA(n_components=n_components)
    elif method == 'RP':
        matrix = random_projection.SparseRandomProjection(n_components=n_components, random_state=7)
    else:
        print('unknown projection method, choose either RP or PCA')
        return None

    train = matrix.fit_transform(train)
    test = matrix.transform(test)

    if attack is None:
        return train, test

    attack = matrix.transform(attack)
    return train, test, attack

[((122, 19, 122, 51), 'keras.layers.Input', 'Input', (), '', False, 'from keras.layers import Dense, Input, Dropout\n'), ((125, 18, 125, 46), 'keras.models.Model', 'Model', ({(125, 24, 125, 36): 'input_vector', (125, 38, 125, 45): 'decoded'}, {}), '(input_vector, decoded)', False, 'from keras.models import Model\n'), ((126, 14, 126, 42), 'keras.models.Model', 'Model', ({(126, 20, 126, 32): 'input_vector', (126, 34, 126, 41): 'encoded'}, {}), '(input_vector, encoded)', False, 'from keras.models import Model\n'), ((174, 11, 174, 24), 'sklearn.metrics.auc', 'auc', ({(174, 15, 174, 18): 'fpr', (174, 20, 174, 23): 'tpr'}, {}), '(fpr, tpr)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((179, 12, 179, 24), 'keras.models.Sequential', 'Sequential', ({}, {}), '()', False, 'from keras.models import Sequential\n'), ((391, 17, 391, 36), 'xgboost.XGBClassifier', 'xgb.XGBClassifier', ({}, {}), '()', True, 'import xgboost as xgb\n'), ((431, 14, 431, 27), 'sklearn.metrics.auc', 'auc', ({(431, 18, 431, 21): 'fpr', (431, 23, 431, 26): 'tpr'}, {}), '(fpr, tpr)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((432, 4, 432, 16), 'matplotlib.pyplot.figure', 'plt.figure', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((434, 4, 435, 63), 'matplotlib.pyplot.plot', 'plt.plot', (), '', True, 'import matplotlib.pyplot as plt\n'), ((436, 4, 436, 65), 'matplotlib.pyplot.plot', 'plt.plot', (), '', True, 'import matplotlib.pyplot as plt\n'), ((437, 4, 437, 24), 'matplotlib.pyplot.xlim', 'plt.xlim', ({(437, 13, 437, 23): '[0.0, 1.0]'}, {}), '([0.0, 1.0])', True, 'import matplotlib.pyplot as plt\n'), ((438, 4, 438, 25), 'matplotlib.pyplot.ylim', 'plt.ylim', ({(438, 13, 438, 24): '[0.0, 1.05]'}, {}), '([0.0, 1.05])', True, 'import matplotlib.pyplot as plt\n'), ((439, 4, 439, 37), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(439, 15, 439, 36): '"""False Positive Rate"""'}, {}), "('False Positive Rate')", True, 'import matplotlib.pyplot as plt\n'), ((440, 4, 440, 36), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(440, 15, 440, 35): '"""True Positive Rate"""'}, {}), "('True Positive Rate')", True, 'import matplotlib.pyplot as plt\n'), ((442, 4, 442, 33), 'matplotlib.pyplot.legend', 'plt.legend', (), '', True, 'import matplotlib.pyplot as plt\n'), ((443, 4, 443, 14), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((449, 18, 449, 43), 'sklearn.metrics.roc_curve', 'roc_curve', ({(449, 28, 449, 34): 'y_test', (449, 36, 449, 42): 'y_pred'}, {}), '(y_test, y_pred)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((455, 14, 455, 27), 'sklearn.metrics.auc', 'auc', ({(455, 18, 455, 21): 'fpr', (455, 23, 455, 26): 'tpr'}, {}), '(fpr, tpr)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((456, 4, 456, 16), 'matplotlib.pyplot.figure', 'plt.figure', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((458, 4, 459, 63), 'matplotlib.pyplot.plot', 'plt.plot', (), '', True, 'import matplotlib.pyplot as plt\n'), ((460, 4, 460, 65), 'matplotlib.pyplot.plot', 'plt.plot', (), '', True, 'import matplotlib.pyplot as plt\n'), ((461, 4, 461, 24), 'matplotlib.pyplot.xlim', 'plt.xlim', ({(461, 13, 461, 23): '[0.0, 1.0]'}, {}), '([0.0, 1.0])', True, 'import matplotlib.pyplot as plt\n'), ((462, 4, 462, 25), 'matplotlib.pyplot.ylim', 'plt.ylim', ({(462, 13, 462, 24): '[0.0, 1.05]'}, {}), '([0.0, 1.05])', True, 'import matplotlib.pyplot as plt\n'), ((463, 4, 463, 37), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(463, 15, 463, 36): '"""False Positive Rate"""'}, {}), "('False Positive Rate')", True, 'import matplotlib.pyplot as plt\n'), ((464, 4, 464, 36), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(464, 15, 464, 35): '"""True Positive Rate"""'}, {}), "('True Positive Rate')", True, 'import matplotlib.pyplot as plt\n'), ((466, 4, 466, 33), 'matplotlib.pyplot.legend', 'plt.legend', (), '', True, 'import matplotlib.pyplot as plt\n'), ((467, 4, 467, 14), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((471, 4, 471, 31), 'matplotlib.pyplot.figure', 'plt.figure', (), '', True, 'import matplotlib.pyplot as plt\n'), ((472, 4, 472, 20), 'matplotlib.pyplot.title', 'plt.title', ({(472, 14, 472, 19): 'title'}, {}), '(title)', True, 'import matplotlib.pyplot as plt\n'), ((473, 4, 473, 24), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(473, 15, 473, 23): '"""Epochs"""'}, {}), "('Epochs')", True, 'import matplotlib.pyplot as plt\n'), ((474, 4, 474, 22), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(474, 15, 474, 21): '"""Loss"""'}, {}), "('Loss')", True, 'import matplotlib.pyplot as plt\n'), ((475, 4, 475, 49), 'matplotlib.pyplot.semilogy', 'plt.semilogy', ({(475, 17, 475, 48): "network_history.history['loss']"}, {}), "(network_history.history['loss'])", True, 'import matplotlib.pyplot as plt\n'), ((476, 4, 476, 53), 'matplotlib.pyplot.semilogy', 'plt.semilogy', ({(476, 17, 476, 52): "network_history.history['val_loss']"}, {}), "(network_history.history['val_loss'])", True, 'import matplotlib.pyplot as plt\n'), ((477, 4, 477, 42), 'matplotlib.pyplot.legend', 'plt.legend', ({(477, 15, 477, 41): "['Training', 'Validation']"}, {}), "(['Training', 'Validation'])", True, 'import matplotlib.pyplot as plt\n'), ((478, 4, 478, 14), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((482, 4, 482, 32), 'matplotlib.pyplot.figure', 'plt.figure', (), '', True, 'import matplotlib.pyplot as plt\n'), ((483, 4, 483, 20), 'matplotlib.pyplot.subplot', 'plt.subplot', ({(483, 16, 483, 19): '(211)'}, {}), '(211)', True, 'import matplotlib.pyplot as plt\n'), ((484, 4, 484, 20), 'matplotlib.pyplot.title', 'plt.title', ({(484, 14, 484, 19): 'title'}, {}), '(title)', True, 'import matplotlib.pyplot as plt\n'), ((485, 4, 485, 24), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(485, 15, 485, 23): '"""Epochs"""'}, {}), "('Epochs')", True, 'import matplotlib.pyplot as plt\n'), ((486, 4, 486, 22), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(486, 15, 486, 21): '"""Loss"""'}, {}), "('Loss')", True, 'import matplotlib.pyplot as plt\n'), ((487, 4, 487, 49), 'matplotlib.pyplot.semilogy', 'plt.semilogy', ({(487, 17, 487, 48): "network_history.history['loss']"}, {}), "(network_history.history['loss'])", True, 'import matplotlib.pyplot as plt\n'), ((488, 4, 488, 53), 'matplotlib.pyplot.semilogy', 'plt.semilogy', ({(488, 17, 488, 52): "network_history.history['val_loss']"}, {}), "(network_history.history['val_loss'])", True, 'import matplotlib.pyplot as plt\n'), ((489, 4, 489, 42), 'matplotlib.pyplot.legend', 'plt.legend', ({(489, 15, 489, 41): "['Training', 'Validation']"}, {}), "(['Training', 'Validation'])", True, 'import matplotlib.pyplot as plt\n'), ((491, 4, 491, 20), 'matplotlib.pyplot.subplot', 'plt.subplot', ({(491, 16, 491, 19): '(212)'}, {}), '(212)', True, 'import matplotlib.pyplot as plt\n'), ((492, 4, 492, 24), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(492, 15, 492, 23): '"""Epochs"""'}, {}), "('Epochs')", True, 'import matplotlib.pyplot as plt\n'), ((493, 4, 493, 26), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(493, 15, 493, 25): '"""Accuracy"""'}, {}), "('Accuracy')", True, 'import matplotlib.pyplot as plt\n'), ((494, 4, 494, 44), 'matplotlib.pyplot.plot', 'plt.plot', ({(494, 13, 494, 43): "network_history.history['acc']"}, {}), "(network_history.history['acc'])", True, 'import matplotlib.pyplot as plt\n'), ((495, 4, 495, 48), 'matplotlib.pyplot.plot', 'plt.plot', ({(495, 13, 495, 47): "network_history.history['val_acc']"}, {}), "(network_history.history['val_acc'])", True, 'import matplotlib.pyplot as plt\n'), ((496, 4, 496, 61), 'matplotlib.pyplot.legend', 'plt.legend', (), '', True, 'import matplotlib.pyplot as plt\n'), ((497, 4, 497, 14), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((123, 14, 123, 50), 'keras.layers.Dense', 'Dense', (), '', False, 'from keras.layers import Dense, Input, Dropout\n'), ((166, 28, 166, 74), 'numpy.concatenate', 'np.concatenate', ({(166, 43, 166, 73): '[y_pred_test, y_pred_outliers]'}, {}), '([y_pred_test, y_pred_outliers])', True, 'import numpy as np\n'), ((168, 21, 168, 67), 'numpy.concatenate', 'np.concatenate', ({(168, 36, 168, 66): '[y_pred_test, y_pred_outliers]'}, {}), '([y_pred_test, y_pred_outliers])', True, 'import numpy as np\n'), ((182, 14, 182, 26), 'keras.layers.Dropout', 'Dropout', ({(182, 22, 182, 25): '(0.1)'}, {}), '(0.1)', False, 'from keras.layers import Dense, Input, Dropout\n'), ((185, 14, 185, 26), 'keras.layers.Dropout', 'Dropout', ({(185, 22, 185, 25): '(0.2)'}, {}), '(0.2)', False, 'from keras.layers import Dense, Input, Dropout\n'), ((187, 14, 187, 83), 'keras.layers.Dense', 'Dense', (), '', False, 'from keras.layers import Dense, Input, Dropout\n'), ((188, 14, 188, 26), 'keras.layers.Dropout', 'Dropout', ({(188, 22, 188, 25): '(0.4)'}, {}), '(0.4)', False, 'from keras.layers import Dense, Input, Dropout\n'), ((190, 14, 190, 79), 'keras.layers.Dense', 'Dense', (), '', False, 'from keras.layers import Dense, Input, Dropout\n'), ((191, 14, 191, 26), 'keras.layers.Dropout', 'Dropout', ({(191, 22, 191, 25): '(0.5)'}, {}), '(0.5)', False, 'from keras.layers import Dense, Input, Dropout\n'), ((193, 14, 193, 79), 'keras.layers.Dense', 'Dense', (), '', False, 'from keras.layers import Dense, Input, Dropout\n'), ((194, 14, 194, 26), 'keras.layers.Dropout', 'Dropout', ({(194, 22, 194, 25): '(0.4)'}, {}), '(0.4)', False, 'from keras.layers import Dense, Input, Dropout\n'), ((196, 14, 196, 80), 'keras.layers.Dense', 'Dense', (), '', False, 'from keras.layers import Dense, Input, Dropout\n'), ((197, 14, 197, 26), 'keras.layers.Dropout', 'Dropout', ({(197, 22, 197, 25): '(0.3)'}, {}), '(0.3)', False, 'from keras.layers import Dense, Input, Dropout\n'), ((199, 14, 199, 73), 'keras.layers.Dense', 'Dense', (), '', False, 'from keras.layers import Dense, Input, Dropout\n'), ((319, 21, 319, 59), 'sklearn.svm.SVC', 'svm.SVC', (), '', False, 'from sklearn import svm\n'), ((333, 21, 333, 59), 'sklearn.svm.SVC', 'svm.SVC', (), '', False, 'from sklearn import svm\n'), ((359, 21, 359, 40), 'xgboost.XGBClassifier', 'xgb.XGBClassifier', ({}, {}), '()', True, 'import xgboost as xgb\n'), ((375, 21, 375, 40), 'xgboost.XGBClassifier', 'xgb.XGBClassifier', ({}, {}), '()', True, 'import xgboost as xgb\n'), ((414, 8, 414, 40), 'sklearn.metrics.confusion_matrix', 'confusion_matrix', ({(414, 25, 414, 31): 'y_test', (414, 33, 414, 39): 'y_pred'}, {}), '(y_test, y_pred)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((415, 22, 415, 47), 'sklearn.metrics.roc_curve', 'roc_curve', ({(415, 32, 415, 38): 'y_test', (415, 40, 415, 46): 'y_pred'}, {}), '(y_test, y_pred)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((416, 13, 416, 64), 'sklearn.metrics.fbeta_score', 'fbeta_score', (), '', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((417, 15, 417, 28), 'sklearn.metrics.auc', 'auc', ({(417, 19, 417, 22): 'fpr', (417, 24, 417, 27): 'tpr'}, {}), '(fpr, tpr)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((430, 28, 430, 74), 'numpy.concatenate', 'np.concatenate', ({(430, 43, 430, 73): '[y_pred_test, y_pred_outliers]'}, {}), '([y_pred_test, y_pred_outliers])', True, 'import numpy as np\n'), ((453, 10, 453, 42), 'sklearn.metrics.confusion_matrix', 'confusion_matrix', ({(453, 27, 453, 33): 'y_test', (453, 35, 453, 41): 'y_pred'}, {}), '(y_test, y_pred)', False, 'from sklearn.metrics import fbeta_score, roc_curve, auc, confusion_matrix\n'), ((502, 17, 502, 47), 'sklearn.decomposition.PCA', 'PCA', (), '', False, 'from sklearn.decomposition import PCA\n'), ((127, 34, 127, 48), 'keras.optimizers.Adam', 'Adam', (), '', False, 'from keras.optimizers import Adam\n'), ((143, 25, 143, 91), 'sklearn.svm.OneClassSVM', 'svm.OneClassSVM', (), '', False, 'from sklearn import svm\n'), ((221, 25, 221, 81), 'sklearn.ensemble.RandomForestClassifier', 'RandomForestClassifier', (), '', False, 'from sklearn.ensemble import RandomForestClassifier\n'), ((235, 25, 235, 81), 'sklearn.ensemble.RandomForestClassifier', 'RandomForestClassifier', (), '', False, 'from sklearn.ensemble import RandomForestClassifier\n'), ((249, 25, 249, 81), 'sklearn.ensemble.RandomForestClassifier', 'RandomForestClassifier', (), '', False, 'from sklearn.ensemble import RandomForestClassifier\n'), ((279, 25, 279, 61), 'sklearn.ensemble.AdaBoostClassifier', 'AdaBoostClassifier', (), '', False, 'from sklearn.ensemble import AdaBoostClassifier\n'), ((293, 25, 293, 61), 'sklearn.ensemble.AdaBoostClassifier', 'AdaBoostClassifier', (), '', False, 'from sklearn.ensemble import AdaBoostClassifier\n'), ((504, 17, 504, 100), 'sklearn.random_projection.SparseRandomProjection', 'random_projection.SparseRandomProjection', (), '', False, 'from sklearn import random_projection\n'), ((36, 29, 36, 95), 'sklearn.svm.OneClassSVM', 'svm.OneClassSVM', (), '', False, 'from sklearn import svm\n'), ((47, 29, 47, 95), 'sklearn.svm.OneClassSVM', 'svm.OneClassSVM', (), '', False, 'from sklearn import svm\n'), ((124, 59, 124, 81), 'keras.regularizers.l1', 'regularizers.l1', ({(124, 75, 124, 80): '0.0001'}, {}), '(0.0001)', False, 'from keras import regularizers\n'), ((165, 44, 165, 73), 'numpy.ones', 'np.ones', ({(165, 52, 165, 72): 'y_pred_test.shape[0]'}, {}), '(y_pred_test.shape[0])', True, 'import numpy as np\n'), ((167, 37, 167, 66), 'numpy.ones', 'np.ones', ({(167, 45, 167, 65): 'y_pred_test.shape[0]'}, {}), '(y_pred_test.shape[0])', True, 'import numpy as np\n'), ((181, 96, 181, 104), 'keras.regularizers.l2', 'l2', ({(181, 99, 181, 103): '(0.01)'}, {}), '(0.01)', False, 'from keras.regularizers import l2\n'), ((184, 62, 184, 70), 'keras.regularizers.l2', 'l2', ({(184, 65, 184, 69): '(0.01)'}, {}), '(0.01)', False, 'from keras.regularizers import l2\n'), ((428, 44, 428, 73), 'numpy.ones', 'np.ones', ({(428, 52, 428, 72): 'y_pred_test.shape[0]'}, {}), '(y_pred_test.shape[0])', True, 'import numpy as np\n'), ((81, 33, 84, 58), 'sklearn.ensemble.IsolationForest', 'IsolationForest', (), '', False, 'from sklearn.ensemble import IsolationForest\n'), ((100, 33, 103, 58), 'sklearn.ensemble.IsolationForest', 'IsolationForest', (), '', False, 'from sklearn.ensemble import IsolationForest\n'), ((165, 78, 165, 111), 'numpy.ones', 'np.ones', ({(165, 86, 165, 110): 'y_pred_outliers.shape[0]'}, {}), '(y_pred_outliers.shape[0])', True, 'import numpy as np\n'), ((167, 71, 167, 104), 'numpy.ones', 'np.ones', ({(167, 79, 167, 103): 'y_pred_outliers.shape[0]'}, {}), '(y_pred_outliers.shape[0])', True, 'import numpy as np\n'), ((429, 47, 429, 80), 'numpy.ones', 'np.ones', ({(429, 55, 429, 79): 'y_pred_outliers.shape[0]'}, {}), '(y_pred_outliers.shape[0])', True, 'import numpy as np\n')]