# -*- coding: utf-8 -*-
"""CapstoneReview2.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1XS4vzrhfn7DdVnhXtThmQP-cgbZ0BC-3
"""

import pandas as pd

from google.colab import drive
drive.mount('/content/drive')

awid = pd.read_csv('/content/drive/My Drive/AWID/AWID-CLS-R-Trn.csv', header=0)
awid.columns= ['frame.interface_id',
 'frame.dlt',
 'frame.offset_shift',
 'frame.time_epoch',
 'frame.time_delta',
 'frame.time_delta_displayed',
 'frame.time_relative',
 'frame.len',
 'frame.cap_len',
 'frame.marked',
 'frame.ignored',
 'radiotap.version',
 'radiotap.pad',
 'radiotap.length',
 'radiotap.present.tsft',
 'radiotap.present.flags',
 'radiotap.present.rate',
 'radiotap.present.channel',
 'radiotap.present.fhss',
 'radiotap.present.dbm_antsignal',
 'radiotap.present.dbm_antnoise',
 'radiotap.present.lock_quality',
 'radiotap.present.tx_attenuation',
 'radiotap.present.db_tx_attenuation',
 'radiotap.present.dbm_tx_power',
 'radiotap.present.antenna',
 'radiotap.present.db_antsignal',
 'radiotap.present.db_antnoise',
 'radiotap.present.rxflags',
 'radiotap.present.xchannel',
 'radiotap.present.mcs',
 'radiotap.present.ampdu',
 'radiotap.present.vht',
 'radiotap.present.reserved',
 'radiotap.present.rtap_ns',
 'radiotap.present.vendor_ns',
 'radiotap.present.ext',
 'radiotap.mactime',
 'radiotap.flags.cfp',
 'radiotap.flags.preamble',
 'radiotap.flags.wep',
 'radiotap.flags.frag',
 'radiotap.flags.fcs',
 'radiotap.flags.datapad',
 'radiotap.flags.badfcs',
 'radiotap.flags.shortgi',
 'radiotap.datarate',
 'radiotap.channel.freq',
 'radiotap.channel.type.turbo',
 'radiotap.channel.type.cck',
 'radiotap.channel.type.ofdm',
 'radiotap.channel.type.2ghz',
 'radiotap.channel.type.5ghz',
 'radiotap.channel.type.passive',
 'radiotap.channel.type.dynamic',
 'radiotap.channel.type.gfsk',
 'radiotap.channel.type.gsm',
 'radiotap.channel.type.sturbo',
 'radiotap.channel.type.half',
 'radiotap.channel.type.quarter',
 'radiotap.dbm_antsignal',
 'radiotap.antenna',
 'radiotap.rxflags.badplcp',
 'wlan.fc.type_subtype',
 'wlan.fc.version',
 'wlan.fc.type',
 'wlan.fc.subtype',
 'wlan.fc.ds',
 'wlan.fc.frag',
 'wlan.fc.retry',
 'wlan.fc.pwrmgt',
 'wlan.fc.moredata',
 'wlan.fc.protected',
 'wlan.fc.order',
 'wlan.duration',
 'wlan.ra',
 'wlan.da',
 'wlan.ta',
 'wlan.sa',
 'wlan.bssid',
 'wlan.frag',
 'wlan.seq',
 'wlan.bar.type',
 'wlan.ba.control.ackpolicy',
 'wlan.ba.control.multitid',
 'wlan.ba.control.cbitmap',
 'wlan.bar.compressed.tidinfo',
 'wlan.ba.bm',
 'wlan.fcs_good',
 'wlan_mgt.fixed.capabilities.ess',
 'wlan_mgt.fixed.capabilities.ibss',
 'wlan_mgt.fixed.capabilities.cfpoll.ap',
 'wlan_mgt.fixed.capabilities.privacy',
 'wlan_mgt.fixed.capabilities.preamble',
 'wlan_mgt.fixed.capabilities.pbcc',
 'wlan_mgt.fixed.capabilities.agility',
 'wlan_mgt.fixed.capabilities.spec_man',
 'wlan_mgt.fixed.capabilities.short_slot_time',
 'wlan_mgt.fixed.capabilities.apsd',
 'wlan_mgt.fixed.capabilities.radio_measurement',
 'wlan_mgt.fixed.capabilities.dsss_ofdm',
 'wlan_mgt.fixed.capabilities.del_blk_ack',
 'wlan_mgt.fixed.capabilities.imm_blk_ack',
 'wlan_mgt.fixed.listen_ival',
 'wlan_mgt.fixed.current_ap',
 'wlan_mgt.fixed.status_code',
 'wlan_mgt.fixed.timestamp',
 'wlan_mgt.fixed.beacon',
 'wlan_mgt.fixed.aid',
 'wlan_mgt.fixed.reason_code',
 'wlan_mgt.fixed.auth.alg',
 'wlan_mgt.fixed.auth_seq',
 'wlan_mgt.fixed.category_code',
 'wlan_mgt.fixed.htact',
 'wlan_mgt.fixed.chanwidth',
 'wlan_mgt.fixed.fragment',
 'wlan_mgt.fixed.sequence',
 'wlan_mgt.tagged.all',
 'wlan_mgt.ssid',
 'wlan_mgt.ds.current_channel',
 'wlan_mgt.tim.dtim_count',
 'wlan_mgt.tim.dtim_period',
 'wlan_mgt.tim.bmapctl.multicast',
 'wlan_mgt.tim.bmapctl.offset',
 'wlan_mgt.country_info.environment',
 'wlan_mgt.rsn.version',
 'wlan_mgt.rsn.gcs.type',
 'wlan_mgt.rsn.pcs.count',
 'wlan_mgt.rsn.akms.count',
 'wlan_mgt.rsn.akms.type',
 'wlan_mgt.rsn.capabilities.preauth',
 'wlan_mgt.rsn.capabilities.no_pairwise',
 'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
 'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
 'wlan_mgt.rsn.capabilities.mfpr',
 'wlan_mgt.rsn.capabilities.mfpc',
 'wlan_mgt.rsn.capabilities.peerkey',
 'wlan_mgt.tcprep.trsmt_pow',
 'wlan_mgt.tcprep.link_mrg',
 'wlan.wep.iv',
 'wlan.wep.key',
 'wlan.wep.icv',
 'wlan.tkip.extiv',
 'wlan.ccmp.extiv',
 'wlan.qos.tid',
 'wlan.qos.priority',
 'wlan.qos.eosp',
 'wlan.qos.ack',
 'wlan.qos.amsdupresent',
 'wlan.qos.buf_state_indicated',
 'wlan.qos.bit4',
 'wlan.qos.txop_dur_req',
 'wlan.qos.buf_state_indicated',
 'data.len',
 'class']

awid.shape

awid.head()

awid['class'].value_counts(normalize=True)

awid.isna().sum()

awid.replace({"?": None}, inplace=True)

awid.isna().sum()

null_data_columns = awid.columns[awid.isnull().mean() >= 0.5]

null_data_columns.shape

awid.drop(null_data_columns, axis=1, inplace=True)

awid.shape

awid.dropna(inplace=True)
awid.shape

awid['class'].value_counts(normalize=True)

awid.select_dtypes(['number']).shape

for col in awid.columns:
    awid[col] = pd.to_numeric(awid[col], errors='ignore')

awid.select_dtypes(['number']).shape

awid.describe()

X, y = awid.select_dtypes(['number']), awid['class']

from sklearn.naive_bayes import GaussianNB

nb = GaussianNB()

# fit our model to the data
nb.fit(X, y)



#TESTING


