mstz commited on
Commit
91203a1
·
1 Parent(s): d687f28

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +25 -5
  2. diamonds.py +30 -15
README.md CHANGED
@@ -2,16 +2,36 @@
2
  language:
3
  - en
4
  tags:
5
- - diamonds
6
  - tabular_classification
7
- - binary_classification
8
- pretty_name: Diamonds
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.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  language:
3
  - en
4
  tags:
5
+ - student performance
6
  - tabular_classification
7
+ - multiclass_classification
8
+ pretty_name: Diamond
9
  size_categories:
10
+ - 1k<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
+ - encoding
15
  - cut
16
  ---
17
  # Diamonds
18
+ The [Diamonds dataset](https://www.kaggle.com/datasets/ulrikthygepedersen/diamonds) from Kaggle.
19
+ Dataset collecting diamonds properties.
20
+
21
+ # Configurations and tasks
22
+ - `encoding` Encoding of non-numerical variables.
23
+ - `cut` Multiclass classification task, predict the cut quality of the diamond.
24
+
25
+ # Features
26
+ |**Feature** |**Description**|
27
+ |-----------------------------------|---------------|
28
+ |`carat` | `float32` |
29
+ |`color` | `string` |
30
+ |`clarity` | `float32` |
31
+ |`depth` | `float32` |
32
+ |`table` | `float32` |
33
+ |`price` | `float32` |
34
+ |`observation_point_on_axis_x` | `float32` |
35
+ |`observation_point_on_axis_y` | `float32` |
36
+ |`observation_point_on_axis_z` | `float32` |
37
+ |`cut` | `int8` |
diamonds.py CHANGED
@@ -79,8 +79,10 @@ 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
 
@@ -101,8 +103,14 @@ class Diamond(datasets.GeneratorBasedBuilder):
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)
@@ -110,25 +118,32 @@ class Diamond(datasets.GeneratorBasedBuilder):
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
- data.loc[:, "clarity"] = data.clarity.apply(lambda x: x.replace("b", "").replace("'", ""))
117
- encode_clarity = partial(self.encode, "clarity")
118
- data.loc[:, "clarity"] = data.clarity.apply(encode_clarity)
119
-
120
- encode_cut = partial(self.encode, "cut")
121
- data.loc[:, "cut"] = data.cut.apply(lambda x: x.replace("b", "").replace("'", ""))
122
- data.loc[:, "cut"] = data.cut.apply(encode_cut)
123
 
124
  data.columns = _BASE_FEATURE_NAMES
125
  data = data.drop_duplicates(subset=["carat", "color", "clarity", "depth", "table",
126
  "price", "cut"])
127
 
128
- if config == "cut":
129
- return data[list(features_types_per_config["cut"].keys())]
130
- else:
131
- raise ValueError(f"Unknown config: {config}")
132
 
133
  def encode(self, feature, value):
134
- return _ENCODING_DICS[feature][value]
 
 
 
 
 
 
 
 
 
 
 
 
79
  # dataset versions
80
  DEFAULT_CONFIG = "cut"
81
  BUILDER_CONFIGS = [
82
+ DiamondConfig(name="encoding",
83
+ description="Encoding dictionaries for discrete features."),
84
  DiamondConfig(name="cut",
85
+ description="5-ary classification, predict the cut quality of the diamond."),
86
  ]
87
 
88
 
 
103
  ]
104
 
105
  def _generate_examples(self, filepath: str):
106
+ if self.config.name not in features_types_per_config:
107
+ raise ValueError(f"Unknown config: {self.config.name}")
108
+
109
+ if self.config.name == "encoding":
110
+ data = self.encoding_dics()
111
+ else:
112
+ data = pandas.read_csv(filepath)
113
+ data = self.preprocess(data, config=self.config.name)
114
 
115
  for row_id, row in data.iterrows():
116
  data_row = dict(row)
 
118
  yield row_id, data_row
119
 
120
  def preprocess(self, data: pandas.DataFrame, config: str = "cut") -> pandas.DataFrame:
121
+ data.loc[:, "clarity"] = data.clarity.apply(lambda x: x.replace("b", "").replace("'", ""))
122
+ data.loc[:, "cut"] = data.cut.apply(lambda x: x.replace("b", "").replace("'", ""))
123
  data.loc[:, "color"] = data.color.astype(str)
124
  data.loc[:, "color"] = data.color.apply(lambda x: x[2])
125
 
126
+ for feature in _ENCODING_DICS:
127
+ encoding_function = partial(self.encode, feature)
128
+ data.loc[:, feature] = data[feature].apply(encoding_function)
 
 
 
 
129
 
130
  data.columns = _BASE_FEATURE_NAMES
131
  data = data.drop_duplicates(subset=["carat", "color", "clarity", "depth", "table",
132
  "price", "cut"])
133
 
134
+
135
+ return data[list(features_types_per_config["cut"].keys())]
 
 
136
 
137
  def encode(self, feature, value):
138
+ if feature in _ENCODING_DICS:
139
+ return _ENCODING_DICS[feature][value]
140
+ raise ValueError(f"Unknown feature: {feature}")
141
+
142
+ def encoding_dics(self):
143
+ data = [pandas.DataFrame([(feature, original, encoded) for original, encoded in d.items()])
144
+ for feature, d in _ENCODING_DICS.items()]
145
+ data = pandas.concat(data, axis="rows").reset_index()
146
+ data.drop("index", axis="columns", inplace=True)
147
+ data.columns = ["feature", "original_value", "encoded_value"]
148
+
149
+ return data