"""Sydt Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ENCODING_DICS = {} _BASE_FEATURE_NAMES = [ "salary", "commission", "age", "education", "car", "zip", "housevalue", "yearsowned", "loan", "class", ] DESCRIPTION = "Sydt dataset." _HOMEPAGE = "" _URLS = ("") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/sydt/resolve/main/sydt.csv" } features_types_per_config = { "sydt": { "salary": datasets.Value("int64"), "commission": datasets.Value("int64"), "age": datasets.Value("int64"), "education": datasets.Value("int64"), "car": datasets.Value("int64"), "zip": datasets.Value("string"), "housevalue": datasets.Value("int64"), "yearsowned": datasets.Value("int64"), "loan": datasets.Value("int64"), "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 SydtConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(SydtConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Sydt(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "sydt" BUILDER_CONFIGS = [SydtConfig(name="sydt", description="Sydt 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, 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 = data[~data["class"].isna()] data["class"] = data["class"].apply(lambda x: x - 1) for feature in _ENCODING_DICS: encoding_function = partial(self.encode, feature) data[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}")