awid_test = pd.read_csv("/content/drive/My Drive/AWID/AWID-CLS-R-Tst.csv", header=None)
awid_test.columns= ['frame.interface_id',
 'frame.dlt',
 'frame.offset_shift',
 'frame.time_epoch',
 'frame.time_delta',
 'frame.time_delta_displayed',
 'frame.time_relative',
 'frame.len',
 'frame.cap_len',
 'frame.marked',
 'frame.ignored',
 'radiotap.version',
 'radiotap.pad',
 'radiotap.length',
 'radiotap.present.tsft',
 'radiotap.present.flags',
 'radiotap.present.rate',
 'radiotap.present.channel',
 'radiotap.present.fhss',
 'radiotap.present.dbm_antsignal',
 'radiotap.present.dbm_antnoise',
 'radiotap.present.lock_quality',
 'radiotap.present.tx_attenuation',
 'radiotap.present.db_tx_attenuation',
 'radiotap.present.dbm_tx_power',
 'radiotap.present.antenna',
 'radiotap.present.db_antsignal',
 'radiotap.present.db_antnoise',
 'radiotap.present.rxflags',
 'radiotap.present.xchannel',
 'radiotap.present.mcs',
 'radiotap.present.ampdu',
 'radiotap.present.vht',
 'radiotap.present.reserved',
 'radiotap.present.rtap_ns',
 'radiotap.present.vendor_ns',
 'radiotap.present.ext',
 'radiotap.mactime',
 'radiotap.flags.cfp',
 'radiotap.flags.preamble',
 'radiotap.flags.wep',
 'radiotap.flags.frag',
 'radiotap.flags.fcs',
 'radiotap.flags.datapad',
 'radiotap.flags.badfcs',
 'radiotap.flags.shortgi',
 'radiotap.datarate',
 'radiotap.channel.freq',
 'radiotap.channel.type.turbo',
 'radiotap.channel.type.cck',
 'radiotap.channel.type.ofdm',
 'radiotap.channel.type.2ghz',
 'radiotap.channel.type.5ghz',
 'radiotap.channel.type.passive',
 'radiotap.channel.type.dynamic',
 'radiotap.channel.type.gfsk',
 'radiotap.channel.type.gsm',
 'radiotap.channel.type.sturbo',
 'radiotap.channel.type.half',
 'radiotap.channel.type.quarter',
 'radiotap.dbm_antsignal',
 'radiotap.antenna',
 'radiotap.rxflags.badplcp',
 'wlan.fc.type_subtype',
 'wlan.fc.version',
 'wlan.fc.type',
 'wlan.fc.subtype',
 'wlan.fc.ds',
 'wlan.fc.frag',
 'wlan.fc.retry',
 'wlan.fc.pwrmgt',
 'wlan.fc.moredata',
 'wlan.fc.protected',
 'wlan.fc.order',
 'wlan.duration',
 'wlan.ra',
 'wlan.da',
 'wlan.ta',
 'wlan.sa',
 'wlan.bssid',
 'wlan.frag',
 'wlan.seq',
 'wlan.bar.type',
 'wlan.ba.control.ackpolicy',
 'wlan.ba.control.multitid',
 'wlan.ba.control.cbitmap',
 'wlan.bar.compressed.tidinfo',
 'wlan.ba.bm',
 'wlan.fcs_good',
 'wlan_mgt.fixed.capabilities.ess',
 'wlan_mgt.fixed.capabilities.ibss',
 'wlan_mgt.fixed.capabilities.cfpoll.ap',
 'wlan_mgt.fixed.capabilities.privacy',
 'wlan_mgt.fixed.capabilities.preamble',
 'wlan_mgt.fixed.capabilities.pbcc',
 'wlan_mgt.fixed.capabilities.agility',
 'wlan_mgt.fixed.capabilities.spec_man',
 'wlan_mgt.fixed.capabilities.short_slot_time',
 'wlan_mgt.fixed.capabilities.apsd',
 'wlan_mgt.fixed.capabilities.radio_measurement',
 'wlan_mgt.fixed.capabilities.dsss_ofdm',
 'wlan_mgt.fixed.capabilities.del_blk_ack',
 'wlan_mgt.fixed.capabilities.imm_blk_ack',
 'wlan_mgt.fixed.listen_ival',
 'wlan_mgt.fixed.current_ap',
 'wlan_mgt.fixed.status_code',
 'wlan_mgt.fixed.timestamp',
 'wlan_mgt.fixed.beacon',
 'wlan_mgt.fixed.aid',
 'wlan_mgt.fixed.reason_code',
 'wlan_mgt.fixed.auth.alg',
 'wlan_mgt.fixed.auth_seq',
 'wlan_mgt.fixed.category_code',
 'wlan_mgt.fixed.htact',
 'wlan_mgt.fixed.chanwidth',
 'wlan_mgt.fixed.fragment',
 'wlan_mgt.fixed.sequence',
 'wlan_mgt.tagged.all',
 'wlan_mgt.ssid',
 'wlan_mgt.ds.current_channel',
 'wlan_mgt.tim.dtim_count',
 'wlan_mgt.tim.dtim_period',
 'wlan_mgt.tim.bmapctl.multicast',
 'wlan_mgt.tim.bmapctl.offset',
 'wlan_mgt.country_info.environment',
 'wlan_mgt.rsn.version',
 'wlan_mgt.rsn.gcs.type',
 'wlan_mgt.rsn.pcs.count',
 'wlan_mgt.rsn.akms.count',
 'wlan_mgt.rsn.akms.type',
 'wlan_mgt.rsn.capabilities.preauth',
 'wlan_mgt.rsn.capabilities.no_pairwise',
 'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
 'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
 'wlan_mgt.rsn.capabilities.mfpr',
 'wlan_mgt.rsn.capabilities.mfpc',
 'wlan_mgt.rsn.capabilities.peerkey',
 'wlan_mgt.tcprep.trsmt_pow',
 'wlan_mgt.tcprep.link_mrg',
 'wlan.wep.iv',
 'wlan.wep.key',
 'wlan.wep.icv',
 'wlan.tkip.extiv',
 'wlan.ccmp.extiv',
 'wlan.qos.tid',
 'wlan.qos.priority',
 'wlan.qos.eosp',
 'wlan.qos.ack',
 'wlan.qos.amsdupresent',
 'wlan.qos.buf_state_indicated',
 'wlan.qos.bit4',
 'wlan.qos.txop_dur_req',
 'wlan.qos.buf_state_indicated',
 'data.len',
 'class']
awid_test.drop(null_data_columns, axis=1, inplace=True)
awid_test.replace({"?": None}, inplace=True)
awid_test.dropna(inplace=True)  

for col in awid_test.columns:
    awid_test[col] = pd.to_numeric(awid_test[col], errors='ignore')
awid_test.shape

from sklearn.metrics import accuracy_score
X_test = awid_test.select_dtypes(['number'])
y_test = awid_test['class']

def get_test_accuracy_of(model):
    y_preds = model.predict(X_test)
    return accuracy_score(y_preds, y_test)
    
get_test_accuracy_of(nb)

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()

lr.fit(X, y)

get_test_accuracy_of(lr)

from sklearn.tree import DecisionTreeClassifier

tree = DecisionTreeClassifier()

tree.fit(X, y)

# Tree does very well!
get_test_accuracy_of(tree)

pd.DataFrame({'feature':awid.select_dtypes(['number']).columns, 
              'importance':tree.feature_importances_}).sort_values('importance', ascending=False).head(10)

from sklearn.ensemble import RandomForestClassifier

forest = RandomForestClassifier()

forest.fit(X, y)

get_test_accuracy_of(forest)

#Pipeline to Decision Tree
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV

preprocessing = Pipeline([
    ("scale", StandardScaler()),
])

pipeline = Pipeline([
    ("preprocessing", preprocessing),
    ("classifier", DecisionTreeClassifier())
])

params = {
    "classifier__max_depth": [None, 3, 5, 10], 
         }

grid = GridSearchCV(pipeline, params)
grid.fit(X, y)

get_test_accuracy_of(grid.best_estimator_)

#Pipeline to Random Forest
preprocessing = Pipeline([
    ("scale", StandardScaler()),
])

pipeline = Pipeline([
    ("preprocessing", preprocessing),
    ("classifier", RandomForestClassifier())
])

params = {
    "classifier__max_depth": [None, 3, 5, 10], 
         }

grid = GridSearchCV(pipeline, params)
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)






