File size: 2,705 Bytes
5804590
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0f929b
5804590
 
 
 
246038c
5804590
 
 
 
 
 
252362a
5804590
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
828d957
5804590
13e8057
5804590
0b80b42
66e1ef0
5804590
 
 
 
 
026824d
 
01eb087
10a3f36
1a12629
 
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
"""Isolet dataset."""

from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")

DESCRIPTION = "Isolet dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Isolet"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/54/isolet")
_CITATION = """
@misc{misc_isolet_54,
  author       = {Cole,Ron & Fanty,Mark},
  title        = {{ISOLET}},
  year         = {1994},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C51G69}}
}"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co./datasets/mstz/isolet/resolve/main/isolet.zip"
}
features_types_per_config = {
    "isolet": {
        str(i): datasets.Value("float64") for i in range(617)
    }
}
features_types_per_config["isolet"]["617"] = datasets.ClassLabel(num_classes=26)
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}


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


class Isolet(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "isolet"
    BUILDER_CONFIGS = [
        IsoletConfig(name="isolet",
                     description="Isolet for letter 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 + "/isolet1+2+3+4.data", header=None).infer_objects()
        data = self.preprocess(data, config=self.config.name)
        
        for row_id, row in data.iterrows():
            data_row = dict(row)
            data_row["617"] = int(data_row["617"])

            yield row_id, data_row

    def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame:
        data.columns = [str(i) for i in range(618)]
        
        data = data.astype({"617": "int8"})
        data.loc[:, "617"] = data["617"].apply(lambda x: int(x) - 1)
        data = data.astype({str(i): "float64" for i in range(617)})

        return data