text_plain_output_5.png text_plain_output_15.png text_plain_output_9.png text_plain_output_4.png text_plain_output_13.png text_plain_output_14.png text_plain_output_27.png text_plain_output_10.png text_plain_output_6.png text_plain_output_24.png text_plain_output_21.png text_plain_output_25.png text_plain_output_18.png text_plain_output_3.png text_plain_output_22.png text_plain_output_7.png text_plain_output_16.png text_plain_output_8.png text_plain_output_26.png application_vnd.jupyter.stderr_output_20.png text_plain_output_23.png text_plain_output_28.png text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png text_plain_output_19.png text_plain_output_17.png text_plain_output_11.png text_plain_output_12.png
from catboost import CatBoostClassifier

from lightgbm import LGBMClassifier

from sklearn.ensemble import GradientBoostingClassifier

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import fbeta_score,accuracy_score

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

from time import time

from xgboost import XGBClassifier

import numpy as np # linear algebra

import os

import pandas as pd

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

import pickle

import numpy as np
import pandas as pd
import os

# TODO: Import two metrics from sklearn - fbeta_score and accuracy_score
from sklearn.metrics import fbeta_score,accuracy_score
from time import time

def train_predict(learner, sample_size, X_train, y_train, X_test, y_test): 
    '''
    inputs:
       - learner: the learning algorithm to be trained and predicted on
       - sample_size: the size of samples (number) to be drawn from training set
       - X_train: features training set
       - y_train: income training set
       - X_test: features testing set
       - y_test: income testing set
    '''
    
    results = {}
    
    # TODO: Fit the learner to the training data using slicing with 'sample_size' using .fit(training_features[:], training_labels[:])
    start = time() # Get start time
    learner = learner.fit(X_train[:sample_size],y_train[:sample_size])
    end = time() # Get end time
    
    # TODO: Calculate the training time
    results['train_time'] = end-start
        
    # TODO: Get the predictions on the test set(X_test),
    #       then get predictions on the first 300 training samples(X_train) using .predict()
    start = time() # Get start time
    predictions_test = learner.predict(X_test)
    predictions_train = learner.predict(X_train[:300])
    end = time() # Get end time
    
    # TODO: Calculate the total prediction time
    results['pred_time'] = end-start
            
    # TODO: Compute accuracy on the first 300 training samples which is y_train[:300]
    results['acc_train'] = accuracy_score(y_train[:300],predictions_train)
        
    # TODO: Compute accuracy on test set using accuracy_score()
    results['acc_test'] = accuracy_score(y_test, predictions_test)
    
    # TODO: Compute F-score on the the first 300 training samples using fbeta_score()
#     results['f_train'] = fbeta_score(y_train[:300],predictions_train, beta=0.5)
        
#     # TODO: Compute F-score on the test set which is y_test
#     results['f_test'] = fbeta_score(y_test, predictions_test,  beta=0.5)
       
    # Success
    print("{} trained on {} samples.".format(learner.__class__.__name__, sample_size))
        
    # Return the results
    return results