a3ea49e6ab7894690a2b7800d91465d8ba9a073b
<mask token>


def get_test_accuracy_of(model):
    y_preds = model.predict(X_test)
    return accuracy_score(y_preds, y_test)


<mask token>

<mask token>
drive.mount('/content/drive')
<mask token>
awid.shape
awid.head()
awid['class'].value_counts(normalize=True)
awid.isna().sum()
awid.replace({'?': None}, inplace=True)
awid.isna().sum()
<mask token>
null_data_columns.shape
awid.drop(null_data_columns, axis=1, inplace=True)
awid.shape
awid.dropna(inplace=True)
awid.shape
awid['class'].value_counts(normalize=True)
awid.select_dtypes(['number']).shape
for col in awid.columns:
    awid[col] = pd.to_numeric(awid[col], errors='ignore')
awid.select_dtypes(['number']).shape
awid.describe()
<mask token>
nb.fit(X, y)
<mask token>
awid_test.drop(null_data_columns, axis=1, inplace=True)
awid_test.replace({'?': None}, inplace=True)
awid_test.dropna(inplace=True)
for col in awid_test.columns:
    awid_test[col] = pd.to_numeric(awid_test[col], errors='ignore')
awid_test.shape
<mask token>


def get_test_accuracy_of(model):
    y_preds = model.predict(X_test)
    return accuracy_score(y_preds, y_test)


get_test_accuracy_of(nb)
<mask token>
lr.fit(X, y)
get_test_accuracy_of(lr)
<mask token>
tree.fit(X, y)
get_test_accuracy_of(tree)
pd.DataFrame({'feature': awid.select_dtypes(['number']).columns,
    'importance': tree.feature_importances_}).sort_values('importance',
    ascending=False).head(10)
<mask token>
forest.fit(X, y)
get_test_accuracy_of(forest)
<mask token>
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)
<mask token>
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)

<mask token>
drive.mount('/content/drive')
awid = pd.read_csv('/content/drive/My Drive/AWID/AWID-CLS-R-Trn.csv', header=0)
awid.columns = ['frame.interface_id', 'frame.dlt', 'frame.offset_shift',
    'frame.time_epoch', 'frame.time_delta', 'frame.time_delta_displayed',
    'frame.time_relative', 'frame.len', 'frame.cap_len', 'frame.marked',
    'frame.ignored', 'radiotap.version', 'radiotap.pad', 'radiotap.length',
    'radiotap.present.tsft', 'radiotap.present.flags',
    'radiotap.present.rate', 'radiotap.present.channel',
    'radiotap.present.fhss', 'radiotap.present.dbm_antsignal',
    'radiotap.present.dbm_antnoise', 'radiotap.present.lock_quality',
    'radiotap.present.tx_attenuation', 'radiotap.present.db_tx_attenuation',
    'radiotap.present.dbm_tx_power', 'radiotap.present.antenna',
    'radiotap.present.db_antsignal', 'radiotap.present.db_antnoise',
    'radiotap.present.rxflags', 'radiotap.present.xchannel',
    'radiotap.present.mcs', 'radiotap.present.ampdu',
    'radiotap.present.vht', 'radiotap.present.reserved',
    'radiotap.present.rtap_ns', 'radiotap.present.vendor_ns',
    'radiotap.present.ext', 'radiotap.mactime', 'radiotap.flags.cfp',
    'radiotap.flags.preamble', 'radiotap.flags.wep', 'radiotap.flags.frag',
    'radiotap.flags.fcs', 'radiotap.flags.datapad', 'radiotap.flags.badfcs',
    'radiotap.flags.shortgi', 'radiotap.datarate', 'radiotap.channel.freq',
    'radiotap.channel.type.turbo', 'radiotap.channel.type.cck',
    'radiotap.channel.type.ofdm', 'radiotap.channel.type.2ghz',
    'radiotap.channel.type.5ghz', 'radiotap.channel.type.passive',
    'radiotap.channel.type.dynamic', 'radiotap.channel.type.gfsk',
    'radiotap.channel.type.gsm', 'radiotap.channel.type.sturbo',
    'radiotap.channel.type.half', 'radiotap.channel.type.quarter',
    'radiotap.dbm_antsignal', 'radiotap.antenna',
    'radiotap.rxflags.badplcp', 'wlan.fc.type_subtype', 'wlan.fc.version',
    'wlan.fc.type', 'wlan.fc.subtype', 'wlan.fc.ds', 'wlan.fc.frag',
    'wlan.fc.retry', 'wlan.fc.pwrmgt', 'wlan.fc.moredata',
    'wlan.fc.protected', 'wlan.fc.order', 'wlan.duration', 'wlan.ra',
    'wlan.da', 'wlan.ta', 'wlan.sa', 'wlan.bssid', 'wlan.frag', 'wlan.seq',
    'wlan.bar.type', 'wlan.ba.control.ackpolicy',
    'wlan.ba.control.multitid', 'wlan.ba.control.cbitmap',
    'wlan.bar.compressed.tidinfo', 'wlan.ba.bm', 'wlan.fcs_good',
    'wlan_mgt.fixed.capabilities.ess', 'wlan_mgt.fixed.capabilities.ibss',
    'wlan_mgt.fixed.capabilities.cfpoll.ap',
    'wlan_mgt.fixed.capabilities.privacy',
    'wlan_mgt.fixed.capabilities.preamble',
    'wlan_mgt.fixed.capabilities.pbcc',
    'wlan_mgt.fixed.capabilities.agility',
    'wlan_mgt.fixed.capabilities.spec_man',
    'wlan_mgt.fixed.capabilities.short_slot_time',
    'wlan_mgt.fixed.capabilities.apsd',
    'wlan_mgt.fixed.capabilities.radio_measurement',
    'wlan_mgt.fixed.capabilities.dsss_ofdm',
    'wlan_mgt.fixed.capabilities.del_blk_ack',
    'wlan_mgt.fixed.capabilities.imm_blk_ack', 'wlan_mgt.fixed.listen_ival',
    'wlan_mgt.fixed.current_ap', 'wlan_mgt.fixed.status_code',
    'wlan_mgt.fixed.timestamp', 'wlan_mgt.fixed.beacon',
    'wlan_mgt.fixed.aid', 'wlan_mgt.fixed.reason_code',
    'wlan_mgt.fixed.auth.alg', 'wlan_mgt.fixed.auth_seq',
    'wlan_mgt.fixed.category_code', 'wlan_mgt.fixed.htact',
    'wlan_mgt.fixed.chanwidth', 'wlan_mgt.fixed.fragment',
    'wlan_mgt.fixed.sequence', 'wlan_mgt.tagged.all', 'wlan_mgt.ssid',
    'wlan_mgt.ds.current_channel', 'wlan_mgt.tim.dtim_count',
    'wlan_mgt.tim.dtim_period', 'wlan_mgt.tim.bmapctl.multicast',
    'wlan_mgt.tim.bmapctl.offset', 'wlan_mgt.country_info.environment',
    'wlan_mgt.rsn.version', 'wlan_mgt.rsn.gcs.type',
    'wlan_mgt.rsn.pcs.count', 'wlan_mgt.rsn.akms.count',
    'wlan_mgt.rsn.akms.type', 'wlan_mgt.rsn.capabilities.preauth',
    'wlan_mgt.rsn.capabilities.no_pairwise',
    'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
    'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
    'wlan_mgt.rsn.capabilities.mfpr', 'wlan_mgt.rsn.capabilities.mfpc',
    'wlan_mgt.rsn.capabilities.peerkey', 'wlan_mgt.tcprep.trsmt_pow',
    'wlan_mgt.tcprep.link_mrg', 'wlan.wep.iv', 'wlan.wep.key',
    'wlan.wep.icv', 'wlan.tkip.extiv', 'wlan.ccmp.extiv', 'wlan.qos.tid',
    'wlan.qos.priority', 'wlan.qos.eosp', 'wlan.qos.ack',
    'wlan.qos.amsdupresent', 'wlan.qos.buf_state_indicated',
    'wlan.qos.bit4', 'wlan.qos.txop_dur_req',
    'wlan.qos.buf_state_indicated', 'data.len', 'class']
