nursery / nursery.py
mstz's picture
Upload nursery.py
b298fdd
"""Nursery Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ENCODING_DICS = {
"is_family_financially_stable": {
"convenient": True,
"inconvenient": False
}
}
DESCRIPTION = "Nursery dataset."
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/69/molecular+biology+nursery+junction+gene+sequences"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/69/molecular+biology+nursery+junction+gene+sequences")
_CITATION = """
@misc{misc_nursery_76,
author = {Rajkovic,Vladislav},
title = {{Nursery}},
year = {1997},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C5P88W}}
}
"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/nursery/raw/main/nursery.data"
}
features_types_per_config = {
"nursery": {
"parents_attitude": datasets.Value("string"),
"current_nursery_status": datasets.Value("string"),
"form": datasets.Value("string"),
"number_of_children": datasets.Value("int8"),
"housing_status": datasets.Value("string"),
"is_family_financially_stable": datasets.Value("bool"),
"social_status": datasets.Value("string"),
"health_status": datasets.Value("string"),
"recommendation": datasets.ClassLabel(num_classes=5, names=("not recommended", "recommended", "priority recommendation",
"highly recommended", "specifically recommended"))
},
"nursery_binary": {
"parents_attitude": datasets.Value("string"),
"current_nursery_status": datasets.Value("string"),
"form": datasets.Value("string"),
"number_of_children": datasets.Value("int8"),
"housing_status": datasets.Value("string"),
"is_family_financially_stable": datasets.Value("bool"),
"social_status": datasets.Value("string"),
"health_status": datasets.Value("string"),
"recommendation": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
},
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
class NurseryConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(NurseryConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Nursery(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "nursery"
BUILDER_CONFIGS = [
NurseryConfig(name="nursery",
description="Nursery for multiclass classification."),
NurseryConfig(name="nursery_binary",
description="Nursery 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:
if self.config.name == "nursery_binary":
data["recommendation"] = data["recommendation"].apply(lambda x: 1 if x > 0 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}")