"""Yeast Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ENCODING_DICS = { "class": { "MIT": 0, "NUC": 1, "CYT": 2, "ME1": 3, "EXC": 4, "ME2": 5, "ME3": 6, "VAC": 7, "POX": 8, "ERL": 9 } } DESCRIPTION = "Yeast dataset." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/110/yeast" _URLS = ("https://archive-beta.ics.uci.edu/dataset/110/yeast") _CITATION = """ @misc{misc_yeast_110, author = {Nakai,Kenta}, title = {{Yeast}}, year = {1996}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C5KG68}} } """ # Dataset info urls_per_split = { "train": "https://huggingface.co./datasets/mstz/yeast/raw/main/yeast.csv" } features_types_per_config = { "yeast": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=10) }, "yeast_0": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_1": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_2": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_3": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_4": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_5": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_6": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_7": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_8": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2) }, "yeast_9": { "mcg": datasets.Value("float64"), "gvh": datasets.Value("float64"), "alm": datasets.Value("float64"), "mit": datasets.Value("float64"), "erl": datasets.Value("bool"), "pox": datasets.Value("float64"), "vac": datasets.Value("float64"), "nuc": 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 YeastConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(YeastConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Yeast(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "yeast" BUILDER_CONFIGS = [ YeastConfig(name="yeast", description="Yeast for multiclass classification."), YeastConfig(name="yeast_0", description="Yeast for binary classification."), YeastConfig(name="yeast_1", description="Yeast for binary classification."), YeastConfig(name="yeast_2", description="Yeast for binary classification."), YeastConfig(name="yeast_3", description="Yeast for binary classification."), YeastConfig(name="yeast_4", description="Yeast for binary classification."), YeastConfig(name="yeast_5", description="Yeast for binary classification."), YeastConfig(name="yeast_6", description="Yeast for binary classification."), YeastConfig(name="yeast_7", description="Yeast for binary classification."), YeastConfig(name="yeast_8", description="Yeast for binary classification."), YeastConfig(name="yeast_9", description="Yeast 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: for feature in _ENCODING_DICS: encoding_function = partial(self.encode, feature) data.loc[:, feature] = data[feature].apply(encoding_function) data["erl"] = data["erl"].apply(lambda x: True if x == 1 else False) data = data.astype({"erl": "bool"}) if self.config.name == "yeast_0": data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0) elif self.config.name == "yeast_1": data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0) elif self.config.name == "yeast_2": data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0) elif self.config.name == "yeast_3": data["class"] = data["class"].apply(lambda x: 1 if x == 3 else 0) elif self.config.name == "yeast_4": data["class"] = data["class"].apply(lambda x: 1 if x == 4 else 0) elif self.config.name == "yeast_5": data["class"] = data["class"].apply(lambda x: 1 if x == 5 else 0) elif self.config.name == "yeast_6": data["class"] = data["class"].apply(lambda x: 1 if x == 6 else 0) elif self.config.name == "yeast_7": data["class"] = data["class"].apply(lambda x: 1 if x == 7 else 0) elif self.config.name == "yeast_8": data["class"] = data["class"].apply(lambda x: 1 if x == 8 else 0) elif self.config.name == "yeast_9": data["class"] = data["class"].apply(lambda x: 1 if x == 9 else 0) 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}")