|
from typing import List |
|
from functools import partial |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
|
|
DESCRIPTION = "Car dataset from the UCI repository." |
|
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/19/car+evaluation" |
|
_URLS = ("https://archive-beta.ics.uci.edu/dataset/19/car+evaluation") |
|
_CITATION = """ |
|
@misc{misc_car_evaluation_19, |
|
author = {Bohanec,Marko}, |
|
title = {{Car Evaluation}}, |
|
year = {1997}, |
|
howpublished = {UCI Machine Learning Repository}, |
|
note = {{DOI}: \\url{10.24432/C5JP48}} |
|
} |
|
""" |
|
|
|
|
|
_BASE_FEATURE_NAMES = [ |
|
"buying", |
|
"maint", |
|
"doors", |
|
"persons", |
|
"lug_boot", |
|
"safety", |
|
"acceptability_level" |
|
] |
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/car/raw/main/car.data" |
|
} |
|
features_types_per_config = { |
|
"car": { |
|
"buying": datasets.Value("int8"), |
|
"maint": datasets.Value("int8"), |
|
"doors": datasets.Value("int8"), |
|
"persons": datasets.Value("int8"), |
|
"lug_boot": datasets.Value("int8"), |
|
"safety": datasets.Value("int8"), |
|
"acceptability_level": datasets.ClassLabel(num_classes=4, |
|
names=("unacceptable", "acceptable", "good", "very good")) |
|
}, |
|
"car_binary": { |
|
"buying": datasets.Value("int8"), |
|
"maint": datasets.Value("int8"), |
|
"doors": datasets.Value("int8"), |
|
"persons": datasets.Value("int8"), |
|
"lug_boot": datasets.Value("int8"), |
|
"safety": datasets.Value("int8"), |
|
"acceptability_level": datasets.ClassLabel(num_classes=2, |
|
names=("unacceptable", "acceptable")) |
|
}, |
|
|
|
} |
|
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} |
|
|
|
_ENCODING_DICS = { |
|
"buying": { |
|
"vhigh": 3, |
|
"high": 2, |
|
"med": 1, |
|
"low": 0 |
|
}, |
|
"maint": { |
|
"vhigh": 3, |
|
"high": 2, |
|
"med": 1, |
|
"low": 0 |
|
}, |
|
"doors": { |
|
"0": 0, |
|
"1": 1, |
|
"2": 2, |
|
"3": 3, |
|
"4": 4, |
|
"5more": 5 |
|
}, |
|
"persons": { |
|
"0": 0, |
|
"1": 1, |
|
"2": 2, |
|
"3": 3, |
|
"4": 4, |
|
"more": 5 |
|
}, |
|
"lug_boot": { |
|
"big": 2, |
|
"med": 1, |
|
"small": 0, |
|
}, |
|
"safety": { |
|
"high": 2, |
|
"med": 1, |
|
"low": 0, |
|
}, |
|
"acceptability_level": { |
|
"unacc": 0, |
|
"acc": 1, |
|
"good": 2, |
|
"vgood": 3 |
|
} |
|
|
|
} |
|
|
|
class CarConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(CarConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Car(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "car" |
|
BUILDER_CONFIGS = [ |
|
CarConfig(name="car", |
|
description="Car for 4-ary classification."), |
|
CarConfig(name="car_binary", |
|
description="Car 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 = 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, config: str = DEFAULT_CONFIG) -> pandas.DataFrame: |
|
data.columns = _BASE_FEATURE_NAMES |
|
|
|
for feature in _ENCODING_DICS: |
|
encoding_function = partial(self.encode, feature) |
|
data.loc[:, feature] = data[feature].apply(encoding_function) |
|
|
|
if config == "car_binary": |
|
data.loc[:, "acceptability_level"] = data["acceptability_level"].apply(lambda x: x if x == 0 else 1) |
|
|
|
|
|
return data |
|
|
|
def encode(self, feature, value): |
|
if feature in _ENCODING_DICS: |
|
return _ENCODING_DICS[feature][value] |
|
raise ValueError(f"Unknown feature: {feature}") |
|
|