from typing import List import datasets import pandas VERSION = datasets.Version("1.0.0") DESCRIPTION = "Pima dataset from the OpenML repository." _HOMEPAGE = "https://www.openml.org/search?type=data&status=active&id=37" _URLS = ("https://www.openml.org/search?type=data&status=active&id=37") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/pima/raw/main/pima.csv" } features_types_per_config = { "pima": { "number_of_pregnancies": datasets.Value("int8"), "plasma_glucose_concentration": datasets.Value("float64"), "diastolic_blood_pressure": datasets.Value("float64"), "triceps_thickness": datasets.Value("float64"), "serum_insulin": datasets.Value("float64"), "bmi": datasets.Value("float64"), "diabetes_pedigree": datasets.Value("float64"), "age": datasets.Value("float64"), "has_diabetes": 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 PimaConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(PimaConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Pima(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "pima" BUILDER_CONFIGS = [ PimaConfig(name="pima", description="Pima 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 = data.rename(columns={"class": "has_diabetes"}) for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row