|
from typing import List |
|
from functools import partial |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
|
|
DESCRIPTION = "Iris efficiency dataset from the UCI repository." |
|
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/53/iris" |
|
_URLS = ("https://archive-beta.ics.uci.edu/dataset/53/iris") |
|
_CITATION = """ |
|
@misc{misc_iris_53, |
|
author = {Fisher,R. A. & Fisher,R.A.}, |
|
title = {{Iris}}, |
|
year = {1988}, |
|
howpublished = {UCI Machine Learning Repository}, |
|
note = {{DOI}: \\url{10.24432/C56C76}} |
|
}""" |
|
|
|
|
|
_BASE_FEATURE_NAMES = [ |
|
"sepal_length", |
|
"sepal_width", |
|
"petal_length", |
|
"petal_width", |
|
"class" |
|
] |
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/iris/raw/main/iris.data" |
|
} |
|
features_types_per_config = { |
|
"iris": { |
|
"sepal_length": datasets.Value("float64"), |
|
"sepal_width": datasets.Value("float64"), |
|
"petal_length": datasets.Value("float64"), |
|
"petal_width": datasets.Value("float64"), |
|
"class": datasets.ClassLabel(num_classes=3, names=("setosa", "versicolor", "virginica")) |
|
}, |
|
"setosa": { |
|
"sepal_length": datasets.Value("float64"), |
|
"sepal_width": datasets.Value("float64"), |
|
"petal_length": datasets.Value("float64"), |
|
"petal_width": datasets.Value("float64"), |
|
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")) |
|
}, |
|
"versicolor": { |
|
"sepal_length": datasets.Value("float64"), |
|
"sepal_width": datasets.Value("float64"), |
|
"petal_length": datasets.Value("float64"), |
|
"petal_width": datasets.Value("float64"), |
|
"class": datasets.ClassLabel(num_classes=2, names=("no", "yes")) |
|
}, |
|
"virginica": { |
|
"sepal_length": datasets.Value("float64"), |
|
"sepal_width": datasets.Value("float64"), |
|
"petal_length": datasets.Value("float64"), |
|
"petal_width": datasets.Value("float64"), |
|
"class": 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 IrisConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(IrisConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Iris(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "iris" |
|
BUILDER_CONFIGS = [ |
|
IrisConfig(name="iris", description="Iris dataset."), |
|
IrisConfig(name="setosa", description="Binary classification of setosa."), |
|
IrisConfig(name="versicolor", description="Binary classification of versicolor."), |
|
IrisConfig(name="virginica", description="Binary classification of virginica.") |
|
] |
|
|
|
|
|
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) -> pandas.DataFrame: |
|
data.columns = _BASE_FEATURE_NAMES |
|
data.loc[:, "class"] = data["class"].apply(lambda x: { |
|
"Iris-setosa": 0, |
|
"Iris-versicolor": 1, |
|
"Iris-virginica": 2 |
|
}[x]) |
|
|
|
|
|
if self.config.name == "setosa": |
|
data.loc[:, "class"] = data["class"].apply(lambda x: 1 if x == 0 else 0) |
|
elif self.config.name == "versicolor": |
|
data.loc[:, "class"] = data["class"].apply(lambda x: 1 if x == 1 else 0) |
|
if self.config.name == "virginica": |
|
data.loc[:, "class"] = data["class"].apply(lambda x: 1 if x == 2 else 0) |
|
|
|
return data |
|
|