|
"""Optdigits Dataset""" |
|
|
|
from typing import List |
|
from functools import partial |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
_ENCODING_DICS = {} |
|
_BASE_FEATURE_NAMES = [ |
|
"att1", |
|
"att2", |
|
"att3", |
|
"att4", |
|
"att5", |
|
"att6", |
|
"att7", |
|
"att8", |
|
"att9", |
|
"att10", |
|
"att11", |
|
"att12", |
|
"att13", |
|
"att14", |
|
"att15", |
|
"att16", |
|
"att17", |
|
"att18", |
|
"att19", |
|
"att20", |
|
"att21", |
|
"att22", |
|
"att23", |
|
"att24", |
|
"att25", |
|
"att26", |
|
"att27", |
|
"att28", |
|
"att29", |
|
"att30", |
|
"att31", |
|
"att32", |
|
"att33", |
|
"att34", |
|
"att35", |
|
"att36", |
|
"att37", |
|
"att38", |
|
"att39", |
|
"att40", |
|
"att41", |
|
"att42", |
|
"att43", |
|
"att44", |
|
"att45", |
|
"att46", |
|
"att47", |
|
"att48", |
|
"att49", |
|
"att50", |
|
"att51", |
|
"att52", |
|
"att53", |
|
"att54", |
|
"att55", |
|
"att56", |
|
"att57", |
|
"att58", |
|
"att59", |
|
"att60", |
|
"att61", |
|
"att62", |
|
"att63", |
|
"att64", |
|
"class", |
|
] |
|
|
|
DESCRIPTION = "Optdigits dataset." |
|
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/80/optical+recognition+of+handwritten+digits" |
|
_URLS = ("https://archive-beta.ics.uci.edu/dataset/80/optical+recognition+of+handwritten+digits") |
|
_CITATION = """ |
|
@misc{misc_optical_recognition_of_handwritten_digits_80, |
|
author = {Alpaydin,E. & Kaynak,C.}, |
|
title = {{Optical Recognition of Handwritten Digits}}, |
|
year = {1998}, |
|
howpublished = {UCI Machine Learning Repository}, |
|
note = {{DOI}: \\url{10.24432/C50P49}} |
|
} |
|
""" |
|
|
|
|
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/optdigits/resolve/main/optdigits.data" |
|
} |
|
features_types_per_config = { |
|
"optdigits": { |
|
"att1": datasets.Value("int64"), |
|
"att2": datasets.Value("int64"), |
|
"att3": datasets.Value("int64"), |
|
"att4": datasets.Value("int64"), |
|
"att5": datasets.Value("int64"), |
|
"att6": datasets.Value("int64"), |
|
"att7": datasets.Value("int64"), |
|
"att8": datasets.Value("int64"), |
|
"att9": datasets.Value("int64"), |
|
"att10": datasets.Value("int64"), |
|
"att11": datasets.Value("int64"), |
|
"att12": datasets.Value("int64"), |
|
"att13": datasets.Value("int64"), |
|
"att14": datasets.Value("int64"), |
|
"att15": datasets.Value("int64"), |
|
"att16": datasets.Value("int64"), |
|
"att17": datasets.Value("int64"), |
|
"att18": datasets.Value("int64"), |
|
"att19": datasets.Value("int64"), |
|
"att20": datasets.Value("int64"), |
|
"att21": datasets.Value("int64"), |
|
"att22": datasets.Value("int64"), |
|
"att23": datasets.Value("int64"), |
|
"att24": datasets.Value("int64"), |
|
"att25": datasets.Value("int64"), |
|
"att26": datasets.Value("int64"), |
|
"att27": datasets.Value("int64"), |
|
"att28": datasets.Value("int64"), |
|
"att29": datasets.Value("int64"), |
|
"att30": datasets.Value("int64"), |
|
"att31": datasets.Value("int64"), |
|
"att32": datasets.Value("int64"), |
|
"att33": datasets.Value("int64"), |
|
"att34": datasets.Value("int64"), |
|
"att35": datasets.Value("int64"), |
|
"att36": datasets.Value("int64"), |
|
"att37": datasets.Value("int64"), |
|
"att38": datasets.Value("int64"), |
|
"att39": datasets.Value("int64"), |
|
"att40": datasets.Value("int64"), |
|
"att41": datasets.Value("int64"), |
|
"att42": datasets.Value("int64"), |
|
"att43": datasets.Value("int64"), |
|
"att44": datasets.Value("int64"), |
|
"att45": datasets.Value("int64"), |
|
"att46": datasets.Value("int64"), |
|
"att47": datasets.Value("int64"), |
|
"att48": datasets.Value("int64"), |
|
"att49": datasets.Value("int64"), |
|
"att50": datasets.Value("int64"), |
|
"att51": datasets.Value("int64"), |
|
"att52": datasets.Value("int64"), |
|
"att53": datasets.Value("int64"), |
|
"att54": datasets.Value("int64"), |
|
"att55": datasets.Value("int64"), |
|
"att56": datasets.Value("int64"), |
|
"att57": datasets.Value("int64"), |
|
"att58": datasets.Value("int64"), |
|
"att59": datasets.Value("int64"), |
|
"att60": datasets.Value("int64"), |
|
"att61": datasets.Value("int64"), |
|
"att62": datasets.Value("int64"), |
|
"att63": datasets.Value("int64"), |
|
"att64": datasets.Value("int64"), |
|
"class": datasets.ClassLabel(num_classes=10) |
|
} |
|
} |
|
for i in range(10): |
|
features_types_per_config[str(i)] = { |
|
"att1": datasets.Value("int64"), |
|
"att2": datasets.Value("int64"), |
|
"att3": datasets.Value("int64"), |
|
"att4": datasets.Value("int64"), |
|
"att5": datasets.Value("int64"), |
|
"att6": datasets.Value("int64"), |
|
"att7": datasets.Value("int64"), |
|
"att8": datasets.Value("int64"), |
|
"att9": datasets.Value("int64"), |
|
"att10": datasets.Value("int64"), |
|
"att11": datasets.Value("int64"), |
|
"att12": datasets.Value("int64"), |
|
"att13": datasets.Value("int64"), |
|
"att14": datasets.Value("int64"), |
|
"att15": datasets.Value("int64"), |
|
"att16": datasets.Value("int64"), |
|
"att17": datasets.Value("int64"), |
|
"att18": datasets.Value("int64"), |
|
"att19": datasets.Value("int64"), |
|
"att20": datasets.Value("int64"), |
|
"att21": datasets.Value("int64"), |
|
"att22": datasets.Value("int64"), |
|
"att23": datasets.Value("int64"), |
|
"att24": datasets.Value("int64"), |
|
"att25": datasets.Value("int64"), |
|
"att26": datasets.Value("int64"), |
|
"att27": datasets.Value("int64"), |
|
"att28": datasets.Value("int64"), |
|
"att29": datasets.Value("int64"), |
|
"att30": datasets.Value("int64"), |
|
"att31": datasets.Value("int64"), |
|
"att32": datasets.Value("int64"), |
|
"att33": datasets.Value("int64"), |
|
"att34": datasets.Value("int64"), |
|
"att35": datasets.Value("int64"), |
|
"att36": datasets.Value("int64"), |
|
"att37": datasets.Value("int64"), |
|
"att38": datasets.Value("int64"), |
|
"att39": datasets.Value("int64"), |
|
"att40": datasets.Value("int64"), |
|
"att41": datasets.Value("int64"), |
|
"att42": datasets.Value("int64"), |
|
"att43": datasets.Value("int64"), |
|
"att44": datasets.Value("int64"), |
|
"att45": datasets.Value("int64"), |
|
"att46": datasets.Value("int64"), |
|
"att47": datasets.Value("int64"), |
|
"att48": datasets.Value("int64"), |
|
"att49": datasets.Value("int64"), |
|
"att50": datasets.Value("int64"), |
|
"att51": datasets.Value("int64"), |
|
"att52": datasets.Value("int64"), |
|
"att53": datasets.Value("int64"), |
|
"att54": datasets.Value("int64"), |
|
"att55": datasets.Value("int64"), |
|
"att56": datasets.Value("int64"), |
|
"att57": datasets.Value("int64"), |
|
"att58": datasets.Value("int64"), |
|
"att59": datasets.Value("int64"), |
|
"att60": datasets.Value("int64"), |
|
"att61": datasets.Value("int64"), |
|
"att62": datasets.Value("int64"), |
|
"att63": datasets.Value("int64"), |
|
"att64": datasets.Value("int64"), |
|
"class": datasets.ClassLabel(num_classes=2) |
|
} |
|
|
|
|
|
|
|
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} |
|
|
|
|
|
class OptdigitsConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(OptdigitsConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Optdigits(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "optdigits" |
|
BUILDER_CONFIGS = [ |
|
OptdigitsConfig(name="optdigits", description="Optdigits for multiclass classification."), |
|
OptdigitsConfig(name="0", description="Optdigits for binary classification: is this a 0?."), |
|
OptdigitsConfig(name="1", description="Optdigits for binary classification: is this a 1?."), |
|
OptdigitsConfig(name="2", description="Optdigits for binary classification: is this a 2?."), |
|
OptdigitsConfig(name="3", description="Optdigits for binary classification: is this a 3?."), |
|
OptdigitsConfig(name="4", description="Optdigits for binary classification: is this a 4?."), |
|
OptdigitsConfig(name="5", description="Optdigits for binary classification: is this a 5?."), |
|
OptdigitsConfig(name="6", description="Optdigits for binary classification: is this a 6?."), |
|
OptdigitsConfig(name="7", description="Optdigits for binary classification: is this a 7?."), |
|
OptdigitsConfig(name="8", description="Optdigits for binary classification: is this a 8?."), |
|
OptdigitsConfig(name="9", description="Optdigits for binary classification: is this a 9?.") |
|
] |
|
|
|
|
|
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 |
|
|
|
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 == "0": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0) |
|
if self.config.name == "1": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0) |
|
if self.config.name == "2": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0) |
|
if self.config.name == "3": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 3 else 0) |
|
if self.config.name == "4": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 4 else 0) |
|
if self.config.name == "5": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 5 else 0) |
|
if self.config.name == "6": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 6 else 0) |
|
if self.config.name == "7": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 7 else 0) |
|
if self.config.name == "8": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 8 else 0) |
|
if self.config.name == "9": |
|
data["class"] = data["class"].apply(lambda x: 1 if x == 9 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}") |
|
|