mstz commited on
Commit
1c1b720
1 Parent(s): a76289d

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +25 -1
  2. letter.data +0 -0
  3. letter.py +205 -0
README.md CHANGED
@@ -1,3 +1,27 @@
1
  ---
2
- license: cc-by-4.0
 
 
 
 
 
 
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language:
3
+ - en
4
+ tags:
5
+ - letter
6
+ - tabular_classification
7
+ - multiclass_classification
8
+ - binary_classification
9
+ - UCI
10
+ pretty_name: Letter
11
+ task_categories: # Full list at https://github.com/huggingface/hub-docs/blob/main/js/src/lib/interfaces/Types.ts
12
+ - tabular-classification
13
+ configs:
14
+ - letter
15
  ---
16
+ # Letter
17
+ The [Letter dataset](https://archive-beta.ics.uci.edu/dataset/59/letter+recognition) from the [UCI repository](https://archive-beta.ics.uci.edu/).
18
+ Letter recognition.
19
+
20
+ # Configurations and tasks
21
+ | **Configuration** | **Task** | **Description** |
22
+ |-----------------------|---------------------------|-------------------------|
23
+ | letter | Multiclass classification.| |
24
+ | A | Binary classification. | Is this letter A? |
25
+ | B | Binary classification. | Is this letter B? |
26
+ | C | Binary classification. | Is this letter C? |
27
+ | ... | Binary classification. | ... |
letter.data ADDED
The diff for this file is too large to render. See raw diff
 
letter.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Letter Dataset"""
2
+
3
+ from typing import List
4
+ from functools import partial
5
+ import string
6
+
7
+ import datasets
8
+
9
+ import pandas
10
+
11
+
12
+ VERSION = datasets.Version("1.0.0")
13
+
14
+ _ENCODING_DICS = {
15
+ "letter": {letter: i for i, letter in zip(string.ascii_uppercase)}
16
+ }
17
+
18
+ DESCRIPTION = "Letter dataset."
19
+ _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/170/letter"
20
+ _URLS = ("https://archive-beta.ics.uci.edu/dataset/170/letter")
21
+ _CITATION = """
22
+ @misc{misc_letter_recognition_59,
23
+ author = {Slate,David},
24
+ title = {{Letter Recognition}},
25
+ year = {1991},
26
+ howpublished = {UCI Machine Learning Repository},
27
+ note = {{DOI}: \\url{10.24432/C5ZP40}}
28
+ }
29
+ """
30
+
31
+ # Dataset info
32
+ urls_per_split = {
33
+ "train": "https://huggingface.co/datasets/mstz/letter/resolve/main/letter.data"
34
+ }
35
+ features_types_per_config = {
36
+ "letter": {
37
+ "x-box": datasets.Value("int64"),
38
+ "y-box": datasets.Value("int64"),
39
+ "width": datasets.Value("int64"),
40
+ "high": datasets.Value("int64"),
41
+ "onpix": datasets.Value("int64"),
42
+ "x-bar": datasets.Value("int64"),
43
+ "y-bar": datasets.Value("int64"),
44
+ "x2bar": datasets.Value("int64"),
45
+ "y2bar": datasets.Value("int64"),
46
+ "xybar": datasets.Value("int64"),
47
+ "x2ybr": datasets.Value("int64"),
48
+ "xy2br": datasets.Value("int64"),
49
+ "x-ege": datasets.Value("int64"),
50
+ "xegvy": datasets.Value("int64"),
51
+ "y-ege": datasets.Value("int64"),
52
+ "yegvx": datasets.Value("int64"),
53
+ "letter": datasets.ClassLabel(num_classes=26)
54
+ }
55
+ }
56
+ for i, letter in enumerate(string.ascii_uppercase):
57
+ features_types_per_config[letter] = {
58
+ "x-box": datasets.Value("int64"),
59
+ "y-box": datasets.Value("int64"),
60
+ "width": datasets.Value("int64"),
61
+ "high": datasets.Value("int64"),
62
+ "onpix": datasets.Value("int64"),
63
+ "x-bar": datasets.Value("int64"),
64
+ "y-bar": datasets.Value("int64"),
65
+ "x2bar": datasets.Value("int64"),
66
+ "y2bar": datasets.Value("int64"),
67
+ "xybar": datasets.Value("int64"),
68
+ "x2ybr": datasets.Value("int64"),
69
+ "xy2br": datasets.Value("int64"),
70
+ "x-ege": datasets.Value("int64"),
71
+ "xegvy": datasets.Value("int64"),
72
+ "y-ege": datasets.Value("int64"),
73
+ "yegvx": datasets.Value("int64"),
74
+ "letter": datasets.ClassLabel(num_classes=2)
75
+ }
76
+
77
+ features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
78
+
79
+
80
+ class LetterConfig(datasets.BuilderConfig):
81
+ def __init__(self, **kwargs):
82
+ super(LetterConfig, self).__init__(version=VERSION, **kwargs)
83
+ self.features = features_per_config[kwargs["name"]]
84
+
85
+
86
+ class Letter(datasets.GeneratorBasedBuilder):
87
+ # dataset versions
88
+ DEFAULT_CONFIG = "letter"
89
+ BUILDER_CONFIGS = [
90
+ LetterConfig(name="letter", description="Letter for multiclass classification."),
91
+ LetterConfig(name="A", description="Letter for binary letter A classification."),
92
+ LetterConfig(name="B", description="Letter for binary letter B classification."),
93
+ LetterConfig(name="C", description="Letter for binary letter C classification."),
94
+ LetterConfig(name="D", description="Letter for binary letter D classification."),
95
+ LetterConfig(name="E", description="Letter for binary letter E classification."),
96
+ LetterConfig(name="F", description="Letter for binary letter F classification."),
97
+ LetterConfig(name="G", description="Letter for binary letter G classification."),
98
+ LetterConfig(name="H", description="Letter for binary letter H classification."),
99
+ LetterConfig(name="I", description="Letter for binary letter I classification."),
100
+ LetterConfig(name="J", description="Letter for binary letter J classification."),
101
+ LetterConfig(name="K", description="Letter for binary letter K classification."),
102
+ LetterConfig(name="L", description="Letter for binary letter L classification."),
103
+ LetterConfig(name="M", description="Letter for binary letter M classification."),
104
+ LetterConfig(name="N", description="Letter for binary letter N classification."),
105
+ LetterConfig(name="O", description="Letter for binary letter O classification."),
106
+ LetterConfig(name="P", description="Letter for binary letter P classification."),
107
+ LetterConfig(name="Q", description="Letter for binary letter Q classification."),
108
+ LetterConfig(name="R", description="Letter for binary letter R classification."),
109
+ LetterConfig(name="S", description="Letter for binary letter S classification."),
110
+ LetterConfig(name="T", description="Letter for binary letter T classification."),
111
+ LetterConfig(name="U", description="Letter for binary letter U classification."),
112
+ LetterConfig(name="V", description="Letter for binary letter V classification."),
113
+ LetterConfig(name="W", description="Letter for binary letter W classification."),
114
+ LetterConfig(name="X", description="Letter for binary letter X classification."),
115
+ LetterConfig(name="Y", description="Letter for binary letter Y classification."),
116
+ LetterConfig(name="Z", description="Letter for binary letter Z classification."),
117
+ ]
118
+
119
+
120
+ def _info(self):
121
+ info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
122
+ features=features_per_config[self.config.name])
123
+
124
+ return info
125
+
126
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
127
+ downloads = dl_manager.download_and_extract(urls_per_split)
128
+
129
+ return [
130
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
131
+ ]
132
+
133
+ def _generate_examples(self, filepath: str):
134
+ data = pandas.read_csv(filepath, header=None)
135
+ data = self.preprocess(data)
136
+
137
+ for row_id, row in data.iterrows():
138
+ data_row = dict(row)
139
+
140
+ yield row_id, data_row
141
+
142
+ def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame:
143
+ for feature in _ENCODING_DICS:
144
+ encoding_function = partial(self.encode, feature)
145
+ data.loc[:, feature] = data[feature].apply(encoding_function)
146
+
147
+ if self.config.name == "A":
148
+ data.letter = data.letter.apply(lambda x: 1 if x == 0 else 0)
149
+ elif self.config.name == "B":
150
+ data.letter = data.letter.apply(lambda x: 1 if x == 1 else 0)
151
+ elif self.config.name == "C":
152
+ data.letter = data.letter.apply(lambda x: 1 if x == 2 else 0)
153
+ elif self.config.name == "D":
154
+ data.letter = data.letter.apply(lambda x: 1 if x == 3 else 0)
155
+ elif self.config.name == "E":
156
+ data.letter = data.letter.apply(lambda x: 1 if x == 4 else 0)
157
+ elif self.config.name == "F":
158
+ data.letter = data.letter.apply(lambda x: 1 if x == 5 else 0)
159
+ elif self.config.name == "G":
160
+ data.letter = data.letter.apply(lambda x: 1 if x == 6 else 0)
161
+ elif self.config.name == "H":
162
+ data.letter = data.letter.apply(lambda x: 1 if x == 7 else 0)
163
+ elif self.config.name == "I":
164
+ data.letter = data.letter.apply(lambda x: 1 if x == 8 else 0)
165
+ elif self.config.name == "J":
166
+ data.letter = data.letter.apply(lambda x: 1 if x == 9 else 0)
167
+ elif self.config.name == "K":
168
+ data.letter = data.letter.apply(lambda x: 1 if x == 10 else 0)
169
+ elif self.config.name == "L":
170
+ data.letter = data.letter.apply(lambda x: 1 if x == 11 else 0)
171
+ elif self.config.name == "M":
172
+ data.letter = data.letter.apply(lambda x: 1 if x == 12 else 0)
173
+ elif self.config.name == "N":
174
+ data.letter = data.letter.apply(lambda x: 1 if x == 13 else 0)
175
+ elif self.config.name == "O":
176
+ data.letter = data.letter.apply(lambda x: 1 if x == 14 else 0)
177
+ elif self.config.name == "P":
178
+ data.letter = data.letter.apply(lambda x: 1 if x == 15 else 0)
179
+ elif self.config.name == "Q":
180
+ data.letter = data.letter.apply(lambda x: 1 if x == 16 else 0)
181
+ elif self.config.name == "R":
182
+ data.letter = data.letter.apply(lambda x: 1 if x == 17 else 0)
183
+ elif self.config.name == "S":
184
+ data.letter = data.letter.apply(lambda x: 1 if x == 18 else 0)
185
+ elif self.config.name == "T":
186
+ data.letter = data.letter.apply(lambda x: 1 if x == 19 else 0)
187
+ elif self.config.name == "U":
188
+ data.letter = data.letter.apply(lambda x: 1 if x == 20 else 0)
189
+ elif self.config.name == "V":
190
+ data.letter = data.letter.apply(lambda x: 1 if x == 21 else 0)
191
+ elif self.config.name == "W":
192
+ data.letter = data.letter.apply(lambda x: 1 if x == 22 else 0)
193
+ elif self.config.name == "X":
194
+ data.letter = data.letter.apply(lambda x: 1 if x == 23 else 0)
195
+ elif self.config.name == "Y":
196
+ data.letter = data.letter.apply(lambda x: 1 if x == 24 else 0)
197
+ elif self.config.name == "Z":
198
+ data.letter = data.letter.apply(lambda x: 1 if x == 25 else 0)
199
+
200
+ return data[list(features_types_per_config[self.config.name].keys())]
201
+
202
+ def encode(self, feature, value):
203
+ if feature in _ENCODING_DICS:
204
+ return _ENCODING_DICS[feature][value]
205
+ raise ValueError(f"Unknown feature: {feature}")