File size: 8,540 Bytes
1c1b720
 
 
 
 
 
 
 
 
 
 
 
 
 
f62a0d7
1c1b720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16027da
1c1b720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Letter Dataset"""

from typing import List
from functools import partial
import string

import datasets

import pandas


VERSION = datasets.Version("1.0.0")

_ENCODING_DICS = {
	"letter": {letter: i for i, letter in enumerate(string.ascii_uppercase)}
}

DESCRIPTION = "Letter dataset."
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/170/letter"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/170/letter")
_CITATION = """
@misc{misc_letter_recognition_59,
  author       = {Slate,David},
  title        = {{Letter Recognition}},
  year         = {1991},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C5ZP40}}
}
"""

# Dataset info
urls_per_split = {
	"train": "https://huggingface.co/datasets/mstz/letter/resolve/main/letter.data"
}
features_types_per_config = {
	"letter": {
		"x-box": datasets.Value("int64"),
		"y-box": datasets.Value("int64"),
		"width": datasets.Value("int64"),
		"high": datasets.Value("int64"),
		"onpix": datasets.Value("int64"),
		"x-bar": datasets.Value("int64"),
		"y-bar": datasets.Value("int64"),
		"x2bar": datasets.Value("int64"),
		"y2bar": datasets.Value("int64"),
		"xybar": datasets.Value("int64"),
		"x2ybr": datasets.Value("int64"),
		"xy2br": datasets.Value("int64"),
		"x-ege": datasets.Value("int64"),
		"xegvy": datasets.Value("int64"),
		"y-ege": datasets.Value("int64"),
		"yegvx": datasets.Value("int64"),
		"letter": datasets.ClassLabel(num_classes=26)
	}
}
for i, letter in enumerate(string.ascii_uppercase):
	features_types_per_config[letter] = {
		"x-box": datasets.Value("int64"),
		"y-box": datasets.Value("int64"),
		"width": datasets.Value("int64"),
		"high": datasets.Value("int64"),
		"onpix": datasets.Value("int64"),
		"x-bar": datasets.Value("int64"),
		"y-bar": datasets.Value("int64"),
		"x2bar": datasets.Value("int64"),
		"y2bar": datasets.Value("int64"),
		"xybar": datasets.Value("int64"),
		"x2ybr": datasets.Value("int64"),
		"xy2br": datasets.Value("int64"),
		"x-ege": datasets.Value("int64"),
		"xegvy": datasets.Value("int64"),
		"y-ege": datasets.Value("int64"),
		"yegvx": datasets.Value("int64"),
		"letter": datasets.ClassLabel(num_classes=2)
	}

features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}


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


class Letter(datasets.GeneratorBasedBuilder):
	# dataset versions
	DEFAULT_CONFIG = "letter"
	BUILDER_CONFIGS = [
		LetterConfig(name="letter", description="Letter for multiclass classification."),		
		LetterConfig(name="A", description="Letter for binary letter A classification."),
		LetterConfig(name="B", description="Letter for binary letter B classification."),
		LetterConfig(name="C", description="Letter for binary letter C classification."),
		LetterConfig(name="D", description="Letter for binary letter D classification."),
		LetterConfig(name="E", description="Letter for binary letter E classification."),
		LetterConfig(name="F", description="Letter for binary letter F classification."),
		LetterConfig(name="G", description="Letter for binary letter G classification."),
		LetterConfig(name="H", description="Letter for binary letter H classification."),
		LetterConfig(name="I", description="Letter for binary letter I classification."),
		LetterConfig(name="J", description="Letter for binary letter J classification."),
		LetterConfig(name="K", description="Letter for binary letter K classification."),
		LetterConfig(name="L", description="Letter for binary letter L classification."),
		LetterConfig(name="M", description="Letter for binary letter M classification."),
		LetterConfig(name="N", description="Letter for binary letter N classification."),
		LetterConfig(name="O", description="Letter for binary letter O classification."),
		LetterConfig(name="P", description="Letter for binary letter P classification."),
		LetterConfig(name="Q", description="Letter for binary letter Q classification."),
		LetterConfig(name="R", description="Letter for binary letter R classification."),
		LetterConfig(name="S", description="Letter for binary letter S classification."),
		LetterConfig(name="T", description="Letter for binary letter T classification."),
		LetterConfig(name="U", description="Letter for binary letter U classification."),
		LetterConfig(name="V", description="Letter for binary letter V classification."),
		LetterConfig(name="W", description="Letter for binary letter W classification."),
		LetterConfig(name="X", description="Letter for binary letter X classification."),
		LetterConfig(name="Y", description="Letter for binary letter Y classification."),
		LetterConfig(name="Z", description="Letter for binary letter Z 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)
		data = self.preprocess(data)

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

			yield row_id, data_row

	def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame:
		for feature in _ENCODING_DICS:
			encoding_function = partial(self.encode, feature)
			data.loc[:, feature] = data[feature].apply(encoding_function)
		
		if self.config.name == "A":
			data.letter = data.letter.apply(lambda x: 1 if x == 0 else 0)
		elif self.config.name == "B":
			data.letter = data.letter.apply(lambda x: 1 if x == 1 else 0)
		elif self.config.name == "C":
			data.letter = data.letter.apply(lambda x: 1 if x == 2 else 0)
		elif self.config.name == "D":
			data.letter = data.letter.apply(lambda x: 1 if x == 3 else 0)
		elif self.config.name == "E":
			data.letter = data.letter.apply(lambda x: 1 if x == 4 else 0)
		elif self.config.name == "F":
			data.letter = data.letter.apply(lambda x: 1 if x == 5 else 0)
		elif self.config.name == "G":
			data.letter = data.letter.apply(lambda x: 1 if x == 6 else 0)
		elif self.config.name == "H":
			data.letter = data.letter.apply(lambda x: 1 if x == 7 else 0)
		elif self.config.name == "I":
			data.letter = data.letter.apply(lambda x: 1 if x == 8 else 0)
		elif self.config.name == "J":
			data.letter = data.letter.apply(lambda x: 1 if x == 9 else 0)
		elif self.config.name == "K":
			data.letter = data.letter.apply(lambda x: 1 if x == 10 else 0)
		elif self.config.name == "L":
			data.letter = data.letter.apply(lambda x: 1 if x == 11 else 0)
		elif self.config.name == "M":
			data.letter = data.letter.apply(lambda x: 1 if x == 12 else 0)
		elif self.config.name == "N":
			data.letter = data.letter.apply(lambda x: 1 if x == 13 else 0)
		elif self.config.name == "O":
			data.letter = data.letter.apply(lambda x: 1 if x == 14 else 0)
		elif self.config.name == "P":
			data.letter = data.letter.apply(lambda x: 1 if x == 15 else 0)
		elif self.config.name == "Q":
			data.letter = data.letter.apply(lambda x: 1 if x == 16 else 0)
		elif self.config.name == "R":
			data.letter = data.letter.apply(lambda x: 1 if x == 17 else 0)
		elif self.config.name == "S":
			data.letter = data.letter.apply(lambda x: 1 if x == 18 else 0)
		elif self.config.name == "T":
			data.letter = data.letter.apply(lambda x: 1 if x == 19 else 0)
		elif self.config.name == "U":
			data.letter = data.letter.apply(lambda x: 1 if x == 20 else 0)
		elif self.config.name == "V":
			data.letter = data.letter.apply(lambda x: 1 if x == 21 else 0)
		elif self.config.name == "W":
			data.letter = data.letter.apply(lambda x: 1 if x == 22 else 0)
		elif self.config.name == "X":
			data.letter = data.letter.apply(lambda x: 1 if x == 23 else 0)
		elif self.config.name == "Y":
			data.letter = data.letter.apply(lambda x: 1 if x == 24 else 0)
		elif self.config.name == "Z":
			data.letter = data.letter.apply(lambda x: 1 if x == 25 else 0)
				
		return data[list(features_types_per_config[self.config.name].keys())]

	def encode(self, feature, value):
		if feature in _ENCODING_DICS:
			return _ENCODING_DICS[feature][value]
		raise ValueError(f"Unknown feature: {feature}")