# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd
import numpy as np
from scipy import stats as sp
import sys, os
sys.path.append('../')
import trace_analysis
import plots_analysis
import trace_statistics

# KNN
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import classification_report
from sklearn.model_selection import GridSearchCV
# SVM
from sklearn import svm
#Import Random Forest Model
from sklearn.ensemble import RandomForestClassifier
# -

# # Data Preparation
#
#
# Prepare the data to be used with kNN

# +
plots = [(os.getcwd() + '/traces/', '2019-01JAN-30-1'), 
         (os.getcwd() + '/traces/', '2019-01JAN-30-2'),
         (os.getcwd() + '/traces/', '2019-01JAN-30-3b113b122b145b166b185'),
         (os.getcwd() + '/traces/', '2019-01JAN-30-1b169b153b182'),
         (os.getcwd() + '/traces/', '2019-01JAN-30-1b169')]
stats = None
prova = []

for row in plots:
    label = 'Normal'
    
    # Assign a label
    if row[1].find('b1') >= 0:
        label = 'Black Hole'
    nodes, packets_node = trace_analysis.process_iotlab_node_by_node2(row[0], row[1])
    
    # Update stats
    if stats is None:
        stats = trace_statistics.compute_labeled_statistics(nodes, packets_node, label)
    else:
        stats = pd.concat([stats, trace_statistics.compute_labeled_statistics(nodes, packets_node, label)])
    
        
stats = stats.reset_index(drop=True)
stats.head(5)
# -

# Select the set of features and labels that we use to fit the algorithm

# +
# separate features from target values
features = stats.drop(columns=['node_id', 'label'])
target = stats['label'].values
print('Targets: ' + str(set(target)))

# split dataset into train and test data
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.3, random_state=1)
# -

# # K-Nearest Neighbor (KNN) Classification
#
# Let us first observe the accuracies for different values of k

trace_analysis.knn_test_number_of_neighbors(X_train, X_test, y_train, y_test)

# Let's build KNN classifier 

# +
#Create KNN Classifier
knn = KNeighborsClassifier(n_neighbors=15)

#Train the model using the training sets
knn.fit(X_train, y_train)

#Predict the response for test dataset
y_pred = knn.predict(X_test)

print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

print("\n\n",classification_report(y_test,y_pred))

# -

# # Support Vector Machines (SVM) Classification
#
# Let us experiment with another classifier

# +
#Create a svm Classifier
svm_clf = svm.SVC(kernel='linear', random_state=9, gamma='scale') # Linear Kernel

#Train the model using the training sets
svm_clf.fit(X_train, y_train)

#Predict the response for test dataset
y_pred = svm_clf.predict(X_test)

print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

print("\n\n",classification_report(y_test,y_pred))
# -

# # Random Forests Classifier
#
# Let us experiment with random forests. First, let us select most relevant features.

trace_analysis.random_forests_features_selection(X_train, X_test, y_train, y_test, features)

# Select relevant features

# +
# separate features from target values
features = stats.drop(columns=['node_id', 'label'])
target = stats['label'].values
print('Targets: ' + str(set(target)))

# split dataset into train and test data
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.3, random_state=1)
# -

# Predicted values

# +
#Create a Gaussian Classifier
rf_clf = RandomForestClassifier(n_estimators=100)

#Train the model using the training sets y_pred=clf.predict(X_test)
rf_clf.fit(X_train,y_train)

y_pred = rf_clf.predict(X_test)


print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

print("\n\n",classification_report(y_test,y_pred))

StarcoderdataJupyterScriptsDedupFiltered