"""ILPD""" from typing import List import datasets import pandas VERSION = datasets.Version("1.0.0") DESCRIPTION = "ILPD dataset from the UCI ML repository." _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/ILPD" _URLS = ("https://archive.ics.uci.edu/ml/datasets/ILPD") _CITATION = """ @misc{misc_ilpd_(indian_liver_patient_dataset)_225, author = {Ramana,Bendi & Venkateswarlu,N.}, title = {{ILPD (Indian Liver Patient Dataset)}}, year = {2012}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C5D02C}} }""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/liver/raw/main/Indian%20Liver%20Patient%20Dataset%20(ILPD).csv" } features_types_per_config = { "liver": { "age": datasets.Value("int64"), "is_male": datasets.Value("bool"), "total_bilirubin": datasets.Value("float64"), "direct_ribilubin": datasets.Value("float64"), "alkaline_phosphotase": datasets.Value("int64"), "alamine_aminotransferasi": datasets.Value("int64"), "aspartate_aminotransferase": datasets.Value("int64"), "total_proteins": datasets.Value("float64"), "albumin": datasets.Value("float64"), "albumin_to_globulin_ratio": datasets.Value("float64"), "class": datasets.ClassLabel(num_classes=2, names=("no", "yes")) } } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class ILPDConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(ILPDConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class ILPD(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "liver" BUILDER_CONFIGS = [ ILPDConfig(name="liver", description="ILPD 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).infer_objects() data[["is_male"]].applymap(bool) data.loc[:, "class"] = data["class"].apply(lambda x: x - 1) data = data.astype({"is_male": "bool"}) for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row