text_plain_output_1.png
from sklearn import preprocessing

from sklearn.ensemble import AdaBoostClassifier

from sklearn.ensemble import ExtraTreesClassifier

from sklearn.ensemble import GradientBoostingClassifier

from sklearn.ensemble import RandomForestClassifier

from sklearn.preprocessing import OneHotEncoder

from sklearn.tree import DecisionTreeClassifier

import pandas as pd

X_train = df_train.drop('xAttack', axis=1)
Y_train = df_train.loc[:, ['xAttack']]
X_test = df_test.drop('xAttack', axis=1)
Y_test = df_test.loc[:, ['xAttack']]

le = preprocessing.LabelEncoder()
enc = OneHotEncoder()
lb = preprocessing.LabelBinarizer()

X_train['protocol_type'] = le.fit_transform(X_train['protocol_type'])
X_test['protocol_type'] = le.fit_transform(X_test['protocol_type'])

Y_train['xAttack'] = le.fit_transform(Y_train['xAttack'])
lb.fit_transform(Y_train['xAttack'])
Y_test['xAttack'] = le.fit_transform(Y_test['xAttack'])
lb.fit_transform(Y_test['xAttack'])

con_list = ['protocol_type', 'service', 'flag', 'land', 'logged_in', 'su_attempted', 'is_host_login', 'is_guest_login']
con_train = X_train.drop(con_list, axis=1)
stdtrain = con_train.std(axis=0)
std_X_train = stdtrain.to_frame()

X_train = X_train.drop(['num_outbound_cmds'], axis=1)
X_test = X_test.drop(['num_outbound_cmds'], axis=1)
df_train = pd.concat([X_train, Y_train], axis=1)

stdrop_list = ['urgent', 'num_shells', 'root_shell', 'num_failed_logins', 'num_access_files', 'dst_host_srv_diff_host_rate', 'diff_srv_rate', 'dst_host_diff_srv_rate', 'wrong_fragment']
X_test_stdrop = X_test.drop(stdrop_list, axis=1)
X_train_stdrop = X_train.drop(stdrop_list, axis=1)
df_train_stdrop = pd.concat([X_train_stdrop, Y_train], axis=1)

AB = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=100, learning_rate=1.0)
RF = RandomForestClassifier(n_estimators=10, criterion='entropy', max_features='auto', bootstrap=True)
ET = ExtraTreesClassifier(n_estimators=10, criterion='gini', max_features='auto', bootstrap=False)
GB = GradientBoostingClassifier(loss='deviance', learning_rate=0.1, n_estimators=200, max_features='auto')

y_train = Y_train['xAttack'].ravel()
x_train = X_train.values
x_test = X_test.values

AB.fit(X_train, Y_train)

AB_feature = AB.feature_importances_
AB_feature
ab_score = AB.score(X_test, Y_test)

RF.fit(X_train, Y_train)

RF_feature = RF.feature_importances_
RF_feature
rf_score = RF.score(X_test, Y_test)

ET.fit(X_train, Y_train)

ET_feature = ET.feature_importances_
ET_feature
et_score = ET.score(X_test, Y_test)

GB.fit(X_train, Y_train)

GB_feature = GB.feature_importances_
GB_feature
gb_score = GB.score(X_test, Y_test)

cols = X_train.columns.values
feature_df = pd.DataFrame({'features': cols, 'AdaBoost': AB_feature, 'RandomForest': RF_feature, 'ExtraTree': ET_feature, 'GradientBoost': GB_feature})
feature_df.head(8)