import numpy as np
import pandas as pd
import random
df = pd.read_csv('networklog.txt')
df.head()

dh1 = df[["LATENCY","THROUGHPUT"]].values
X_train = pd.DataFrame(dh1,columns=["x","y"]).sample(n=200,replace=True)
X_train.head()
X_test = pd.DataFrame(dh1,columns=["x","y"]).sample(n=100,replace=True)
X_test.head()
X_outliers = pd.DataFrame(dh1,columns=["x","y"]).sample(n=50,replace=True)
X_outliers.head()

%matplotlib inline
import matplotlib.pyplot as plt
p1 = plt.scatter(X_train.x,X_train.y,c='white',s=50,edgecolors='k')
p2 = plt.scatter(X_test.x,X_test.y,c='green',s=50,edgecolors='k')
p3 = plt.scatter(X_outliers.x,X_outliers.y,c='blue',s=50,edgecolors='k')
plt.legend(
    [p1, p2, p3],
    ['training set', 'normal testing set', 'anomalous testing set'],
    loc='lower right',
)
plt.show()

from sklearn.ensemble import IsolationForest
clf = IsolationForest()
clf.fit(X_train)
y_pred_train = clf.predict(X_train)
y_pred_test = clf.predict(X_test)
y_pred_outliers = clf.predict(X_outliers)

X_outliers = X_outliers.assign(pred=y_pred_outliers)
X_outliers.head()

p1 = plt.scatter(X_train.x,X_train.y,c='white',s=50,edgecolors='k')
p2 = plt.scatter(
    X_outliers.loc[X_outliers.pred==-1,['x']],
    X_outliers.loc[X_outliers.pred==-1,['y']],
    c='blue',
    s=50,
    edgecolors='k',
)
p3 = plt.scatter(
    X_outliers.loc[X_outliers.pred==1,['x']],
    X_outliers.loc[X_outliers.pred==1,['y']],
    c="red",
    s=50,
    edgecolors="k",
)
plt.legend(
    [p1, p2, p3],
    ['training observations', 'detected outliers', 'incorrectly labeled outliers'],
    loc = 'lower right',
)
plt.show()

X_test = X_test.assign(pred=y_pred_test)
X_test.head()
p1 = plt.scatter(X_train.x,X_train.y,c='white',s=50,edgecolors='k')
p2 = plt.scatter(
    X_test.loc[X_test.pred==1,['x']],
    X_test.loc[X_test.pred==1,['y']],
    c='blue',
    s=50,
    edgecolors='k',
)
p3 = plt.scatter(
    X_test.loc[X_test.pred==-1,['x']],
    X_test.loc[X_test.pred==-1,['y']],
    c="red",
    s=50,
    edgecolors="k",
)
plt.legend([p1,p2,p3],
           [
               "training observations",
               "correctly labeled test observations",
               "incorrectly labeled test observation",
           ],
           loc = "lower right",
           )
plt.show()