awid.shape
awid.head()
awid['class'].value_counts(normalize=True)
awid.isna().sum()
awid.replace({'?': None}, inplace=True)
awid.isna().sum()
null_data_columns = awid.columns[awid.isnull().mean() >= 0.5]
null_data_columns.shape
awid.drop(null_data_columns, axis=1, inplace=True)
awid.shape
awid.dropna(inplace=True)
awid.shape
awid['class'].value_counts(normalize=True)
awid.select_dtypes(['number']).shape
for col in awid.columns:
    awid[col] = pd.to_numeric(awid[col], errors='ignore')
awid.select_dtypes(['number']).shape
awid.describe()
X, y = awid.select_dtypes(['number']), awid['class']
<mask token>
nb = GaussianNB()
nb.fit(X, y)
awid_test = pd.read_csv('/content/drive/My Drive/AWID/AWID-CLS-R-Tst.csv',
    header=None)
awid_test.columns = ['frame.interface_id', 'frame.dlt',
    'frame.offset_shift', 'frame.time_epoch', 'frame.time_delta',
    'frame.time_delta_displayed', 'frame.time_relative', 'frame.len',
    'frame.cap_len', 'frame.marked', 'frame.ignored', 'radiotap.version',
    'radiotap.pad', 'radiotap.length', 'radiotap.present.tsft',
    'radiotap.present.flags', 'radiotap.present.rate',
    'radiotap.present.channel', 'radiotap.present.fhss',
    'radiotap.present.dbm_antsignal', 'radiotap.present.dbm_antnoise',
    'radiotap.present.lock_quality', 'radiotap.present.tx_attenuation',
    'radiotap.present.db_tx_attenuation', 'radiotap.present.dbm_tx_power',
    'radiotap.present.antenna', 'radiotap.present.db_antsignal',
    'radiotap.present.db_antnoise', 'radiotap.present.rxflags',
    'radiotap.present.xchannel', 'radiotap.present.mcs',
    'radiotap.present.ampdu', 'radiotap.present.vht',
    'radiotap.present.reserved', 'radiotap.present.rtap_ns',
    'radiotap.present.vendor_ns', 'radiotap.present.ext',
    'radiotap.mactime', 'radiotap.flags.cfp', 'radiotap.flags.preamble',
    'radiotap.flags.wep', 'radiotap.flags.frag', 'radiotap.flags.fcs',
    'radiotap.flags.datapad', 'radiotap.flags.badfcs',
    'radiotap.flags.shortgi', 'radiotap.datarate', 'radiotap.channel.freq',
    'radiotap.channel.type.turbo', 'radiotap.channel.type.cck',
    'radiotap.channel.type.ofdm', 'radiotap.channel.type.2ghz',
    'radiotap.channel.type.5ghz', 'radiotap.channel.type.passive',
    'radiotap.channel.type.dynamic', 'radiotap.channel.type.gfsk',
    'radiotap.channel.type.gsm', 'radiotap.channel.type.sturbo',
    'radiotap.channel.type.half', 'radiotap.channel.type.quarter',
    'radiotap.dbm_antsignal', 'radiotap.antenna',
    'radiotap.rxflags.badplcp', 'wlan.fc.type_subtype', 'wlan.fc.version',
    'wlan.fc.type', 'wlan.fc.subtype', 'wlan.fc.ds', 'wlan.fc.frag',
    'wlan.fc.retry', 'wlan.fc.pwrmgt', 'wlan.fc.moredata',
    'wlan.fc.protected', 'wlan.fc.order', 'wlan.duration', 'wlan.ra',
    'wlan.da', 'wlan.ta', 'wlan.sa', 'wlan.bssid', 'wlan.frag', 'wlan.seq',
    'wlan.bar.type', 'wlan.ba.control.ackpolicy',
    'wlan.ba.control.multitid', 'wlan.ba.control.cbitmap',
    'wlan.bar.compressed.tidinfo', 'wlan.ba.bm', 'wlan.fcs_good',
    'wlan_mgt.fixed.capabilities.ess', 'wlan_mgt.fixed.capabilities.ibss',
    'wlan_mgt.fixed.capabilities.cfpoll.ap',
    'wlan_mgt.fixed.capabilities.privacy',
    'wlan_mgt.fixed.capabilities.preamble',
    'wlan_mgt.fixed.capabilities.pbcc',
    'wlan_mgt.fixed.capabilities.agility',
    'wlan_mgt.fixed.capabilities.spec_man',
    'wlan_mgt.fixed.capabilities.short_slot_time',
    'wlan_mgt.fixed.capabilities.apsd',
    'wlan_mgt.fixed.capabilities.radio_measurement',
    'wlan_mgt.fixed.capabilities.dsss_ofdm',
    'wlan_mgt.fixed.capabilities.del_blk_ack',
    'wlan_mgt.fixed.capabilities.imm_blk_ack', 'wlan_mgt.fixed.listen_ival',
    'wlan_mgt.fixed.current_ap', 'wlan_mgt.fixed.status_code',
    'wlan_mgt.fixed.timestamp', 'wlan_mgt.fixed.beacon',
    'wlan_mgt.fixed.aid', 'wlan_mgt.fixed.reason_code',
    'wlan_mgt.fixed.auth.alg', 'wlan_mgt.fixed.auth_seq',
    'wlan_mgt.fixed.category_code', 'wlan_mgt.fixed.htact',
    'wlan_mgt.fixed.chanwidth', 'wlan_mgt.fixed.fragment',
    'wlan_mgt.fixed.sequence', 'wlan_mgt.tagged.all', 'wlan_mgt.ssid',
    'wlan_mgt.ds.current_channel', 'wlan_mgt.tim.dtim_count',
    'wlan_mgt.tim.dtim_period', 'wlan_mgt.tim.bmapctl.multicast',
    'wlan_mgt.tim.bmapctl.offset', 'wlan_mgt.country_info.environment',
    'wlan_mgt.rsn.version', 'wlan_mgt.rsn.gcs.type',
    'wlan_mgt.rsn.pcs.count', 'wlan_mgt.rsn.akms.count',
    'wlan_mgt.rsn.akms.type', 'wlan_mgt.rsn.capabilities.preauth',
    'wlan_mgt.rsn.capabilities.no_pairwise',
    'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
    'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
    'wlan_mgt.rsn.capabilities.mfpr', 'wlan_mgt.rsn.capabilities.mfpc',
    'wlan_mgt.rsn.capabilities.peerkey', 'wlan_mgt.tcprep.trsmt_pow',
    'wlan_mgt.tcprep.link_mrg', 'wlan.wep.iv', 'wlan.wep.key',
    'wlan.wep.icv', 'wlan.tkip.extiv', 'wlan.ccmp.extiv', 'wlan.qos.tid',
    'wlan.qos.priority', 'wlan.qos.eosp', 'wlan.qos.ack',
    'wlan.qos.amsdupresent', 'wlan.qos.buf_state_indicated',
    'wlan.qos.bit4', 'wlan.qos.txop_dur_req',
    'wlan.qos.buf_state_indicated', 'data.len', 'class']
awid_test.drop(null_data_columns, axis=1, inplace=True)
awid_test.replace({'?': None}, inplace=True)
awid_test.dropna(inplace=True)
for col in awid_test.columns:
    awid_test[col] = pd.to_numeric(awid_test[col], errors='ignore')
awid_test.shape
<mask token>
X_test = awid_test.select_dtypes(['number'])
y_test = awid_test['class']


def get_test_accuracy_of(model):
    y_preds = model.predict(X_test)
    return accuracy_score(y_preds, y_test)


get_test_accuracy_of(nb)
<mask token>
lr = LogisticRegression()
lr.fit(X, y)
get_test_accuracy_of(lr)
<mask token>
tree = DecisionTreeClassifier()
tree.fit(X, y)
get_test_accuracy_of(tree)
pd.DataFrame({'feature': awid.select_dtypes(['number']).columns,
    'importance': tree.feature_importances_}).sort_values('importance',
    ascending=False).head(10)
