import numpy as np
import pandas as pd
from sklearn.kernel_approximation import RBFSampler
from sklearn.linear_model import SGDClassifier
from sklearn.cross_validation import train_test_split
from sklearn import svm
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import (precision_score, recall_score,f1_score, accuracy_score,mean_squared_error,mean_absolute_error)
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import Normalizer
from pandas_ml import ConfusionMatrix

traindata = pd.read_csv('kddtrain.csv', header=None)
testdata = pd.read_csv('kddtest.csv', header=None)

X = traindata.iloc[:,1:42]
Y = traindata.iloc[:,0]
C = testdata.iloc[:,0]
T = testdata.iloc[:,1:42]

scaler = Normalizer().fit(X)
trainX = scaler.transform(X)

scaler = Normalizer().fit(T)
testT = scaler.transform(T)


traindata = np.array(trainX)
trainlabel = np.array(Y)

testdata = np.array(testT)
testlabel = np.array(C)


model = LogisticRegression()
model.fit(traindata, trainlabel)


# make predictions
expected = testlabel
predicted = model.predict(testdata)


print("***************************************************************")



# fit a Naive Bayes model to the data
model = GaussianNB()
model.fit(traindata, trainlabel)
print(model)
# make predictions
expected = testlabel
predicted = model.predict(testdata)

cm = ConfusionMatrix(expected, predicted)
print(expected.shape)
print(predicted.shape)
expected = np.array(expected)
predicted = np.array(predicted)
cm.print_stats()

np.savetxt('expected.txt', expected, fmt='%01d')
np.savetxt('predicted.txt',predicted , fmt='%01d')

print(cm)
print(expected.shape)
print(predicted.shape)
cm.stats()
print("***************************************************************")



# fit a k-nearest neighbor model to the data
model = KNeighborsClassifier()
model.fit(traindata, trainlabel)
print(model)
# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")



model = DecisionTreeClassifier()
model.fit(traindata, trainlabel)
print(model)
# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")






model = AdaBoostClassifier(n_estimators=100)
model.fit(traindata, trainlabel)

# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")




model = RandomForestClassifier(n_estimators=100)
model = clf.fit(traindata, trainlabel)

# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")









e70843b965e3b13a45a420ade00b119d14ba0b6d
<mask token>

<mask token>
model.fit(traindata, trainlabel)
<mask token>
print('***************************************************************')
<mask token>
model.fit(traindata, trainlabel)
print(model)
<mask token>
print(expected.shape)
print(predicted.shape)
<mask token>
cm.print_stats()
np.savetxt('expected.txt', expected, fmt='%01d')
np.savetxt('predicted.txt', predicted, fmt='%01d')
print(cm)
print(expected.shape)
print(predicted.shape)
cm.stats()
print('***************************************************************')
<mask token>
model.fit(traindata, trainlabel)
print(model)
<mask token>
print(cm)
<mask token>
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
<mask token>
model.fit(traindata, trainlabel)
print(model)
<mask token>
print(cm)
<mask token>
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
<mask token>
model.fit(traindata, trainlabel)
<mask token>
print(cm)
<mask token>
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
<mask token>
print(cm)
<mask token>
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')

<mask token>
traindata = pd.read_csv('kddtrain.csv', header=None)
testdata = pd.read_csv('kddtest.csv', header=None)
X = traindata.iloc[:, 1:42]
Y = traindata.iloc[:, 0]
C = testdata.iloc[:, 0]
T = testdata.iloc[:, 1:42]
scaler = Normalizer().fit(X)
trainX = scaler.transform(X)
scaler = Normalizer().fit(T)
testT = scaler.transform(T)
traindata = np.array(trainX)
trainlabel = np.array(Y)
testdata = np.array(testT)
testlabel = np.array(C)
model = LogisticRegression()
model.fit(traindata, trainlabel)
expected = testlabel
predicted = model.predict(testdata)
print('***************************************************************')
model = GaussianNB()
model.fit(traindata, trainlabel)
print(model)
expected = testlabel
predicted = model.predict(testdata)
cm = ConfusionMatrix(expected, predicted)
print(expected.shape)
print(predicted.shape)
expected = np.array(expected)
predicted = np.array(predicted)
cm.print_stats()
np.savetxt('expected.txt', expected, fmt='%01d')
np.savetxt('predicted.txt', predicted, fmt='%01d')
print(cm)
print(expected.shape)
print(predicted.shape)
cm.stats()
print('***************************************************************')
model = KNeighborsClassifier()
model.fit(traindata, trainlabel)
print(model)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
model = DecisionTreeClassifier()
model.fit(traindata, trainlabel)
print(model)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
model = AdaBoostClassifier(n_estimators=100)
model.fit(traindata, trainlabel)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
model = RandomForestClassifier(n_estimators=100)
model = clf.fit(traindata, trainlabel)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')

