wine_origin / wine_origin.py
mstz's picture
Upload wine_origin.py
bda962e
"""WineOrigin Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ENCODING_DICS = {}
DESCRIPTION = "WineOrigin dataset."
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/108/wine_origin+database+generator+version+2"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/108/wine_origin+database+generator+version+2")
_CITATION = """
@misc{misc_wine_origin_database_generator_(version_2)_108,
author = {Breiman,L. & Stone,C.J.},
title = {{Waveform Database Generator (Version 2)}},
year = {1988},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C56014}}
}
"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/wine_origin/raw/main/wine.data"
}
features_types_per_config = {
"wine_origin": {
"malic_acid": datasets.Value("float64"),
"ash": datasets.Value("float64"),
"alcalinity_of_ash": datasets.Value("float64"),
"magnesium": datasets.Value("float64"),
"phenols": datasets.Value("float64"),
"flavanoids": datasets.Value("float64"),
"nonflavanoid_phenols": datasets.Value("float64"),
"proanthocyanins": datasets.Value("float64"),
"color_intensity": datasets.Value("float64"),
"hue": datasets.Value("float64"),
"diluted_wines": datasets.Value("float64"),
"proline": datasets.Value("float64"),
"unknown": datasets.Value("int64"),
"class": datasets.ClassLabel(num_classes=3)
},
"wine_origin_0": {
"malic_acid": datasets.Value("float64"),
"ash": datasets.Value("float64"),
"alcalinity_of_ash": datasets.Value("float64"),
"magnesium": datasets.Value("float64"),
"phenols": datasets.Value("float64"),
"flavanoids": datasets.Value("float64"),
"nonflavanoid_phenols": datasets.Value("float64"),
"proanthocyanins": datasets.Value("float64"),
"color_intensity": datasets.Value("float64"),
"hue": datasets.Value("float64"),
"diluted_wines": datasets.Value("float64"),
"proline": datasets.Value("float64"),
"unknown": datasets.Value("int64"),
"class": datasets.ClassLabel(num_classes=2)
},
"wine_origin_1": {
"malic_acid": datasets.Value("float64"),
"ash": datasets.Value("float64"),
"alcalinity_of_ash": datasets.Value("float64"),
"magnesium": datasets.Value("float64"),
"phenols": datasets.Value("float64"),
"flavanoids": datasets.Value("float64"),
"nonflavanoid_phenols": datasets.Value("float64"),
"proanthocyanins": datasets.Value("float64"),
"color_intensity": datasets.Value("float64"),
"hue": datasets.Value("float64"),
"diluted_wines": datasets.Value("float64"),
"proline": datasets.Value("float64"),
"unknown": datasets.Value("int64"),
"class": datasets.ClassLabel(num_classes=2)
},
"wine_origin_2": {
"malic_acid": datasets.Value("float64"),
"ash": datasets.Value("float64"),
"alcalinity_of_ash": datasets.Value("float64"),
"magnesium": datasets.Value("float64"),
"phenols": datasets.Value("float64"),
"flavanoids": datasets.Value("float64"),
"nonflavanoid_phenols": datasets.Value("float64"),
"proanthocyanins": datasets.Value("float64"),
"color_intensity": datasets.Value("float64"),
"hue": datasets.Value("float64"),
"diluted_wines": datasets.Value("float64"),
"proline": datasets.Value("float64"),
"unknown": datasets.Value("int64"),
"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 WineOriginConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(WineOriginConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class WineOrigin(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "wine_origin"
BUILDER_CONFIGS = [
WineOriginConfig(name="wine_origin", description="WineOrigin for multiclass classification."),
WineOriginConfig(name="wine_origin_0", description="WineOrigin for binary classification."),
WineOriginConfig(name="wine_origin_1", description="WineOrigin for binary classification."),
WineOriginConfig(name="wine_origin_2", description="WineOrigin 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 == "wine_origin_0":
data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0)
elif self.config.name == "wine_origin_1":
data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0)
elif self.config.name == "wine_origin_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}")