<mask token>
forest = RandomForestClassifier()
forest.fit(X, y)
get_test_accuracy_of(forest)
<mask token>
preprocessing = Pipeline([('scale', StandardScaler())])
pipeline = Pipeline([('preprocessing', preprocessing), ('classifier',
    DecisionTreeClassifier())])
params = {'classifier__max_depth': [None, 3, 5, 10]}
grid = GridSearchCV(pipeline, params)
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)
preprocessing = Pipeline([('scale', StandardScaler())])
pipeline = Pipeline([('preprocessing', preprocessing), ('classifier',
    RandomForestClassifier())])
params = {'classifier__max_depth': [None, 3, 5, 10]}
grid = GridSearchCV(pipeline, params)
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)

<mask token>
import pandas as pd
from google.colab import drive
drive.mount('/content/drive')
awid = pd.read_csv('/content/drive/My Drive/AWID/AWID-CLS-R-Trn.csv', header=0)
awid.columns = ['frame.interface_id', 'frame.dlt', 'frame.offset_shift',
    'frame.time_epoch', 'frame.time_delta', 'frame.time_delta_displayed',
    'frame.time_relative', 'frame.len', 'frame.cap_len', 'frame.marked',
    'frame.ignored', 'radiotap.version', 'radiotap.pad', 'radiotap.length',
    'radiotap.present.tsft', 'radiotap.present.flags',
    'radiotap.present.rate', 'radiotap.present.channel',
    'radiotap.present.fhss', 'radiotap.present.dbm_antsignal',
    'radiotap.present.dbm_antnoise', 'radiotap.present.lock_quality',
    'radiotap.present.tx_attenuation', 'radiotap.present.db_tx_attenuation',
    'radiotap.present.dbm_tx_power', 'radiotap.present.antenna',
    'radiotap.present.db_antsignal', 'radiotap.present.db_antnoise',
    'radiotap.present.rxflags', 'radiotap.present.xchannel',
    'radiotap.present.mcs', 'radiotap.present.ampdu',
    'radiotap.present.vht', 'radiotap.present.reserved',
    'radiotap.present.rtap_ns', 'radiotap.present.vendor_ns',
    'radiotap.present.ext', 'radiotap.mactime', 'radiotap.flags.cfp',
    'radiotap.flags.preamble', 'radiotap.flags.wep', 'radiotap.flags.frag',
    'radiotap.flags.fcs', 'radiotap.flags.datapad', 'radiotap.flags.badfcs',
    'radiotap.flags.shortgi', 'radiotap.datarate', 'radiotap.channel.freq',
    'radiotap.channel.type.turbo', 'radiotap.channel.type.cck',
    'radiotap.channel.type.ofdm', 'radiotap.channel.type.2ghz',
    'radiotap.channel.type.5ghz', 'radiotap.channel.type.passive',
    'radiotap.channel.type.dynamic', 'radiotap.channel.type.gfsk',
    'radiotap.channel.type.gsm', 'radiotap.channel.type.sturbo',
    'radiotap.channel.type.half', 'radiotap.channel.type.quarter',
    'radiotap.dbm_antsignal', 'radiotap.antenna',
    'radiotap.rxflags.badplcp', 'wlan.fc.type_subtype', 'wlan.fc.version',
    'wlan.fc.type', 'wlan.fc.subtype', 'wlan.fc.ds', 'wlan.fc.frag',
    'wlan.fc.retry', 'wlan.fc.pwrmgt', 'wlan.fc.moredata',
    'wlan.fc.protected', 'wlan.fc.order', 'wlan.duration', 'wlan.ra',
    'wlan.da', 'wlan.ta', 'wlan.sa', 'wlan.bssid', 'wlan.frag', 'wlan.seq',
    'wlan.bar.type', 'wlan.ba.control.ackpolicy',
    'wlan.ba.control.multitid', 'wlan.ba.control.cbitmap',
    'wlan.bar.compressed.tidinfo', 'wlan.ba.bm', 'wlan.fcs_good',
    'wlan_mgt.fixed.capabilities.ess', 'wlan_mgt.fixed.capabilities.ibss',
    'wlan_mgt.fixed.capabilities.cfpoll.ap',
    'wlan_mgt.fixed.capabilities.privacy',
    'wlan_mgt.fixed.capabilities.preamble',
    'wlan_mgt.fixed.capabilities.pbcc',
    'wlan_mgt.fixed.capabilities.agility',
    'wlan_mgt.fixed.capabilities.spec_man',
    'wlan_mgt.fixed.capabilities.short_slot_time',
    'wlan_mgt.fixed.capabilities.apsd',
    'wlan_mgt.fixed.capabilities.radio_measurement',
    'wlan_mgt.fixed.capabilities.dsss_ofdm',
    'wlan_mgt.fixed.capabilities.del_blk_ack',
    'wlan_mgt.fixed.capabilities.imm_blk_ack', 'wlan_mgt.fixed.listen_ival',
    'wlan_mgt.fixed.current_ap', 'wlan_mgt.fixed.status_code',
    'wlan_mgt.fixed.timestamp', 'wlan_mgt.fixed.beacon',
    'wlan_mgt.fixed.aid', 'wlan_mgt.fixed.reason_code',
    'wlan_mgt.fixed.auth.alg', 'wlan_mgt.fixed.auth_seq',
    'wlan_mgt.fixed.category_code', 'wlan_mgt.fixed.htact',
    'wlan_mgt.fixed.chanwidth', 'wlan_mgt.fixed.fragment',
    'wlan_mgt.fixed.sequence', 'wlan_mgt.tagged.all', 'wlan_mgt.ssid',
    'wlan_mgt.ds.current_channel', 'wlan_mgt.tim.dtim_count',
    'wlan_mgt.tim.dtim_period', 'wlan_mgt.tim.bmapctl.multicast',
    'wlan_mgt.tim.bmapctl.offset', 'wlan_mgt.country_info.environment',
    'wlan_mgt.rsn.version', 'wlan_mgt.rsn.gcs.type',
    'wlan_mgt.rsn.pcs.count', 'wlan_mgt.rsn.akms.count',
    'wlan_mgt.rsn.akms.type', 'wlan_mgt.rsn.capabilities.preauth',
    'wlan_mgt.rsn.capabilities.no_pairwise',
    'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
    'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
    'wlan_mgt.rsn.capabilities.mfpr', 'wlan_mgt.rsn.capabilities.mfpc',
    'wlan_mgt.rsn.capabilities.peerkey', 'wlan_mgt.tcprep.trsmt_pow',
    'wlan_mgt.tcprep.link_mrg', 'wlan.wep.iv', 'wlan.wep.key',
    'wlan.wep.icv', 'wlan.tkip.extiv', 'wlan.ccmp.extiv', 'wlan.qos.tid',
    'wlan.qos.priority', 'wlan.qos.eosp', 'wlan.qos.ack',
    'wlan.qos.amsdupresent', 'wlan.qos.buf_state_indicated',
    'wlan.qos.bit4', 'wlan.qos.txop_dur_req',
    'wlan.qos.buf_state_indicated', 'data.len', 'class']
awid.shape
awid.head()
awid['class'].value_counts(normalize=True)
awid.isna().sum()
awid.replace({'?': None}, inplace=True)
awid.isna().sum()
null_data_columns = awid.columns[awid.isnull().mean() >= 0.5]
null_data_columns.shape
awid.drop(null_data_columns, axis=1, inplace=True)
awid.shape
awid.dropna(inplace=True)
awid.shape
awid['class'].value_counts(normalize=True)
awid.select_dtypes(['number']).shape
for col in awid.columns:
    awid[col] = pd.to_numeric(awid[col], errors='ignore')
awid.select_dtypes(['number']).shape
awid.describe()
X, y = awid.select_dtypes(['number']), awid['class']
from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(X, y)
awid_test = pd.read_csv('/content/drive/My Drive/AWID/AWID-CLS-R-Tst.csv',
    header=None)
