|
"""Mushroom""" |
|
|
|
from typing import List |
|
from functools import partial |
|
|
|
import datasets |
|
|
|
import pandas |
|
import numpy |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
_ORIGINAL_FEATURE_NAMES = [ |
|
"is_poisonous", |
|
"cap_shape", |
|
"cap_surface", |
|
"cap_color", |
|
"has_bruises", |
|
"odor", |
|
"gill_attachment", |
|
"gill_spacing", |
|
"gill_size", |
|
"gill_color", |
|
"stalk_shape", |
|
"stalk_root", |
|
"stalk_surface_above_ring", |
|
"stalk_surface_belows_ring", |
|
"stalk_color_above_ring", |
|
"stalk_color_below_ring", |
|
"veil_type", |
|
"veil_color", |
|
"number_of_rings", |
|
"ring_type", |
|
"spore_print_color", |
|
"population", |
|
"habitat" |
|
] |
|
_BASE_FEATURE_NAMES = [ |
|
"cap_shape", |
|
"cap_surface", |
|
"cap_color", |
|
"has_bruises", |
|
"odor", |
|
"gill_attachment", |
|
"gill_spacing", |
|
"gill_size", |
|
"gill_color", |
|
"stalk_shape", |
|
"stalk_surface_above_ring", |
|
"stalk_surface_belows_ring", |
|
"stalk_color_above_ring", |
|
"stalk_color_below_ring", |
|
"veil_type", |
|
"veil_color", |
|
"number_of_rings", |
|
"ring_type", |
|
"spore_print_color", |
|
"population", |
|
"habitat" |
|
] |
|
|
|
_ENCODING_DICS = { |
|
"is_poisonous": { |
|
"p": 1, |
|
"e": 0 |
|
}, |
|
"cap_shape": { |
|
"b": "bell", |
|
"c": "conical", |
|
"x": "convex", |
|
"f": "flat", |
|
"k": "knobbed", |
|
"s": "sunken", |
|
}, |
|
"cap_surface": { |
|
"f": "fibrous", |
|
"g": "grooves", |
|
"y": "scaly", |
|
"s": "smooth" |
|
}, |
|
"cap_color": { |
|
"n": "brown", |
|
"b": "buff", |
|
"c": "cinnamon", |
|
"g": "gray", |
|
"r": "green", |
|
"p": "pink", |
|
"u": "purple", |
|
"e": "red", |
|
"w": "white", |
|
"y": "yellow" |
|
}, |
|
"has_bruises": { |
|
"f": False, |
|
"t": True |
|
}, |
|
"odor": { |
|
"a": "almond", |
|
"l": "anise", |
|
"c": "creosote", |
|
"y": "fishy", |
|
"f": "foul", |
|
"m": "musty", |
|
"n": "none", |
|
"p": "pungent", |
|
"s": "spicy" |
|
}, |
|
"gill_attachment": { |
|
"a": "attached", |
|
"d": "descending", |
|
"f": "free", |
|
"n": "notched", |
|
}, |
|
"gill_spacing": { |
|
"c": "close", |
|
"w": "crowded", |
|
"d": "distant", |
|
}, |
|
"gill_size": { |
|
"b": "broad", |
|
"n": "narrow" |
|
}, |
|
"gill_color": { |
|
"k": "black", |
|
"n": "brown", |
|
"b": "buff", |
|
"h": "chocolate", |
|
"g": "gray", |
|
"r": "green", |
|
"o": "orange", |
|
"p": "pink", |
|
"u": "purple", |
|
"e": "red", |
|
"w": "white", |
|
"y": "yellow", |
|
}, |
|
"stalk_shape": { |
|
"e": "enlarging", |
|
"t": "tapering", |
|
}, |
|
"stalk_surface_above_ring": { |
|
"f": "fibrous", |
|
"y": "scaly", |
|
"k": "silky", |
|
"s": "smooth", |
|
}, |
|
"stalk_surface_above_ring": { |
|
"f": "fibrous", |
|
"y": "scaly", |
|
"k": "silky", |
|
"s": "smooth", |
|
}, |
|
"stalk_color_above_ring": { |
|
"n": "brown", |
|
"b": "buff", |
|
"c": "cinnamon", |
|
"g": "gray", |
|
"o": "orange", |
|
"p": "pink", |
|
"e": "red", |
|
"w": "white", |
|
"y": "yellow", |
|
}, |
|
"stalk_color_below_ring": { |
|
"n": "brown", |
|
"b": "buff", |
|
"c": "cinnamon", |
|
"g": "gray", |
|
"o": "orange", |
|
"p": "pink", |
|
"e": "red", |
|
"w": "white", |
|
"y": "yellow", |
|
}, |
|
"veil_type": { |
|
"p": "partial", |
|
"u": "universal", |
|
}, |
|
"veil_color": { |
|
"n": "brown", |
|
"o": "orange", |
|
"w": "white", |
|
"y": "yellow", |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
"ring_type": { |
|
"c": "cobwebby", |
|
"e": "evanescent", |
|
"f": "flaring", |
|
"l": "large", |
|
"n": "none", |
|
"p": "pendant", |
|
"s": "sheathing", |
|
"z": "zone", |
|
}, |
|
"spore_print_color": { |
|
"k": "black", |
|
"n": "brown", |
|
"b": "buff", |
|
"h": "chocolate", |
|
"r": "green", |
|
"o": "orange", |
|
"u": "purple", |
|
"w": "white", |
|
"y": "yellow", |
|
}, |
|
"population": { |
|
"a": "abundant", |
|
"c": "clustered", |
|
"n": "numerous", |
|
"s": "scattered", |
|
"v": "several", |
|
"y": "solitary", |
|
}, |
|
"habitat": { |
|
"g": "grasses", |
|
"l": "leaves", |
|
"m": "meadows", |
|
"p": "paths", |
|
"u": "urban", |
|
"w": "waste", |
|
"d": "woods", |
|
} |
|
} |
|
|
|
DESCRIPTION = "Mushroom dataset from the UCI ML repository." |
|
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Mushroom" |
|
_URLS = ("https://huggingface.co/datasets/mstz/mushroom/raw/mushroom.csv") |
|
_CITATION = """ |
|
@misc{misc_mushroom_73, |
|
title = {{Mushroom}}, |
|
year = {1987}, |
|
howpublished = {UCI Machine Learning Repository}, |
|
note = {{DOI}: \\url{10.24432/C5959T}} |
|
}""" |
|
|
|
|
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/mushroom/raw/main/agaricus-lepiota.data" |
|
} |
|
features_types_per_config = { |
|
"mushroom": { |
|
"cap_shape": datasets.Value("string"), |
|
"cap_surface": datasets.Value("string"), |
|
"cap_color": datasets.Value("string"), |
|
"has_bruises": datasets.Value("bool"), |
|
"odor": datasets.Value("string"), |
|
"gill_attachment": datasets.Value("string"), |
|
"gill_spacing": datasets.Value("string"), |
|
"gill_size": datasets.Value("string"), |
|
"gill_color": datasets.Value("string"), |
|
"stalk_shape": datasets.Value("string"), |
|
|
|
"stalk_surface_above_ring": datasets.Value("string"), |
|
"stalk_surface_belows_ring": datasets.Value("string"), |
|
"stalk_color_above_ring": datasets.Value("string"), |
|
"stalk_color_below_ring": datasets.Value("string"), |
|
"veil_type": datasets.Value("string"), |
|
"veil_color": datasets.Value("string"), |
|
"number_of_rings": datasets.Value("string"), |
|
"ring_type": datasets.Value("string"), |
|
"spore_print_color": datasets.Value("string"), |
|
"population": datasets.Value("string"), |
|
"habitat": datasets.Value("string"), |
|
"is_poisonous": 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 MushroomConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(MushroomConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Mushroom(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "mushroom" |
|
BUILDER_CONFIGS = [ |
|
MushroomConfig(name="mushroom", |
|
description="Mushroom 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, 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.columns = _ORIGINAL_FEATURE_NAMES |
|
if "stalk_root" in data.columns: |
|
data.drop("stalk_root", axis="columns", inplace=True) |
|
data = data[features_types_per_config[config].keys()] |
|
|
|
for feature in _ENCODING_DICS: |
|
encoding_function = partial(self.encode, feature) |
|
data.loc[:, feature] = data[feature].apply(encoding_function) |
|
data = data.infer_objects() |
|
|
|
return data |
|
|
|
def encode(self, feature, value): |
|
if feature in _ENCODING_DICS: |
|
return _ENCODING_DICS[feature][value] |
|
raise ValueError(f"Unknown feature: {feature}") |
|
|
|
def encode_race(self, race): |
|
return _RACE_ENCODING[race] |
|
|