seeds / seeds.py
mstz's picture
Upload seeds.py
2753b90
"""Seeds Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ENCODING_DICS = {}
DESCRIPTION = "Seeds dataset."
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification")
_CITATION = """
@misc{misc_seeds_236,
author = {Charytanowicz,Magorzata, Niewczas,Jerzy, Kulczycki,Piotr, Kowalski,Piotr & Lukasik,Szymon},
title = {{seeds}},
year = {2012},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C5H30K}}
}
"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/seeds/raw/main/seeds.csv"
}
features_types_per_config = {
"seeds": {
"area": datasets.Value("float64"),
"perimeter": datasets.Value("float64"),
"compactness": datasets.Value("float64"),
"length": datasets.Value("float64"),
"width": datasets.Value("float64"),
"asymmetry": datasets.Value("float64"),
"length_grove": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=3),
},
"seeds_0": {
"area": datasets.Value("float64"),
"perimeter": datasets.Value("float64"),
"compactness": datasets.Value("float64"),
"length": datasets.Value("float64"),
"width": datasets.Value("float64"),
"asymmetry": datasets.Value("float64"),
"length_grove": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"seeds_1": {
"area": datasets.Value("float64"),
"perimeter": datasets.Value("float64"),
"compactness": datasets.Value("float64"),
"length": datasets.Value("float64"),
"width": datasets.Value("float64"),
"asymmetry": datasets.Value("float64"),
"length_grove": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"seeds_2": {
"area": datasets.Value("float64"),
"perimeter": datasets.Value("float64"),
"compactness": datasets.Value("float64"),
"length": datasets.Value("float64"),
"width": datasets.Value("float64"),
"asymmetry": datasets.Value("float64"),
"length_grove": datasets.Value("float64"),
"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 SeedsConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(SeedsConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Seeds(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "seeds"
BUILDER_CONFIGS = [
SeedsConfig(name="seeds", description="Seeds for multiclass classification."),
SeedsConfig(name="seeds_0", description="Seeds for binary classification."),
SeedsConfig(name="seeds_1", description="Seeds for binary classification."),
SeedsConfig(name="seeds_2", description="Seeds for binary 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:
data["class"] = data["class"].apply(lambda x: x - 1)
if self.config.name == "seeds_0":
data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0)
elif self.config.name == "seeds_1":
data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0)
elif self.config.name == "seeds_2":
data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0)
for feature in _ENCODING_DICS:
encoding_function = partial(self.encode, feature)
data.loc[:, feature] = data[feature].apply(encoding_function)
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}")