CIC-IDS2017 / CIC-IDS2017.py
rdpahalavan's picture
Create CIC-IDS2017.py
0a4a434
raw
history blame
5.02 kB
import os
import datasets
import pandas as pd
_CITATION = """\
@article{maharajan2020attack,
title={Attack classification and intrusion detection in IoT network using machine learning techniques},
author={Maharajan, R and Raja, KS},
journal={Computers \& Electrical Engineering},
volume={87},
pages={106783},
year={2020},
publisher={Elsevier}
}"""
_DESCRIPTION = """\
The CIC-IDS2017 dataset is an intrusion detection dataset that consists of network traffic data. \
It contains different network attacks and normal traffic. This dataset can be used for evaluating \
intrusion detection systems in IoT networks.
"""
_HOMEPAGE = "https://www.unb.ca/cic/datasets/ids-2017.html"
_LICENSE = "Unknown"
_FOLDERS = {
"folder_1": "rdpahalavan/CIC-IDS2017/Network-Flows",
"folder_2": "rdpahalavan/CIC-IDS2017/Payload-Bytes",
"folder_3": "rdpahalavan/CIC-IDS2017/Packet-Bytes",
"folder_4": "rdpahalavan/CIC-IDS2017/Packet-Fields",
}
class CICIDS2017(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="folder_1", version=VERSION, description="Folder 1 of CIC-IDS2017 dataset"),
datasets.BuilderConfig(name="folder_2", version=VERSION, description="Folder 2 of CIC-IDS2017 dataset"),
datasets.BuilderConfig(name="folder_3", version=VERSION, description="Folder 3 of CIC-IDS2017 dataset"),
datasets.BuilderConfig(name="folder_4", version=VERSION, description="Folder 4 of CIC-IDS2017 dataset"),
]
DEFAULT_CONFIG_NAME = "folder_1"
def _info(self):
if self.config.name == "folder_1":
features = datasets.Features(
{
"source_ip": datasets.Value("string"),
"destination_ip": datasets.Value("string"),
"timestamp": datasets.Value("string"),
"protocol": datasets.Value("string"),
"flow_duration": datasets.Value("float"),
# Add more features specific to folder_1 configuration
}
)
elif self.config.name == "folder_2":
features = datasets.Features(
{
"source_ip": datasets.Value("string"),
"destination_ip": datasets.Value("string"),
"timestamp": datasets.Value("string"),
"protocol": datasets.Value("string"),
"flow_duration": datasets.Value("float"),
# Add more features specific to folder_2 configuration
}
)
elif self.config.name == "folder_3":
features = datasets.Features(
{
"source_ip": datasets.Value("string"),
"destination_ip": datasets.Value("string"),
"timestamp": datasets.Value("string"),
"protocol": datasets.Value("string"),
"flow_duration": datasets.Value("float"),
# Add more features specific to folder_3 configuration
}
)
else: # folder_4
features = datasets.Features(
{
"source_ip": datasets.Value("string"),
"destination_ip": datasets.Value("string"),
"timestamp": datasets.Value("string"),
"protocol": datasets.Value("string"),
"flow_duration": datasets.Value("float"),
# Add more features specific to folder_4 configuration
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
folder_path = _FOLDERS[self.config.name]
data_dir = dl_manager.download(folder_path)
csv_files = [
filename for filename in os.listdir(data_dir) if filename.endswith(".csv")
]
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"data_dir": data_dir, "csv_files": csv_files},
)
]
def _generate_examples(self, data_dir, csv_files):
for csv_file in csv_files:
file_path = os.path.join(data_dir, csv_file)
df = pd.read_csv(file_path)
for idx, row in df.iterrows():
example = {
"source_ip": row["source_ip"],
"destination_ip": row["destination_ip"],
"timestamp": row["timestamp"],
"protocol": row["protocol"],
"flow_duration": row["flow_duration"],
# Add more feature values according to the dataset columns
}
yield idx, example
datasets.load_dataset("rdpahalavan/CIC-IDS2017")