awid_test.columns = ['frame.interface_id', 'frame.dlt',
    'frame.offset_shift', 'frame.time_epoch', 'frame.time_delta',
    'frame.time_delta_displayed', 'frame.time_relative', 'frame.len',
    'frame.cap_len', 'frame.marked', 'frame.ignored', 'radiotap.version',
    'radiotap.pad', 'radiotap.length', 'radiotap.present.tsft',
    'radiotap.present.flags', 'radiotap.present.rate',
    'radiotap.present.channel', 'radiotap.present.fhss',
    'radiotap.present.dbm_antsignal', 'radiotap.present.dbm_antnoise',
    'radiotap.present.lock_quality', 'radiotap.present.tx_attenuation',
    'radiotap.present.db_tx_attenuation', 'radiotap.present.dbm_tx_power',
    'radiotap.present.antenna', 'radiotap.present.db_antsignal',
    'radiotap.present.db_antnoise', 'radiotap.present.rxflags',
    'radiotap.present.xchannel', 'radiotap.present.mcs',
    'radiotap.present.ampdu', 'radiotap.present.vht',
    'radiotap.present.reserved', 'radiotap.present.rtap_ns',
    'radiotap.present.vendor_ns', 'radiotap.present.ext',
    'radiotap.mactime', 'radiotap.flags.cfp', 'radiotap.flags.preamble',
    'radiotap.flags.wep', 'radiotap.flags.frag', 'radiotap.flags.fcs',
    'radiotap.flags.datapad', 'radiotap.flags.badfcs',
    'radiotap.flags.shortgi', 'radiotap.datarate', 'radiotap.channel.freq',
    'radiotap.channel.type.turbo', 'radiotap.channel.type.cck',
    'radiotap.channel.type.ofdm', 'radiotap.channel.type.2ghz',
    'radiotap.channel.type.5ghz', 'radiotap.channel.type.passive',
    'radiotap.channel.type.dynamic', 'radiotap.channel.type.gfsk',
    'radiotap.channel.type.gsm', 'radiotap.channel.type.sturbo',
    'radiotap.channel.type.half', 'radiotap.channel.type.quarter',
    'radiotap.dbm_antsignal', 'radiotap.antenna',
    'radiotap.rxflags.badplcp', 'wlan.fc.type_subtype', 'wlan.fc.version',
    'wlan.fc.type', 'wlan.fc.subtype', 'wlan.fc.ds', 'wlan.fc.frag',
    'wlan.fc.retry', 'wlan.fc.pwrmgt', 'wlan.fc.moredata',
    'wlan.fc.protected', 'wlan.fc.order', 'wlan.duration', 'wlan.ra',
    'wlan.da', 'wlan.ta', 'wlan.sa', 'wlan.bssid', 'wlan.frag', 'wlan.seq',
    'wlan.bar.type', 'wlan.ba.control.ackpolicy',
    'wlan.ba.control.multitid', 'wlan.ba.control.cbitmap',
    'wlan.bar.compressed.tidinfo', 'wlan.ba.bm', 'wlan.fcs_good',
    'wlan_mgt.fixed.capabilities.ess', 'wlan_mgt.fixed.capabilities.ibss',
    'wlan_mgt.fixed.capabilities.cfpoll.ap',
    'wlan_mgt.fixed.capabilities.privacy',
    'wlan_mgt.fixed.capabilities.preamble',
    'wlan_mgt.fixed.capabilities.pbcc',
    'wlan_mgt.fixed.capabilities.agility',
    'wlan_mgt.fixed.capabilities.spec_man',
    'wlan_mgt.fixed.capabilities.short_slot_time',
    'wlan_mgt.fixed.capabilities.apsd',
    'wlan_mgt.fixed.capabilities.radio_measurement',
    'wlan_mgt.fixed.capabilities.dsss_ofdm',
    'wlan_mgt.fixed.capabilities.del_blk_ack',
    'wlan_mgt.fixed.capabilities.imm_blk_ack', 'wlan_mgt.fixed.listen_ival',
    'wlan_mgt.fixed.current_ap', 'wlan_mgt.fixed.status_code',
    'wlan_mgt.fixed.timestamp', 'wlan_mgt.fixed.beacon',
    'wlan_mgt.fixed.aid', 'wlan_mgt.fixed.reason_code',
    'wlan_mgt.fixed.auth.alg', 'wlan_mgt.fixed.auth_seq',
    'wlan_mgt.fixed.category_code', 'wlan_mgt.fixed.htact',
    'wlan_mgt.fixed.chanwidth', 'wlan_mgt.fixed.fragment',
    'wlan_mgt.fixed.sequence', 'wlan_mgt.tagged.all', 'wlan_mgt.ssid',
    'wlan_mgt.ds.current_channel', 'wlan_mgt.tim.dtim_count',
    'wlan_mgt.tim.dtim_period', 'wlan_mgt.tim.bmapctl.multicast',
    'wlan_mgt.tim.bmapctl.offset', 'wlan_mgt.country_info.environment',
    'wlan_mgt.rsn.version', 'wlan_mgt.rsn.gcs.type',
    'wlan_mgt.rsn.pcs.count', 'wlan_mgt.rsn.akms.count',
    'wlan_mgt.rsn.akms.type', 'wlan_mgt.rsn.capabilities.preauth',
    'wlan_mgt.rsn.capabilities.no_pairwise',
    'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
    'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
    'wlan_mgt.rsn.capabilities.mfpr', 'wlan_mgt.rsn.capabilities.mfpc',
    'wlan_mgt.rsn.capabilities.peerkey', 'wlan_mgt.tcprep.trsmt_pow',
    'wlan_mgt.tcprep.link_mrg', 'wlan.wep.iv', 'wlan.wep.key',
    'wlan.wep.icv', 'wlan.tkip.extiv', 'wlan.ccmp.extiv', 'wlan.qos.tid',
    'wlan.qos.priority', 'wlan.qos.eosp', 'wlan.qos.ack',
    'wlan.qos.amsdupresent', 'wlan.qos.buf_state_indicated',
    'wlan.qos.bit4', 'wlan.qos.txop_dur_req',
    'wlan.qos.buf_state_indicated', 'data.len', 'class']
awid_test.drop(null_data_columns, axis=1, inplace=True)
awid_test.replace({'?': None}, inplace=True)
awid_test.dropna(inplace=True)
for col in awid_test.columns:
    awid_test[col] = pd.to_numeric(awid_test[col], errors='ignore')
awid_test.shape
from sklearn.metrics import accuracy_score
X_test = awid_test.select_dtypes(['number'])
y_test = awid_test['class']


def get_test_accuracy_of(model):
    y_preds = model.predict(X_test)
    return accuracy_score(y_preds, y_test)


get_test_accuracy_of(nb)
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(X, y)
get_test_accuracy_of(lr)
from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier()
tree.fit(X, y)
get_test_accuracy_of(tree)
pd.DataFrame({'feature': awid.select_dtypes(['number']).columns,
    'importance': tree.feature_importances_}).sort_values('importance',
    ascending=False).head(10)
from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier()
forest.fit(X, y)
get_test_accuracy_of(forest)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
preprocessing = Pipeline([('scale', StandardScaler())])
pipeline = Pipeline([('preprocessing', preprocessing), ('classifier',
    DecisionTreeClassifier())])
params = {'classifier__max_depth': [None, 3, 5, 10]}
grid = GridSearchCV(pipeline, params)
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)
preprocessing = Pipeline([('scale', StandardScaler())])
pipeline = Pipeline([('preprocessing', preprocessing), ('classifier',
    RandomForestClassifier())])
params = {'classifier__max_depth': [None, 3, 5, 10]}
grid = GridSearchCV(pipeline, params)
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)

# -*- coding: utf-8 -*-
"""CapstoneReview2.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1XS4vzrhfn7DdVnhXtThmQP-cgbZ0BC-3
"""

import pandas as pd

