Datasets:
Update diamonds.py
Browse files- diamonds.py +26 -13
diamonds.py
CHANGED
|
@@ -69,8 +69,20 @@ features_types_per_config = {
|
|
| 69 |
"observation_point_on_axis_y": datasets.Value("float32"),
|
| 70 |
"observation_point_on_axis_z": datasets.Value("float32"),
|
| 71 |
"cut": datasets.ClassLabel(num_classes=5, names=("Fair", "Good", "Very Good", "Premium", "Ideal"))
|
| 72 |
-
}
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
|
| 76 |
|
|
@@ -85,10 +97,9 @@ class Diamond(datasets.GeneratorBasedBuilder):
|
|
| 85 |
# dataset versions
|
| 86 |
DEFAULT_CONFIG = "cut"
|
| 87 |
BUILDER_CONFIGS = [
|
| 88 |
-
DiamondConfig(name="encoding",
|
| 89 |
-
|
| 90 |
-
DiamondConfig(name="
|
| 91 |
-
description="5-ary classification, predict the cut quality of the diamond."),
|
| 92 |
]
|
| 93 |
|
| 94 |
|
|
@@ -118,18 +129,20 @@ class Diamond(datasets.GeneratorBasedBuilder):
|
|
| 118 |
yield row_id, data_row
|
| 119 |
|
| 120 |
def preprocess(self, data: pandas.DataFrame, config: str = "cut") -> pandas.DataFrame:
|
| 121 |
-
data
|
| 122 |
-
data
|
| 123 |
-
data
|
| 124 |
-
data
|
| 125 |
|
| 126 |
for feature in _ENCODING_DICS:
|
| 127 |
encoding_function = partial(self.encode, feature)
|
| 128 |
-
data
|
| 129 |
|
| 130 |
data.columns = _BASE_FEATURE_NAMES
|
| 131 |
-
data = data.drop_duplicates(subset=["carat", "color", "clarity", "depth", "table",
|
| 132 |
-
|
|
|
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
return data[list(features_types_per_config["cut"].keys())]
|
|
|
|
| 69 |
"observation_point_on_axis_y": datasets.Value("float32"),
|
| 70 |
"observation_point_on_axis_z": datasets.Value("float32"),
|
| 71 |
"cut": datasets.ClassLabel(num_classes=5, names=("Fair", "Good", "Very Good", "Premium", "Ideal"))
|
| 72 |
+
},
|
| 73 |
+
|
| 74 |
+
"cut_binary": {
|
| 75 |
+
"carat": datasets.Value("float32"),
|
| 76 |
+
"color": datasets.Value("string"),
|
| 77 |
+
"clarity": datasets.Value("float32"),
|
| 78 |
+
"depth": datasets.Value("float32"),
|
| 79 |
+
"table": datasets.Value("float32"),
|
| 80 |
+
"price": datasets.Value("float32"),
|
| 81 |
+
"observation_point_on_axis_x": datasets.Value("float32"),
|
| 82 |
+
"observation_point_on_axis_y": datasets.Value("float32"),
|
| 83 |
+
"observation_point_on_axis_z": datasets.Value("float32"),
|
| 84 |
+
"cut": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
|
| 85 |
+
},
|
| 86 |
}
|
| 87 |
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
|
| 88 |
|
|
|
|
| 97 |
# dataset versions
|
| 98 |
DEFAULT_CONFIG = "cut"
|
| 99 |
BUILDER_CONFIGS = [
|
| 100 |
+
DiamondConfig(name="encoding", description="Encoding dictionaries for discrete features."),
|
| 101 |
+
DiamondConfig(name="cut", description="5-ary classification, predict the cut quality of the diamond."),
|
| 102 |
+
DiamondConfig(name="cut_binary", description="Binary classification."),
|
|
|
|
| 103 |
]
|
| 104 |
|
| 105 |
|
|
|
|
| 129 |
yield row_id, data_row
|
| 130 |
|
| 131 |
def preprocess(self, data: pandas.DataFrame, config: str = "cut") -> pandas.DataFrame:
|
| 132 |
+
data["clarity"] = data.clarity.apply(lambda x: x.replace("b", "").replace("'", ""))
|
| 133 |
+
data["cut"] = data.cut.apply(lambda x: x.replace("b", "").replace("'", ""))
|
| 134 |
+
data["color"] = data.color.astype(str)
|
| 135 |
+
data["color"] = data.color.apply(lambda x: x[2]).replace("\"", "")
|
| 136 |
|
| 137 |
for feature in _ENCODING_DICS:
|
| 138 |
encoding_function = partial(self.encode, feature)
|
| 139 |
+
data[feature] = data[feature].apply(encoding_function)
|
| 140 |
|
| 141 |
data.columns = _BASE_FEATURE_NAMES
|
| 142 |
+
data = data.drop_duplicates(subset=["carat", "color", "clarity", "depth", "table", "price", "cut"])
|
| 143 |
+
|
| 144 |
+
if self.config.name == "cut_binary":
|
| 145 |
+
data.cut = data.cut.apply(lambda x: 0 if x <= 2 else 1)
|
| 146 |
|
| 147 |
|
| 148 |
return data[list(features_types_per_config["cut"].keys())]
|