|
"""Kddcup Dataset""" |
|
|
|
from typing import List |
|
from functools import partial |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
_ENCODING_DICS = { |
|
"class": {value: i |
|
for i, value in enumerate(["normal.", |
|
"buffer_overflow.", |
|
"loadmodule.", |
|
"perl.", |
|
"neptune.", |
|
"smurf.", |
|
"guess_passwd.", |
|
"pod.", |
|
"teardrop.", |
|
"portsweep.", |
|
"ipsweep.", |
|
"land.", |
|
"ftp_write.", |
|
"back.", |
|
"imap.", |
|
"satan.", |
|
"phf.", |
|
"nmap.", |
|
"multihop.", |
|
"warezmaster.", |
|
"warezclient.", |
|
"spy.", |
|
"rootkit."]) |
|
} |
|
} |
|
_BASE_FEATURE_NAMES = [ |
|
"duration", |
|
"protocol_type", |
|
"service", |
|
"flag", |
|
"src_bytes", |
|
"dst_bytes", |
|
"land", |
|
"wrong_fragment", |
|
"urgent", |
|
"hot", |
|
"num_failed_logins", |
|
"logged_in", |
|
"num_compromised", |
|
"root_shell", |
|
"su_attempted", |
|
"num_root", |
|
"num_file_creations", |
|
"num_shells", |
|
"num_access_files", |
|
"num_outbound_cmds", |
|
"is_host_login", |
|
"is_guest_login", |
|
"count", |
|
"srv_count", |
|
"serror_rate", |
|
"srv_serror_rate", |
|
"rerror_rate", |
|
"srv_rerror_rate", |
|
"same_srv_rate", |
|
"diff_srv_rate", |
|
"srv_diff_host_rate", |
|
"dst_host_count", |
|
"dst_host_srv_count", |
|
"dst_host_same_srv_rate", |
|
"dst_host_diff_srv_rate", |
|
"dst_host_same_src_port_rate", |
|
"dst_host_srv_diff_host_rate", |
|
"dst_host_serror_rate", |
|
"dst_host_srv_serror_rate", |
|
"dst_host_rerror_rate", |
|
"dst_host_srv_rerror_rate", |
|
"class", |
|
] |
|
|
|
DESCRIPTION = "Kddcup dataset." |
|
_HOMEPAGE = "" |
|
_URLS = ("") |
|
_CITATION = """""" |
|
|
|
|
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/kddcup/resolve/main/kddcup.data" |
|
} |
|
features_types_per_config = { |
|
"kddcup": { |
|
"duration": datasets.Value("float64"), |
|
"protocol_type": datasets.Value("string"), |
|
"service": datasets.Value("string"), |
|
"flag": datasets.Value("string"), |
|
"src_bytes": datasets.Value("int64"), |
|
"dst_bytes": datasets.Value("int64"), |
|
"land": datasets.Value("int64"), |
|
"wrong_fragment": datasets.Value("int64"), |
|
"urgent": datasets.Value("int64"), |
|
"hot": datasets.Value("int64"), |
|
"num_failed_logins": datasets.Value("int64"), |
|
"logged_in": datasets.Value("int64"), |
|
"num_compromised": datasets.Value("int64"), |
|
"root_shell": datasets.Value("int64"), |
|
"su_attempted": datasets.Value("int64"), |
|
"num_root": datasets.Value("int64"), |
|
"num_file_creations": datasets.Value("int64"), |
|
"num_shells": datasets.Value("int64"), |
|
"num_access_files": datasets.Value("int64"), |
|
"num_outbound_cmds": datasets.Value("int64"), |
|
"is_host_login": datasets.Value("int64"), |
|
"is_guest_login": datasets.Value("int64"), |
|
"count": datasets.Value("int64"), |
|
"srv_count": datasets.Value("int64"), |
|
"serror_rate": datasets.Value("float64"), |
|
"srv_serror_rate": datasets.Value("float64"), |
|
"rerror_rate": datasets.Value("float64"), |
|
"srv_rerror_rate": datasets.Value("float64"), |
|
"same_srv_rate": datasets.Value("float64"), |
|
"diff_srv_rate": datasets.Value("float64"), |
|
"srv_diff_host_rate": datasets.Value("float64"), |
|
"dst_host_count": datasets.Value("int64"), |
|
"dst_host_srv_count": datasets.Value("int64"), |
|
"dst_host_same_srv_rate": datasets.Value("float64"), |
|
"dst_host_diff_srv_rate": datasets.Value("float64"), |
|
"dst_host_same_src_port_rate": datasets.Value("float64"), |
|
"dst_host_srv_diff_host_rate": datasets.Value("float64"), |
|
"dst_host_serror_rate": datasets.Value("float64"), |
|
"dst_host_srv_serror_rate": datasets.Value("float64"), |
|
"dst_host_rerror_rate": datasets.Value("float64"), |
|
"dst_host_srv_rerror_rate": datasets.Value("float64"), |
|
"class": datasets.ClassLabel(num_classes=23, |
|
names=("normal.", "buffer_overflow.", "loadmodule.", "perl.", "neptune.", |
|
"smurf.", "guess_passwd.", "pod.", "teardrop.", "portsweep.", |
|
"ipsweep.", "land.", "ftp_write.", "back.", "imap.", "satan.", |
|
"phf.", "nmap.", "multihop.", "warezmaster.", "warezclient.", |
|
"spy.", "rootkit.")), |
|
} |
|
} |
|
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} |
|
|
|
|
|
class KddcupConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(KddcupConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Kddcup(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "kddcup" |
|
BUILDER_CONFIGS = [ |
|
KddcupConfig(name="kddcup", description="Kddcup for multiclass classification.") |
|
] |
|
|
|
|
|
def _info(self): |
|
info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE, |
|
features=features_per_config[self.config.name]) |
|
|
|
return info |
|
|
|
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: |
|
downloads = dl_manager.download_and_extract(urls_per_split) |
|
|
|
return [ |
|
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}), |
|
] |
|
|
|
def _generate_examples(self, filepath: str): |
|
data = pandas.read_csv(filepath, header=None) |
|
data.columns = _BASE_FEATURE_NAMES |
|
data = self.preprocess(data) |
|
|
|
for row_id, row in data.iterrows(): |
|
data_row = dict(row) |
|
|
|
yield row_id, data_row |
|
|
|
def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame: |
|
for feature in _ENCODING_DICS: |
|
encoding_function = partial(self.encode, feature) |
|
data.loc[:, feature] = data[feature].apply(encoding_function) |
|
|
|
return data[list(features_types_per_config[self.config.name].keys())] |
|
|
|
def encode(self, feature, value): |
|
if feature in _ENCODING_DICS: |
|
return _ENCODING_DICS[feature][value] |
|
raise ValueError(f"Unknown feature: {feature}") |
|
|