from typing import List import datasets import pandas VERSION = datasets.Version("1.0.0") _BASE_FEATURE_NAMES = [ "family", "product_type", "steel_type", "carbon", "hardness", "temper_rolling", "condition", "formability", "strength", "non_ageing", "surface_finish", "surface_quality", "enamelability", "bc", "bf", "bt", "bw_time", "bl", "m", "chrom", "phos", "cbond", "marvi", "exptl", "ferro", "corr", "blue", "lustre", "jurofm", "s", "p", "is_coil", "thick", "width", "len", "oil", "bore", "packing", "class" ] DESCRIPTION = "Anneal dataset from the UCI ML repository." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/3/annealing" _URLS = ("https://archive-beta.ics.uci.edu/dataset/3/annealing") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/anneal/raw/main/anneal.data", "test": "https://huggingface.co/datasets/mstz/anneal/raw/main/anneal.test" } features_types_per_config = { "annealing": { "family": datasets.Value("string"), "product_type": datasets.Value("string"), "steel_type": datasets.Value("string"), "carbon": datasets.Value("float64"), "hardness": datasets.Value("float64"), "temper_rolling": datasets.Value("bool"), "condition": datasets.Value("string"), "formability": datasets.Value("int8"), "strength": datasets.Value("float64"), "non_ageing": datasets.Value("bool"), "surface_finish": datasets.Value("string"), "surface_quality": datasets.Value("string"), "enamelability": datasets.Value("int8"), "bc": datasets.Value("bool"), "bf": datasets.Value("bool"), "bt": datasets.Value("bool"), "bw_time": datasets.Value("string"), "bl": datasets.Value("bool"), "m": datasets.Value("bool"), "chrom": datasets.Value("bool"), "phos": datasets.Value("bool"), "cbond": datasets.Value("bool"), "marvi": datasets.Value("bool"), "exptl": datasets.Value("bool"), "ferro": datasets.Value("bool"), "corr": datasets.Value("bool"), "blue": datasets.Value("string"), "lustre": datasets.Value("bool"), "jurofm": datasets.Value("bool"), "s": datasets.Value("bool"), "p": datasets.Value("bool"), "is_coil": datasets.Value("bool"), "thick": datasets.Value("float64"), "width": datasets.Value("float64"), "len": datasets.Value("float64"), "oil": datasets.Value("string"), "bore": datasets.Value("string"), "packing": datasets.Value("string"), "class": datasets.ClassLabel(num_classes=6, names=["1", "2", "3", "4", "5", "U"]) } } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class AnnealConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(AnnealConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Anneal(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "annealing" BUILDER_CONFIGS = [ AnnealConfig(name="annealing", description="Anneal for multiclass classification.") ] def _info(self): if self.config.name not in features_per_config: raise ValueError(f"Unknown configuration: {self.config.name}") 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, config=self.config.name) for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame: data.columns = _BASE_FEATURE_NAMES data.loc[:, "formability"] = data.formability.apply(lambda x: int(x) if x != "-" else 0) data.loc[:, "enamelability"] = data.enamelability.apply(lambda x: int(x) if x != "-" else 0) data.loc[:, "non_ageing"] = data.formability.apply(lambda x: True if x == "-" else False) data.loc[:, "bc"] = data.bc.apply(lambda x: True if x == "Y" else False) data.loc[:, "bf"] = data.bf.apply(lambda x: True if x == "Y" else False) data.loc[:, "bt"] = data.bt.apply(lambda x: True if x == "Y" else False) data.loc[:, "bl"] = data.bl.apply(lambda x: True if x == "Y" else False) data.loc[:, "m"] = data.m.apply(lambda x: True if x == "Y" else False) data.loc[:, "chrom"] = data.chrom.apply(lambda x: True if x == "C" else False) data.loc[:, "phos"] = data.phos.apply(lambda x: True if x == "P" else False) data.loc[:, "cbond"] = data.cbond.apply(lambda x: True if x == "Y" else False) data.loc[:, "marvi"] = data.marvi.apply(lambda x: True if x == "Y" else False) data.loc[:, "exptl"] = data.exptl.apply(lambda x: True if x == "Y" else False) data.loc[:, "ferro"] = data.ferro.apply(lambda x: True if x == "Y" else False) data.loc[:, "corr"] = data.corr.apply(lambda x: True if x == "Y" else False) data.loc[:, "lustre"] = data.lustre.apply(lambda x: True if x == "Y" else False) data.loc[:, "jurofm"] = data.jurofm.apply(lambda x: True if x == "Y" else False) data.loc[:, "s"] = data.s.apply(lambda x: True if x == "Y" else False) data.loc[:, "p"] = data.p.apply(lambda x: True if x == "Y" else False) data.loc[:, "class"] = data.p.apply(lambda x: int(x) if x != "U" else 0) return data