from google.colab import drive
drive.mount('/content/drive')

awid = pd.read_csv('/content/drive/My Drive/AWID/AWID-CLS-R-Trn.csv', header=0)
awid.columns= ['frame.interface_id',
 'frame.dlt',
 'frame.offset_shift',
 'frame.time_epoch',
 'frame.time_delta',
 'frame.time_delta_displayed',
 'frame.time_relative',
 'frame.len',
 'frame.cap_len',
 'frame.marked',
 'frame.ignored',
 'radiotap.version',
 'radiotap.pad',
 'radiotap.length',
 'radiotap.present.tsft',
 'radiotap.present.flags',
 'radiotap.present.rate',
 'radiotap.present.channel',
 'radiotap.present.fhss',
 'radiotap.present.dbm_antsignal',
 'radiotap.present.dbm_antnoise',
 'radiotap.present.lock_quality',
 'radiotap.present.tx_attenuation',
 'radiotap.present.db_tx_attenuation',
 'radiotap.present.dbm_tx_power',
 'radiotap.present.antenna',
 'radiotap.present.db_antsignal',
 'radiotap.present.db_antnoise',
 'radiotap.present.rxflags',
 'radiotap.present.xchannel',
 'radiotap.present.mcs',
 'radiotap.present.ampdu',
 'radiotap.present.vht',
 'radiotap.present.reserved',
 'radiotap.present.rtap_ns',
 'radiotap.present.vendor_ns',
 'radiotap.present.ext',
 'radiotap.mactime',
 'radiotap.flags.cfp',
 'radiotap.flags.preamble',
 'radiotap.flags.wep',
 'radiotap.flags.frag',
 'radiotap.flags.fcs',
 'radiotap.flags.datapad',
 'radiotap.flags.badfcs',
 'radiotap.flags.shortgi',
 'radiotap.datarate',
 'radiotap.channel.freq',
 'radiotap.channel.type.turbo',
 'radiotap.channel.type.cck',
 'radiotap.channel.type.ofdm',
 'radiotap.channel.type.2ghz',
 'radiotap.channel.type.5ghz',
 'radiotap.channel.type.passive',
 'radiotap.channel.type.dynamic',
 'radiotap.channel.type.gfsk',
 'radiotap.channel.type.gsm',
 'radiotap.channel.type.sturbo',
 'radiotap.channel.type.half',
 'radiotap.channel.type.quarter',
 'radiotap.dbm_antsignal',
 'radiotap.antenna',
 'radiotap.rxflags.badplcp',
 'wlan.fc.type_subtype',
 'wlan.fc.version',
 'wlan.fc.type',
 'wlan.fc.subtype',
 'wlan.fc.ds',
 'wlan.fc.frag',
 'wlan.fc.retry',
 'wlan.fc.pwrmgt',
 'wlan.fc.moredata',
 'wlan.fc.protected',
 'wlan.fc.order',
 'wlan.duration',
 'wlan.ra',
 'wlan.da',
 'wlan.ta',
 'wlan.sa',
 'wlan.bssid',
 'wlan.frag',
 'wlan.seq',
 'wlan.bar.type',
 'wlan.ba.control.ackpolicy',
 'wlan.ba.control.multitid',
 'wlan.ba.control.cbitmap',
 'wlan.bar.compressed.tidinfo',
 'wlan.ba.bm',
 'wlan.fcs_good',
 'wlan_mgt.fixed.capabilities.ess',
 'wlan_mgt.fixed.capabilities.ibss',
 'wlan_mgt.fixed.capabilities.cfpoll.ap',
 'wlan_mgt.fixed.capabilities.privacy',
 'wlan_mgt.fixed.capabilities.preamble',
 'wlan_mgt.fixed.capabilities.pbcc',
 'wlan_mgt.fixed.capabilities.agility',
 'wlan_mgt.fixed.capabilities.spec_man',
 'wlan_mgt.fixed.capabilities.short_slot_time',
 'wlan_mgt.fixed.capabilities.apsd',
 'wlan_mgt.fixed.capabilities.radio_measurement',
 'wlan_mgt.fixed.capabilities.dsss_ofdm',
 'wlan_mgt.fixed.capabilities.del_blk_ack',
 'wlan_mgt.fixed.capabilities.imm_blk_ack',
 'wlan_mgt.fixed.listen_ival',
 'wlan_mgt.fixed.current_ap',
 'wlan_mgt.fixed.status_code',
 'wlan_mgt.fixed.timestamp',
 'wlan_mgt.fixed.beacon',
 'wlan_mgt.fixed.aid',
 'wlan_mgt.fixed.reason_code',
 'wlan_mgt.fixed.auth.alg',
 'wlan_mgt.fixed.auth_seq',
 'wlan_mgt.fixed.category_code',
 'wlan_mgt.fixed.htact',
 'wlan_mgt.fixed.chanwidth',
 'wlan_mgt.fixed.fragment',
 'wlan_mgt.fixed.sequence',
 'wlan_mgt.tagged.all',
 'wlan_mgt.ssid',
 'wlan_mgt.ds.current_channel',
 'wlan_mgt.tim.dtim_count',
 'wlan_mgt.tim.dtim_period',
 'wlan_mgt.tim.bmapctl.multicast',
 'wlan_mgt.tim.bmapctl.offset',
 'wlan_mgt.country_info.environment',
 'wlan_mgt.rsn.version',
 'wlan_mgt.rsn.gcs.type',
 'wlan_mgt.rsn.pcs.count',
 'wlan_mgt.rsn.akms.count',
 'wlan_mgt.rsn.akms.type',
 'wlan_mgt.rsn.capabilities.preauth',
 'wlan_mgt.rsn.capabilities.no_pairwise',
 'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
 'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
 'wlan_mgt.rsn.capabilities.mfpr',
 'wlan_mgt.rsn.capabilities.mfpc',
 'wlan_mgt.rsn.capabilities.peerkey',
 'wlan_mgt.tcprep.trsmt_pow',
 'wlan_mgt.tcprep.link_mrg',
 'wlan.wep.iv',
 'wlan.wep.key',
 'wlan.wep.icv',
 'wlan.tkip.extiv',
 'wlan.ccmp.extiv',
 'wlan.qos.tid',
 'wlan.qos.priority',
 'wlan.qos.eosp',
 'wlan.qos.ack',
 'wlan.qos.amsdupresent',
 'wlan.qos.buf_state_indicated',
 'wlan.qos.bit4',
 'wlan.qos.txop_dur_req',
 'wlan.qos.buf_state_indicated',
 'data.len',
 'class']

awid.shape

awid.head()

awid['class'].value_counts(normalize=True)

awid.isna().sum()

awid.replace({"?": None}, inplace=True)

awid.isna().sum()

null_data_columns = awid.columns[awid.isnull().mean() >= 0.5]

null_data_columns.shape

awid.drop(null_data_columns, axis=1, inplace=True)

awid.shape

awid.dropna(inplace=True)
awid.shape

awid['class'].value_counts(normalize=True)

awid.select_dtypes(['number']).shape

for col in awid.columns:
    awid[col] = pd.to_numeric(awid[col], errors='ignore')

awid.select_dtypes(['number']).shape

awid.describe()

X, y = awid.select_dtypes(['number']), awid['class']

from sklearn.naive_bayes import GaussianNB

nb = GaussianNB()

# fit our model to the data
nb.fit(X, y)



#TESTING


