abalone / abalone.py
mstz's picture
Update abalone.py
56bbaf9
"""Abalone."""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ORIGINAL_FEATURE_NAMES = [
"Sex",
"Length",
"Diameter",
"Height",
"Whole_weight",
"Shucked_weight",
"Viscera_weight",
"Shell_weight",
"Ring",
]
_BASE_FEATURE_NAMES = [
"sex",
"length",
"diameter",
"height",
"whole_weight",
"shucked_weight",
"viscera_weight",
"shell_weight",
"number_of_rings",
]
DESCRIPTION = "Abalone dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Abalone"
_URLS = ("https://huggingface.co/datasets/mstz/abalone/raw/abalone.data")
_CITATION = """
@misc{misc_abalone_1,
title = {{Abalone}},
year = {1995},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C55C7W}}
}"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/abalone/raw/main/abalone.data",
}
features_types_per_config = {
"abalone": {
"sex": datasets.Value("string"),
"length": datasets.Value("float64"),
"diameter": datasets.Value("float64"),
"height": datasets.Value("float64"),
"whole_weight": datasets.Value("float64"),
"shucked_weight": datasets.Value("float64"),
"viscera_weight": datasets.Value("float64"),
"shell_weight": datasets.Value("float64"),
"number_of_rings": datasets.Value("int8")
},
"binary": {
"sex": datasets.Value("string"),
"length": datasets.Value("float64"),
"diameter": datasets.Value("float64"),
"height": datasets.Value("float64"),
"whole_weight": datasets.Value("float64"),
"shucked_weight": datasets.Value("float64"),
"viscera_weight": datasets.Value("float64"),
"shell_weight": datasets.Value("float64"),
"is_old": datasets.ClassLabel(num_classes=2)
}
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
class AbaloneConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(AbaloneConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Abalone(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "abalone"
BUILDER_CONFIGS = [
AbaloneConfig(name="abalone", description="Abalone for regression."),
AbaloneConfig(name="binary", description="Abalone 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, header=None)
data.columns = _BASE_FEATURE_NAMES
if self.config.name == "binary":
data = data.rename(columns={"number_of_rings": "is_old"})
data["is_old"] = data["is_old"].apply(lambda x: 1 if x > 9 else 0)
for row_id, row in data.iterrows():
data_row = dict(row)
yield row_id, data_row