|
"""Heart Failure Dataset""" |
|
|
|
from typing import List |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
_BASE_FEATURE_NAMES = [ |
|
"age", |
|
"has_anaemia", |
|
"creatinine_phosphokinase_concentration_in_blood", |
|
"has_diabetes", |
|
"heart_ejection_fraction", |
|
"has_high_blood_pressure", |
|
"platelets_concentration_in_blood", |
|
"serum_creatinine_concentration_in_blood", |
|
"serum_sodium_concentration_in_blood", |
|
"sex", |
|
"is_smoker", |
|
"days_in_study", |
|
"is_dead" |
|
] |
|
|
|
DESCRIPTION = "Heart Failure dataset." |
|
_HOMEPAGE = "https://www.kaggle.com/datasets/ulrikthygepedersen/heart_failures" |
|
_URLS = ("https://www.kaggle.com/datasets/ulrikthygepedersen/heart_failures") |
|
_CITATION = """""" |
|
|
|
|
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/heart_failure/raw/main/heart_failure_clinical_records_dataset.csv", |
|
} |
|
features_types_per_config = { |
|
"death": { |
|
"age": datasets.Value("int8"), |
|
"has_anaemia": datasets.Value("bool"), |
|
"creatinine_phosphokinase_concentration_in_blood": datasets.Value("float64"), |
|
"has_diabetes": datasets.Value("bool"), |
|
"heart_ejection_fraction": datasets.Value("float64"), |
|
"has_high_blood_pressure": datasets.Value("bool"), |
|
"platelets_concentration_in_blood": datasets.Value("float64"), |
|
"serum_creatinine_concentration_in_blood": datasets.Value("float64"), |
|
"serum_sodium_concentration_in_blood": datasets.Value("float64"), |
|
"is_male": datasets.Value("bool"), |
|
"is_smoker": datasets.Value("bool"), |
|
"days_in_study": datasets.Value("int64"), |
|
"is_dead": 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 HeartFailureConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(HeartFailureConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class HeartFailure(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "death" |
|
BUILDER_CONFIGS = [ |
|
HeartFailureConfig(name="death", |
|
description="Binary classification, predict if the patient dies.") |
|
] |
|
|
|
|
|
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 = "death") -> pandas.DataFrame: |
|
data.columns = _BASE_FEATURE_NAMES |
|
data = data.rename(columns={"sex": "is_male"}) |
|
data = data.astype({"has_anaemia": "bool", "has_diabetes": "bool", "has_high_blood_pressure": "bool", "is_male": "bool", |
|
"is_smoker": "bool"}) |
|
|
|
return data |
|
|