File size: 2,295 Bytes
339e263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Haberman"""

from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")

DESCRIPTION = "Haberman dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Haberman"
_URLS = ("https://archive.ics.uci.edu/ml/datasets/Haberman")
_CITATION = """
@misc{misc_haberman's_survival_43,
  author       = {Haberman,S.},
  title        = {{Haberman's Survival}},
  year         = {1999},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C5XK51}}
}"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/haberman/raw/main/haberman.data"
}
features_types_per_config = {
    "survival": {
        "age": datasets.Value("int32"),
        "year_of_operation": datasets.Value("int32"),
        "number_of_axillary_nodes": datasets.Value("int32"),
        "has_survived_5_years": 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 HabermanConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(HabermanConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Haberman(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "survival"
    BUILDER_CONFIGS = [
        HabermanConfig(name="survival",
                       description="Haberman 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)

        for row_id, row in data.iterrows():
            data_row = dict(row)

            yield row_id, data_row