"""Diamond Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _BASE_FEATURE_NAMES = [ "carat", "cut", "color", "clarity", "depth", "table", "price", "observation_point_on_axis_x", "observation_point_on_axis_y", "observation_point_on_axis_z" ] _ENCODING_DICS = { "cut": { "Fair": 0, "Good": 1, "Very Good": 2, "Premium": 3, "Ideal": 4 }, "clarity": { "IF": 0, "VVS1": 1, "VVS2": 2, "VS1": 3, "VS2": 4, "SI1": 5, "SI2": 6, "I1": 7 } } DESCRIPTION = "Diamond quality dataset." _HOMEPAGE = "https://www.kaggle.com/datasets/ulrikthygepedersen/diamonds" _URLS = ("https://www.kaggle.com/datasets/ulrikthygepedersen/diamonds") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/diamonds/raw/main/diamonds.csv", } features_types_per_config = { "cut": { "carat": datasets.Value("float32"), "color": datasets.Value("string"), "clarity": datasets.Value("float32"), "depth": datasets.Value("float32"), "table": datasets.Value("float32"), "price": datasets.Value("float32"), "observation_point_on_axis_x": datasets.Value("float32"), "observation_point_on_axis_y": datasets.Value("float32"), "observation_point_on_axis_z": datasets.Value("float32"), "cut": datasets.ClassLabel(num_classes=5, names=("Fair", "Good", "Very Good", "Premium", "Ideal")) } } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class DiamondConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(DiamondConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Diamond(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "cut" BUILDER_CONFIGS = [ DiamondConfig(name="cut", description="5-ary classification, predict the cut quality of the diamond."), ] def _info(self): if self.config.name not in features_per_config: raise ValueError(f"Unknown configuration: {self.config.name}") 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 = "cut") -> pandas.DataFrame: data.loc[:, "color"] = data.color.astype(str) data.loc[:, "color"] = data.color.apply(lambda x: x[2]) encode_clarity = partial(encode, "clarity") data.loc[:, "clarity"] = data.clarity.apply(encode_clarity) data.columns = _BASE_FEATURE_NAMES data = data.drop_duplicates(subset=["carat", "color", "clarity", "depth", "table", "price", "cut"]) if config == "cut": return data[list(features_types_per_config["cut"].keys())] else: raise ValueError(f"Unknown config: {config}") def encode(self, feature, value): return _ENCODING_DICS[feature][value]