blood / blood.py
mstz's picture
Update blood.py
778b279
"""Blood"""
from typing import List
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
"months_since_last_donation",
"total_donation",
"total_blood_donated_in_cc",
"months_since_last_donation",
"has_donated_last_month"
]
DESCRIPTION = "Blood dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Blood"
_URLS = ("https://huggingface.co/datasets/mstz/blood/raw/blood.csv")
_CITATION = """
@misc{misc_blood_transfusion_service_center_176,
author = {Yeh,I-Cheng},
title = {{Blood Transfusion Service Center}},
year = {2008},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C5GS39}}
}"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/blood/raw/main/transfusion.data"
}
features_types_per_config = {
"blood": {
"months_since_last_donation": datasets.Value("int64"),
"total_donation": datasets.Value("int64"),
"total_blood_donated_in_cc": datasets.Value("int64"),
"months_since_last_donation": datasets.Value("int64"),
"has_donated_last_month": 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 BloodConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(BloodConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Blood(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "blood"
BUILDER_CONFIGS = [
BloodConfig(name="blood", description="Blood 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.columns = _BASE_FEATURE_NAMES
data = data.astype({
"months_since_last_donation": "int64",
"total_donation": "int64",
"total_blood_donated_in_cc": "int64",
"months_since_last_donation": "int64",
"has_donated_last_month": "int64"
})
for row_id, row in data.iterrows():
data_row = dict(row)
if isinstance(data_row["months_since_last_donation"], pandas.Series):
data_row["months_since_last_donation"] = data_row["months_since_last_donation"].values[0]
yield row_id, data_row