letter / letter.py
mstz's picture
Upload letter.py
16027da
"""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}")