File size: 3,604 Bytes
1f0ab33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df37011
1f0ab33
 
 
 
1a6984c
56bbaf9
 
 
 
 
 
 
 
 
 
 
1f0ab33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bbaf9
 
1f0ab33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bbaf9
 
 
 
1f0ab33
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Abalone."""

from typing import List
from functools import partial

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_ORIGINAL_FEATURE_NAMES = [
    "Sex",
    "Length",
    "Diameter",
    "Height",
    "Whole_weight",
    "Shucked_weight",
    "Viscera_weight",
    "Shell_weight",
    "Ring",
]
_BASE_FEATURE_NAMES = [
    "sex",
    "length",
    "diameter",
    "height",
    "whole_weight",
    "shucked_weight",
    "viscera_weight",
    "shell_weight",
    "number_of_rings",
]

DESCRIPTION = "Abalone dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Abalone"
_URLS = ("https://huggingface.co/datasets/mstz/abalone/raw/abalone.data")
_CITATION = """
@misc{misc_abalone_1,
  title        = {{Abalone}},
  year         = {1995},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C55C7W}}
}"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/abalone/raw/main/abalone.data",
}
features_types_per_config = {
    "abalone": {
        "sex": datasets.Value("string"),
        "length": datasets.Value("float64"),
        "diameter": datasets.Value("float64"),
        "height": datasets.Value("float64"),
        "whole_weight": datasets.Value("float64"),
        "shucked_weight": datasets.Value("float64"),
        "viscera_weight": datasets.Value("float64"),
        "shell_weight": datasets.Value("float64"),
        "number_of_rings": datasets.Value("int8")
    },
    "binary": {
        "sex": datasets.Value("string"),
        "length": datasets.Value("float64"),
        "diameter": datasets.Value("float64"),
        "height": datasets.Value("float64"),
        "whole_weight": datasets.Value("float64"),
        "shucked_weight": datasets.Value("float64"),
        "viscera_weight": datasets.Value("float64"),
        "shell_weight": datasets.Value("float64"),
        "is_old": datasets.ClassLabel(num_classes=2)
    }
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}


class AbaloneConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(AbaloneConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Abalone(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "abalone"
    BUILDER_CONFIGS = [
        AbaloneConfig(name="abalone", description="Abalone for regression."),
        AbaloneConfig(name="binary", description="Abalone 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

        if self.config.name == "binary":
            data = data.rename(columns={"number_of_rings": "is_old"})
            data["is_old"] = data["is_old"].apply(lambda x: 1 if x > 9 else 0)

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

            yield row_id, data_row