wine / wine.py
mstz's picture
Update wine.py
5e6567b
"""Wine Dataset"""
from typing import List
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
"fixed_acidity",
"volatile_acidity",
"citric_acid",
"residual_sugar",
"chlorides",
"free_sulfur_dioxide",
"total_sulfur_dioxide",
"density",
"pH",
"sulphates",
"alcohol",
"quality",
"is_red"
]
DESCRIPTION = "Wine quality dataset."
_HOMEPAGE = "https://www.kaggle.com/datasets/ghassenkhaled/wine-quality-data"
_URLS = ("https://www.kaggle.com/datasets/ghassenkhaled/wine-quality-data")
_CITATION = """"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/wine/raw/main/Wine_Quality_Data.csv",
}
features_types_per_config = {
"wine": {
"fixed_acidity": datasets.Value("float64"),
"volatile_acidity": datasets.Value("float64"),
"citric_acid": datasets.Value("float64"),
"residual_sugar": datasets.Value("float64"),
"chlorides": datasets.Value("float64"),
"free_sulfur_dioxide": datasets.Value("float64"),
"total_sulfur_dioxide": datasets.Value("float64"),
"density": datasets.Value("float64"),
"pH": datasets.Value("float64"),
"sulphates": datasets.Value("float64"),
"alcohol": datasets.Value("float64"),
"quality": datasets.Value("int8"),
"is_red": datasets.ClassLabel(num_classes=2, names=("red", "white"))
}
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
class WineConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(WineConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Wine(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "wine"
BUILDER_CONFIGS = [
WineConfig(name="wine",
description="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, config=self.config.name)
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 = data.rename(columns={"color": "is_red"})
data.loc[data.is_red == "red", "is_red"] = 0
data.loc[data.is_red == "white", "is_red"] = 1
return data