awid_test = pd.read_csv("/content/drive/My Drive/AWID/AWID-CLS-R-Tst.csv", header=None)
awid_test.columns= ['frame.interface_id',
 'frame.dlt',
 'frame.offset_shift',
 'frame.time_epoch',
 'frame.time_delta',
 'frame.time_delta_displayed',
 'frame.time_relative',
 'frame.len',
 'frame.cap_len',
 'frame.marked',
 'frame.ignored',
 'radiotap.version',
 'radiotap.pad',
 'radiotap.length',
 'radiotap.present.tsft',
 'radiotap.present.flags',
 'radiotap.present.rate',
 'radiotap.present.channel',
 'radiotap.present.fhss',
 'radiotap.present.dbm_antsignal',
 'radiotap.present.dbm_antnoise',
 'radiotap.present.lock_quality',
 'radiotap.present.tx_attenuation',
 'radiotap.present.db_tx_attenuation',
 'radiotap.present.dbm_tx_power',
 'radiotap.present.antenna',
 'radiotap.present.db_antsignal',
 'radiotap.present.db_antnoise',
 'radiotap.present.rxflags',
 'radiotap.present.xchannel',
 'radiotap.present.mcs',
 'radiotap.present.ampdu',
 'radiotap.present.vht',
 'radiotap.present.reserved',
 'radiotap.present.rtap_ns',
 'radiotap.present.vendor_ns',
 'radiotap.present.ext',
 'radiotap.mactime',
 'radiotap.flags.cfp',
 'radiotap.flags.preamble',
 'radiotap.flags.wep',
 'radiotap.flags.frag',
 'radiotap.flags.fcs',
 'radiotap.flags.datapad',
 'radiotap.flags.badfcs',
 'radiotap.flags.shortgi',
 'radiotap.datarate',
 'radiotap.channel.freq',
 'radiotap.channel.type.turbo',
 'radiotap.channel.type.cck',
 'radiotap.channel.type.ofdm',
 'radiotap.channel.type.2ghz',
 'radiotap.channel.type.5ghz',
 'radiotap.channel.type.passive',
 'radiotap.channel.type.dynamic',
 'radiotap.channel.type.gfsk',
 'radiotap.channel.type.gsm',
 'radiotap.channel.type.sturbo',
 'radiotap.channel.type.half',
 'radiotap.channel.type.quarter',
 'radiotap.dbm_antsignal',
 'radiotap.antenna',
 'radiotap.rxflags.badplcp',
 'wlan.fc.type_subtype',
 'wlan.fc.version',
 'wlan.fc.type',
 'wlan.fc.subtype',
 'wlan.fc.ds',
 'wlan.fc.frag',
 'wlan.fc.retry',
 'wlan.fc.pwrmgt',
 'wlan.fc.moredata',
 'wlan.fc.protected',
 'wlan.fc.order',
 'wlan.duration',
 'wlan.ra',
 'wlan.da',
 'wlan.ta',
 'wlan.sa',
 'wlan.bssid',
 'wlan.frag',
 'wlan.seq',
 'wlan.bar.type',
 'wlan.ba.control.ackpolicy',
 'wlan.ba.control.multitid',
 'wlan.ba.control.cbitmap',
 'wlan.bar.compressed.tidinfo',
 'wlan.ba.bm',
 'wlan.fcs_good',
 'wlan_mgt.fixed.capabilities.ess',
 'wlan_mgt.fixed.capabilities.ibss',
 'wlan_mgt.fixed.capabilities.cfpoll.ap',
 'wlan_mgt.fixed.capabilities.privacy',
 'wlan_mgt.fixed.capabilities.preamble',
 'wlan_mgt.fixed.capabilities.pbcc',
 'wlan_mgt.fixed.capabilities.agility',
 'wlan_mgt.fixed.capabilities.spec_man',
 'wlan_mgt.fixed.capabilities.short_slot_time',
 'wlan_mgt.fixed.capabilities.apsd',
 'wlan_mgt.fixed.capabilities.radio_measurement',
 'wlan_mgt.fixed.capabilities.dsss_ofdm',
 'wlan_mgt.fixed.capabilities.del_blk_ack',
 'wlan_mgt.fixed.capabilities.imm_blk_ack',
 'wlan_mgt.fixed.listen_ival',
 'wlan_mgt.fixed.current_ap',
 'wlan_mgt.fixed.status_code',
 'wlan_mgt.fixed.timestamp',
 'wlan_mgt.fixed.beacon',
 'wlan_mgt.fixed.aid',
 'wlan_mgt.fixed.reason_code',
 'wlan_mgt.fixed.auth.alg',
 'wlan_mgt.fixed.auth_seq',
 'wlan_mgt.fixed.category_code',
 'wlan_mgt.fixed.htact',
 'wlan_mgt.fixed.chanwidth',
 'wlan_mgt.fixed.fragment',
 'wlan_mgt.fixed.sequence',
 'wlan_mgt.tagged.all',
 'wlan_mgt.ssid',
 'wlan_mgt.ds.current_channel',
 'wlan_mgt.tim.dtim_count',
 'wlan_mgt.tim.dtim_period',
 'wlan_mgt.tim.bmapctl.multicast',
 'wlan_mgt.tim.bmapctl.offset',
 'wlan_mgt.country_info.environment',
 'wlan_mgt.rsn.version',
 'wlan_mgt.rsn.gcs.type',
 'wlan_mgt.rsn.pcs.count',
 'wlan_mgt.rsn.akms.count',
 'wlan_mgt.rsn.akms.type',
 'wlan_mgt.rsn.capabilities.preauth',
 'wlan_mgt.rsn.capabilities.no_pairwise',
 'wlan_mgt.rsn.capabilities.ptksa_replay_counter',
 'wlan_mgt.rsn.capabilities.gtksa_replay_counter',
 'wlan_mgt.rsn.capabilities.mfpr',
 'wlan_mgt.rsn.capabilities.mfpc',
 'wlan_mgt.rsn.capabilities.peerkey',
 'wlan_mgt.tcprep.trsmt_pow',
 'wlan_mgt.tcprep.link_mrg',
 'wlan.wep.iv',
 'wlan.wep.key',
 'wlan.wep.icv',
 'wlan.tkip.extiv',
 'wlan.ccmp.extiv',
 'wlan.qos.tid',
 'wlan.qos.priority',
 'wlan.qos.eosp',
 'wlan.qos.ack',
 'wlan.qos.amsdupresent',
 'wlan.qos.buf_state_indicated',
 'wlan.qos.bit4',
 'wlan.qos.txop_dur_req',
 'wlan.qos.buf_state_indicated',
 'data.len',
 'class']
awid_test.drop(null_data_columns, axis=1, inplace=True)
awid_test.replace({"?": None}, inplace=True)
awid_test.dropna(inplace=True)  

for col in awid_test.columns:
    awid_test[col] = pd.to_numeric(awid_test[col], errors='ignore')
awid_test.shape

from sklearn.metrics import accuracy_score
X_test = awid_test.select_dtypes(['number'])
y_test = awid_test['class']

def get_test_accuracy_of(model):
    y_preds = model.predict(X_test)
    return accuracy_score(y_preds, y_test)
    
get_test_accuracy_of(nb)

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()

lr.fit(X, y)

get_test_accuracy_of(lr)

from sklearn.tree import DecisionTreeClassifier

tree = DecisionTreeClassifier()

tree.fit(X, y)

# Tree does very well!
get_test_accuracy_of(tree)

pd.DataFrame({'feature':awid.select_dtypes(['number']).columns, 
              'importance':tree.feature_importances_}).sort_values('importance', ascending=False).head(10)

from sklearn.ensemble import RandomForestClassifier

forest = RandomForestClassifier()

forest.fit(X, y)

get_test_accuracy_of(forest)

#Pipeline to Decision Tree
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV

preprocessing = Pipeline([
    ("scale", StandardScaler()),
])

pipeline = Pipeline([
    ("preprocessing", preprocessing),
    ("classifier", DecisionTreeClassifier())
])

params = {
    "classifier__max_depth": [None, 3, 5, 10], 
         }

grid = GridSearchCV(pipeline, params)
grid.fit(X, y)

get_test_accuracy_of(grid.best_estimator_)

#Pipeline to Random Forest
preprocessing = Pipeline([
    ("scale", StandardScaler()),
])

pipeline = Pipeline([
    ("preprocessing", preprocessing),
    ("classifier", RandomForestClassifier())
])

params = {
    "classifier__max_depth": [None, 3, 5, 10], 
         }

grid = GridSearchCV(pipeline, params)
grid.fit(X, y)
get_test_accuracy_of(grid.best_estimator_)





