from typing import List import datasets import pandas VERSION = datasets.Version("1.0.0") DESCRIPTION = "Hayes efficiency dataset from the UCI repository." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/242/hayes+efficiency" _URLS = ("https://archive-beta.ics.uci.edu/dataset/30/hayes+method+choice") _CITATION = """ @misc{misc_hayes_efficiency_242, author = {Tsanas,Athanasios & Xifara,Angeliki}, title = {{Hayes efficiency}}, year = {2012}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C51307}} }""" # Dataset info _BASE_FEATURE_NAMES = [ "name", "hobby", "age", "educational_level", "marital_level", "class" ] urls_per_split = { "train": "https://huggingface.co/datasets/mstz/hayes_roth/raw/main/hayes.data" } features_types_per_config = { "hayes": { "hobby": datasets.Value("string"), "age": datasets.Value("int8"), "educational_level": datasets.Value("int8"), "marital_level": datasets.Value("string"), "class": datasets.ClassLabel(num_classes=3) }, "hayes_1": { "hobby": datasets.Value("string"), "age": datasets.Value("int8"), "educational_level": datasets.Value("int8"), "marital_level": datasets.Value("string"), "class": datasets.ClassLabel(num_classes=2) }, "hayes_2": { "hobby": datasets.Value("string"), "age": datasets.Value("int8"), "educational_level": datasets.Value("int8"), "marital_level": datasets.Value("string"), "class": datasets.ClassLabel(num_classes=2) }, "hayes_3": { "hobby": datasets.Value("string"), "age": datasets.Value("int8"), "educational_level": datasets.Value("int8"), "marital_level": datasets.Value("string"), "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 HayesConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(HayesConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Hayes(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "hayes" BUILDER_CONFIGS = [ HayesConfig(name="hayes", description="Hayes dataset."), HayesConfig(name="hayes_1", description="Hayes for binary classification (is example of class 1?)."), HayesConfig(name="hayes_2", description="Hayes for binary classification (is example of class 2?)."), HayesConfig(name="hayes_3", description="Hayes for binary classification (is example of class 3?).") ] 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 = 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.columns = _BASE_FEATURE_NAMES data.drop("name", axis="columns", inplace=True) if self.config.name == "hayes_1": data.loc[:, "class"] = data["class"].apply(lambda x: 1 if x == 1 else 0) elif self.config.name == "hayes_2": data.loc[:, "class"] = data["class"].apply(lambda x: 1 if x == 2 else 0) elif self.config.name == "hayes_3": data.loc[:, "class"] = data["class"].apply(lambda x: 1 if x == 3 else 0) else: data.loc[:, "class"] = data["class"].apply(lambda x: x - 1) return data