| """Balance_Scale""" |
|
|
| from typing import List |
| from functools import partial |
|
|
| import datasets |
|
|
| import pandas |
|
|
|
|
| VERSION = datasets.Version("1.0.0") |
| _BASE_FEATURE_NAMES = [ |
| "balance", |
| "left_weight", |
| "left_distance", |
| "right_weight", |
| "right_distance", |
| ] |
|
|
| DESCRIPTION = "Balance_Scale dataset from the UCI ML repository." |
| _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Balance_Scale" |
| _URLS = ("https://huggingface.co/datasets/mstz/balance_scale/raw/balance_scale.data") |
| _CITATION = """ |
| @misc{misc_balance_scale_12, |
| title = {{Balance Scale}}, |
| year = {1994}, |
| howpublished = {UCI Machine Learning Repository}, |
| note = {{DOI}: \\url{10.24432/C5488X}} |
| }""" |
|
|
| |
| urls_per_split = { |
| "train": "https://huggingface.co/datasets/mstz/balance_scale/raw/main/balance_scale.data", |
| } |
| features_types_per_config = { |
| "balance": { |
| "left_weight": datasets.Value("int64"), |
| "left_distance": datasets.Value("int64"), |
| "right_weight": datasets.Value("int64"), |
| "right_distance": datasets.Value("int64"), |
| "balance": datasets.ClassLabel(num_classes=3, names=("tips_left", "balanced", "tips_right")) |
| }, |
| "is_balanced": { |
| "left_weight": datasets.Value("int64"), |
| "left_distance": datasets.Value("int64"), |
| "right_weight": datasets.Value("int64"), |
| "right_distance": datasets.Value("int64"), |
| "is_balanced": 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 Balance_ScaleConfig(datasets.BuilderConfig): |
| def __init__(self, **kwargs): |
| super(Balance_ScaleConfig, self).__init__(version=VERSION, **kwargs) |
| self.features = features_per_config[kwargs["name"]] |
|
|
|
|
| class Balance_Scale(datasets.GeneratorBasedBuilder): |
| |
| DEFAULT_CONFIG = "balance" |
| BUILDER_CONFIGS = [ |
| Balance_ScaleConfig(name="balance", description="Multiclass classification of the scale balance."), |
| Balance_ScaleConfig(name="is_balanced", description="Binary classification of the scale balance."), |
| ] |
|
|
|
|
| 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 = data[["left_weight", "left_distance", "right_weight", "right_distance", "balance"]] |
| data["balance"] = data.balance.apply(lambda x: 0 if x == "L" else 1 if x == "B" else 2) |
| if self.config.name == "is_balanced": |
| data["balance"] = data.balance.apply(lambda x: 1 if x == 1 else 0) |
| data = data.rename(columns={"balance": "is_balanced"}) |
|
|
| for row_id, row in data.iterrows(): |
| data_row = dict(row) |
|
|
| yield row_id, data_row |
|
|