mstz commited on
Commit
c540bf6
1 Parent(s): 0ee0455

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +17 -0
  2. diamonds.csv +0 -0
  3. diamonds.py +129 -0
README.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ tags:
5
+ - diamonds
6
+ - tabular_classification
7
+ - binary_classification
8
+ pretty_name: Compas
9
+ size_categories:
10
+ - 10K<n<100K
11
+ task_categories: # Full list at https://github.com/huggingface/hub-docs/blob/main/js/src/lib/interfaces/Types.ts
12
+ - tabular-classification
13
+ configs:
14
+ - cut
15
+ ---
16
+ # Diamonds
17
+ The [Diamonds dataset](https://www.kaggle.com/datasets/ulrikthygepedersen/diamonds) is cool.
diamonds.csv ADDED
The diff for this file is too large to render. See raw diff
 
diamonds.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Diamond Dataset"""
2
+
3
+ from typing import List
4
+ from functools import partial
5
+
6
+ import datasets
7
+
8
+ import pandas
9
+
10
+
11
+ VERSION = datasets.Version("1.0.0")
12
+ _BASE_FEATURE_NAMES = [
13
+ "carat",
14
+ "cut",
15
+ "color",
16
+ "clarity",
17
+ "depth",
18
+ "table",
19
+ "price",
20
+ "observation_point_on_axis_x",
21
+ "observation_point_on_axis_y",
22
+ "observation_point_on_axis_z"
23
+ ]
24
+
25
+ _ENCODING_DICS = {
26
+ "cut": {
27
+ "Fair": 0,
28
+ "Good": 1,
29
+ "Very Good": 2,
30
+ "Premium": 3,
31
+ "Ideal": 4
32
+ },
33
+ "clarity": {
34
+ "IF": 0,
35
+ "VVS1": 1,
36
+ "VVS2": 2,
37
+ "VS1": 3,
38
+ "VS2": 4,
39
+ "SI1": 5,
40
+ "SI2": 6,
41
+ "I1": 7
42
+ }
43
+ }
44
+
45
+ DESCRIPTION = "Diamond quality dataset."
46
+ _HOMEPAGE = "https://www.kaggle.com/datasets/ulrikthygepedersen/diamonds"
47
+ _URLS = ("https://www.kaggle.com/datasets/ulrikthygepedersen/diamonds")
48
+ _CITATION = """"""
49
+
50
+ # Dataset info
51
+ urls_per_split = {
52
+ "train": "https://huggingface.co/datasets/mstz/diamonds/raw/main/diamonds.csv",
53
+ }
54
+ features_types_per_config = {
55
+ "cut": {
56
+ "carat": datasets.Value("float32"),
57
+ "color": datasets.Value("string"),
58
+ "clarity": datasets.Value("float32"),
59
+ "depth": datasets.Value("float32"),
60
+ "table": datasets.Value("float32"),
61
+ "price": datasets.Value("float32"),
62
+ "observation_point_on_axis_x": datasets.Value("float32"),
63
+ "observation_point_on_axis_y": datasets.Value("float32"),
64
+ "observation_point_on_axis_z": datasets.Value("float32"),
65
+ "cut": datasets.ClassLabel(num_classes=5, names=("Fair", "Good", "Very Good", "Premium", "Ideal"))
66
+ }
67
+
68
+ }
69
+ features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
70
+
71
+
72
+ class DiamondConfig(datasets.BuilderConfig):
73
+ def __init__(self, **kwargs):
74
+ super(DiamondConfig, self).__init__(version=VERSION, **kwargs)
75
+ self.features = features_per_config[kwargs["name"]]
76
+
77
+
78
+ class Diamond(datasets.GeneratorBasedBuilder):
79
+ # dataset versions
80
+ DEFAULT_CONFIG = "cut"
81
+ BUILDER_CONFIGS = [
82
+ DiamondConfig(name="cut",
83
+ description="5-ary classification, predict the cut quality of the diamond."),
84
+ ]
85
+
86
+
87
+ def _info(self):
88
+ if self.config.name not in features_per_config:
89
+ raise ValueError(f"Unknown configuration: {self.config.name}")
90
+
91
+ info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
92
+ features=features_per_config[self.config.name])
93
+
94
+ return info
95
+
96
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
97
+ downloads = dl_manager.download_and_extract(urls_per_split)
98
+
99
+ return [
100
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
101
+ ]
102
+
103
+ def _generate_examples(self, filepath: str):
104
+ data = pandas.read_csv(filepath)
105
+ data = self.preprocess(data, config=self.config.name)
106
+
107
+ for row_id, row in data.iterrows():
108
+ data_row = dict(row)
109
+
110
+ yield row_id, data_row
111
+
112
+ def preprocess(self, data: pandas.DataFrame, config: str = "cut") -> pandas.DataFrame:
113
+ data.loc[:, "color"] = data.color.astype(str)
114
+ data.loc[:, "color"] = data.color.apply(lambda x: x[2])
115
+
116
+ encode_clarity = partial(encode, "clarity")
117
+ data.loc[:, "clarity"] = data.clarity.apply(encode_clarity)
118
+
119
+ data.columns = _BASE_FEATURE_NAMES
120
+ data = data.drop_duplicates(subset=["carat", "color", "clarity", "depth", "table",
121
+ "price", "cut"])
122
+
123
+ if config == "cut":
124
+ return data[list(features_types_per_config["cut"].keys())]
125
+ else:
126
+ raise ValueError(f"Unknown config: {config}")
127
+
128
+ def encode(self, feature, value):
129
+ return _ENCODING_DICS[feature][value]