def evaluate(results):
    """
    Visualization code to display results of various learners.
    
    inputs:
      - learners: a list of supervised learners
      - stats: a list of dictionaries of the statistic results from 'train_predict()'
      - accuracy: The score for the naive predictor
      - f1: The score for the naive predictor
    """
  
    # Create figure
    fig, ax = pl.subplots(2, 3, figsize = (11,7))

    # Constants
    bar_width = 0.23
    colors = ['#A00000','#00A0A0','#00A000', '#ffffff']
    
    # Super loop to plot four panels of data
    for k, learner in enumerate(results.keys()):
        for j, metric in enumerate(['train_time', 'acc_train', 'f_train', 'pred_time', 'acc_test', 'f_test']):
            for i in np.arange(3):
                try:
                
                    # Creative plot code
                    ax[j//3, j%3].bar(i+k*bar_width, results[learner][i][metric], width = bar_width, color = colors[k])
                    ax[j//3, j%3].set_xticks([0.45, 1.45, 2.45])
                    ax[j//3, j%3].set_xticklabels(["1%", "10%", "100%"])
                    ax[j//3, j%3].set_xlabel("Training Set Size")
                    ax[j//3, j%3].set_xlim((-0.1, 3.0))
                except:
                    print('+++',learner, i, metric,'+++')
    
    # Add unique y-labels
    ax[0, 0].set_ylabel("Time (in seconds)")
    ax[0, 1].set_ylabel("Accuracy Score")
    ax[0, 2].set_ylabel("F-score")
    ax[1, 0].set_ylabel("Time (in seconds)")
    ax[1, 1].set_ylabel("Accuracy Score")
    ax[1, 2].set_ylabel("F-score")
    
    # Add titles
    ax[0, 0].set_title("Model Training")
    ax[0, 1].set_title("Accuracy Score on Training Subset")
    ax[0, 2].set_title("F-score on Training Subset")
    ax[1, 0].set_title("Model Predicting")
    ax[1, 1].set_title("Accuracy Score on Testing Set")
    ax[1, 2].set_title("F-score on Testing Set")
    
    # Add horizontal lines for naive predictors
    ax[0, 1].axhline(y = accuracy, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    ax[1, 1].axhline(y = accuracy, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    ax[0, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    ax[1, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
    
    # Set y-limits for score panels
    ax[0, 1].set_ylim((0, 1))
    ax[0, 2].set_ylim((0, 1))
    ax[1, 1].set_ylim((0, 1))
    ax[1, 2].set_ylim((0, 1))

    # Create patches for the legend
    patches = []
    for i, learner in enumerate(results.keys()):
        patches.append(mpatches.Patch(color = colors[i], label = learner))
    pl.legend(handles = patches, bbox_to_anchor = (-.80, 2.53), \
               loc = 'upper center', borderaxespad = 0., ncol = 3, fontsize = 'x-large')
    
    # Aesthetics
    pl.suptitle("Performance Metrics for Three Supervised Learning Models", fontsize = 16, y = 1.10)
    pl.tight_layout()
    pl.show()

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from xgboost import XGBClassifier
import pandas as pd
from sklearn.model_selection import train_test_split
def train_df(filename):
    df = pd.read_csv(filename, index_col=0)
    m = list(df[' Label'].value_counts().index)
    j = {}
    h = 1
    for i in m:
        if i == 'BENIGN':
            j[i] = 0
        else:
            j[i] = h
            h += 1
    df[' Label'] = [j[x] for x in df[' Label']]
    df = df.dropna()
    y = df[' Label']
    X = df.drop(columns=['Flow ID', ' Label', ' Source IP', ' Destination IP', ' Timestamp', 'SimillarHTTP']).clip(-1000000.0, 1000000.0)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)
    clf_A = GradientBoostingClassifier(random_state=42)
    clf_B = RandomForestClassifier(random_state=42)
    clf_C = KNeighborsClassifier()
    samples_100 = len(y_train)
    samples_10 = int(samples_100 * 0.1)
    samples_1 = int(samples_100 * 0.01)
    results = {}
    for clf in [clf_A, clf_B, clf_C]:
        clf_name = clf.__class__.__name__
        results[clf_name] = {}
        for i, samples in enumerate([samples_1, samples_10, samples_100]):
            results[clf_name][i] = train_predict(clf, samples, X_train, y_train, X_test, y_test)
    clf_D = LGBMClassifier(random_state=42)
    clf_E = CatBoostClassifier(random_state=42, verbose=False)
    clf_F = XGBClassifier(random_state=42)
    for clf in [clf_D, clf_E, clf_F]:
        clf_name = clf.__class__.__name__
        results[clf_name] = {}
        for i, samples in enumerate([samples_1, samples_10, samples_100]):
            results[clf_name][i] = train_predict(clf, samples, X_train, y_train, X_test, y_test)
    return results

import pickle
import os, operator, sys
dirpath = '/kaggle/input/ddos-evaluation-dataset-cic-ddos2019/CSV-01-12/01-12'
all_files = (os.path.join(basedir, filename) for basedir, dirs, files in os.walk(dirpath) for filename in files)
files_and_sizes = ((path, os.path.getsize(path)) for path in all_files)
sorted_files_with_size = sorted(files_and_sizes, key=operator.itemgetter(1))
sorted_files_with_size
cont_flag = True
for filename, size in sorted_files_with_size:
    if cont_flag:
        if 'DrDoS_SSDP' in filename:
            cont_flag = False
        continue
    res = train_df(filename)
    pickle.dump(res, open('results_' + filename.split('/')[-1].replace('.csv', '') + '.p', 'wb'))
    print(filename.split('/')[-1].replace('.csv', ''), ' Done\n')