"""Shuttle Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ENCODING_DICS = {} DESCRIPTION = "Shuttle dataset." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/148/statlog+shuttle" _URLS = ("https://archive-beta.ics.uci.edu/dataset/148/statlog+shuttle") _CITATION = """ @misc{misc_statlog_(shuttle)_148, title = {{Statlog (Shuttle)}}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \url{10.24432/C5WS31}} } """ # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/shuttle/raw/main/shuttle.csv" } features_types_per_config = { "shuttle": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=7), }, "shuttle_0": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2), }, "shuttle_1": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2), }, "shuttle_2": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2), }, "shuttle_3": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2), }, "shuttle_4": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2), }, "shuttle_5": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2), }, "shuttle_6": { "time": datasets.Value("float64"), "rad_flow": datasets.Value("float64"), "fpv_close": datasets.Value("float64"), "fpv_open": datasets.Value("float64"), "high": datasets.Value("float64"), "bypass": datasets.Value("float64"), "bvp_close": datasets.Value("float64"), "bvp_open": datasets.Value("float64"), "feature": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2), }, } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class ShuttleConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(ShuttleConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Shuttle(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "shuttle" BUILDER_CONFIGS = [ ShuttleConfig(name="shuttle", description="Shuttle for multiclass classification."), ShuttleConfig(name="shuttle_0", description="Shuttle for binary classification."), ShuttleConfig(name="shuttle_1", description="Shuttle for binary classification."), ShuttleConfig(name="shuttle_2", description="Shuttle for binary classification."), ShuttleConfig(name="shuttle_3", description="Shuttle for binary classification."), ShuttleConfig(name="shuttle_4", description="Shuttle for binary classification."), ShuttleConfig(name="shuttle_5", description="Shuttle for binary classification."), ShuttleConfig(name="shuttle_6", description="Shuttle for binary 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) 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: data["class"] = data["class"].apply(lambda x: x - 1) if self.config.name == "shuttle_0": data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0) elif self.config.name == "shuttle_1": data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0) elif self.config.name == "shuttle_2": data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0) elif self.config.name == "shuttle_3": data["class"] = data["class"].apply(lambda x: 1 if x == 3 else 0) elif self.config.name == "shuttle_4": data["class"] = data["class"].apply(lambda x: 1 if x == 4 else 0) elif self.config.name == "shuttle_5": data["class"] = data["class"].apply(lambda x: 1 if x == 5 else 0) elif self.config.name == "shuttle_6": data["class"] = data["class"].apply(lambda x: 1 if x == 6 else 0) 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}")