import numpy as np
import pandas as pd
from sklearn.kernel_approximation import RBFSampler
from sklearn.linear_model import SGDClassifier
from sklearn.cross_validation import train_test_split
from sklearn import svm
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score, mean_squared_error, mean_absolute_error
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import Normalizer
from pandas_ml import ConfusionMatrix
traindata = pd.read_csv('kddtrain.csv', header=None)
testdata = pd.read_csv('kddtest.csv', header=None)
X = traindata.iloc[:, 1:42]
Y = traindata.iloc[:, 0]
C = testdata.iloc[:, 0]
T = testdata.iloc[:, 1:42]
scaler = Normalizer().fit(X)
trainX = scaler.transform(X)
scaler = Normalizer().fit(T)
testT = scaler.transform(T)
traindata = np.array(trainX)
trainlabel = np.array(Y)
testdata = np.array(testT)
testlabel = np.array(C)
model = LogisticRegression()
model.fit(traindata, trainlabel)
expected = testlabel
predicted = model.predict(testdata)
print('***************************************************************')
model = GaussianNB()
model.fit(traindata, trainlabel)
print(model)
expected = testlabel
predicted = model.predict(testdata)
cm = ConfusionMatrix(expected, predicted)
print(expected.shape)
print(predicted.shape)
expected = np.array(expected)
predicted = np.array(predicted)
cm.print_stats()
np.savetxt('expected.txt', expected, fmt='%01d')
np.savetxt('predicted.txt', predicted, fmt='%01d')
print(cm)
print(expected.shape)
print(predicted.shape)
cm.stats()
print('***************************************************************')
model = KNeighborsClassifier()
model.fit(traindata, trainlabel)
print(model)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
model = DecisionTreeClassifier()
model.fit(traindata, trainlabel)
print(model)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
model = AdaBoostClassifier(n_estimators=100)
model.fit(traindata, trainlabel)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')
model = RandomForestClassifier(n_estimators=100)
model = clf.fit(traindata, trainlabel)
expected = testlabel
predicted = model.predict(testdata)
cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0]) / np.sum(cm[0])
fpr = float(cm[1][1]) / np.sum(cm[1])
print('%.3f' % tpr)
print('%.3f' % fpr)
print('Accuracy')
print('%.3f' % accuracy)
print('precision')
print('%.3f' % precision)
print('recall')
print('%.3f' % recall)
print('f-score')
print('%.3f' % f1)
print('fpr')
print('%.3f' % fpr)
print('tpr')
print('%.3f' % tpr)
print('***************************************************************')

import numpy as np
import pandas as pd
from sklearn.kernel_approximation import RBFSampler
from sklearn.linear_model import SGDClassifier
from sklearn.cross_validation import train_test_split
from sklearn import svm
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import (precision_score, recall_score,f1_score, accuracy_score,mean_squared_error,mean_absolute_error)
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import Normalizer
from pandas_ml import ConfusionMatrix

traindata = pd.read_csv('kddtrain.csv', header=None)
testdata = pd.read_csv('kddtest.csv', header=None)

X = traindata.iloc[:,1:42]
Y = traindata.iloc[:,0]
C = testdata.iloc[:,0]
T = testdata.iloc[:,1:42]

scaler = Normalizer().fit(X)
trainX = scaler.transform(X)

scaler = Normalizer().fit(T)
testT = scaler.transform(T)


traindata = np.array(trainX)
trainlabel = np.array(Y)

testdata = np.array(testT)
testlabel = np.array(C)


model = LogisticRegression()
model.fit(traindata, trainlabel)


# make predictions
expected = testlabel
predicted = model.predict(testdata)


print("***************************************************************")



# fit a Naive Bayes model to the data
model = GaussianNB()
model.fit(traindata, trainlabel)
print(model)
# make predictions
expected = testlabel
predicted = model.predict(testdata)

cm = ConfusionMatrix(expected, predicted)
print(expected.shape)
print(predicted.shape)
expected = np.array(expected)
predicted = np.array(predicted)
cm.print_stats()

np.savetxt('expected.txt', expected, fmt='%01d')
np.savetxt('predicted.txt',predicted , fmt='%01d')

print(cm)
print(expected.shape)
print(predicted.shape)
cm.stats()
print("***************************************************************")



# fit a k-nearest neighbor model to the data
model = KNeighborsClassifier()
model.fit(traindata, trainlabel)
print(model)
# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")



model = DecisionTreeClassifier()
model.fit(traindata, trainlabel)
print(model)
# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")






model = AdaBoostClassifier(n_estimators=100)
model.fit(traindata, trainlabel)

# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")




model = RandomForestClassifier(n_estimators=100)
model = clf.fit(traindata, trainlabel)

# make predictions
expected = testlabel
predicted = model.predict(testdata)
# summarize the fit of the model

cm = metrics.confusion_matrix(expected, predicted)
print(cm)
tpr = float(cm[0][0])/np.sum(cm[0])
fpr = float(cm[1][1])/np.sum(cm[1])
print("%.3f" %tpr)
print("%.3f" %fpr)
print("Accuracy")
print("%.3f" %accuracy)
print("precision")
print("%.3f" %precision)
print("recall")
print("%.3f" %recall)
print("f-score")
print("%.3f" %f1)
print("fpr")
print("%.3f" %fpr)
print("tpr")
print("%.3f" %tpr)
print("***************************************************************")








