mstz commited on
Commit
b42d454
·
1 Parent(s): 650b68f

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +26 -1
  2. adult.py +59 -72
README.md CHANGED
@@ -11,9 +11,34 @@ size_categories:
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
  - income
15
  - income-no race
16
  - race
17
  ---
18
  # Adult
19
- The [Adult dataset](https://archive.ics.uci.edu/ml/datasets/Adult) is cool.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  - income
16
  - income-no race
17
  - race
18
  ---
19
  # Adult
20
+ The [Adult dataset](https://archive.ics.uci.edu/ml/datasets/Adult) from the [UCI ML repository](https://archive.ics.uci.edu/ml/datasets).
21
+ Census dataset including income threshold.
22
+
23
+ # Configurations and tasks
24
+ The dataset has four configurations:
25
+ - `encoding`, which holds the encoding dictionaries mapping binary and ordinal features to their value;
26
+ - `income`, for binary classification of the individual's income;
27
+ - `income-no race`, as `income`, but the `race` feature is removed;
28
+ - `race`, multiclass classification to predict the `race` of the individual.
29
+
30
+ # Features
31
+ - `age` Age of the person;
32
+ - `capital_gain` Capital gained by the person;
33
+ - `capital_loss` Capital lost by the person;
34
+ - `education` Education level: the higher, the more educated the person;
35
+ - `final_weight`
36
+ - `hours_per_week` Hours worked per week;
37
+ - `marital_status` Marital status of the person;
38
+ - `native_country` Native country of the person;
39
+ - `occupation` Job of the person;
40
+ - `race` Race of the person;
41
+ - `relationship`
42
+ - `sex` Sex of the person;
43
+ - `workclass` Type of job of the person;
44
+ - `over_threshold` `1` for income `>= 50k$`, `0` otherwise.
adult.py CHANGED
@@ -1,6 +1,7 @@
1
  """Adult: A Census Dataset"""
2
 
3
  from typing import List
 
4
 
5
  import datasets
6
 
@@ -39,8 +40,22 @@ _BASE_FEATURE_NAMES = [
39
  "relationship",
40
  "sex",
41
  "workclass",
42
- "threshold",
43
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  DESCRIPTION = "Adult dataset from the UCI ML repository."
45
  _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Adult"
46
  _URLS = ("https://huggingface.co/datasets/mstz/adult/raw/adult.csv")
@@ -82,7 +97,7 @@ features_types_per_config = {
82
  "relationship": datasets.Value("string"),
83
  "sex": datasets.Value("int8"),
84
  "workclass": datasets.Value("string"),
85
- "threshold": datasets.ClassLabel(num_classes=2, names=("no", "yes"))},
86
  "income-no race": {"age": datasets.Value("int64"),
87
  "capital_gain": datasets.Value("float64"),
88
  "capital_loss": datasets.Value("float64"),
@@ -95,7 +110,7 @@ features_types_per_config = {
95
  "relationship": datasets.Value("string"),
96
  "sex": datasets.Value("int8"),
97
  "workclass": datasets.Value("string"),
98
- "threshold": datasets.ClassLabel(num_classes=2, names=("no", "yes"))},
99
  "race": {"age": datasets.Value("int64"),
100
  "capital_gain": datasets.Value("float64"),
101
  "capital_loss": datasets.Value("float64"),
@@ -124,6 +139,8 @@ class Adult(datasets.GeneratorBasedBuilder):
124
  # dataset versions
125
  DEFAULT_CONFIG = "income"
126
  BUILDER_CONFIGS = [
 
 
127
  AdultConfig(name="income",
128
  description="Adult for income threshold binary classification."),
129
  AdultConfig(name="income-no race",
@@ -151,91 +168,61 @@ class Adult(datasets.GeneratorBasedBuilder):
151
  ]
152
 
153
  def _generate_examples(self, filepath: str):
154
- data = pandas.read_csv(filepath)
155
- data = self.preprocess(data, config=self.config.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
- for row_id, row in data.iterrows():
158
- data_row = dict(row)
159
 
160
- yield row_id, data_row
161
 
162
  def preprocess(self, data: pandas.DataFrame, config: str = "income") -> pandas.DataFrame:
163
  data.drop("education", axis="columns", inplace=True)
 
164
  data = data[["age", "capital_gain", "capital_loss", "education-num", "final_weight",
165
  "hours_per_week", "marital_status", "native_country", "occupation",
166
- "race", "relationship", "sex", "workclass", "threshold"]]
167
  data.columns = _BASE_FEATURE_NAMES
168
 
169
- # binarize features
170
- data.loc[:, "sex"] = data.sex.apply(self.encode_sex)
 
171
 
172
  if config == "income":
173
- return self.income_preprocessing(data)
174
  elif config == "income-no race":
175
- return self.income_norace_preprocessing(data)
176
  elif config =="race":
 
 
 
177
  return self.race_preprocessing(data)
178
  else:
179
  raise ValueError(f"Unknown config: {config}")
180
 
181
- def income_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
182
- data = data[list(features_types_per_config["income"].keys())]
183
-
184
- return data
185
-
186
- def income_norace_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
187
- data = data[list(features_types_per_config["income-no race"].keys())]
188
-
189
- return data
190
 
191
- def race_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
192
- features = list(features_types_per_config["race"].keys())
193
- features[features.index("over_threshold")] = "threshold"
194
- data.loc[:, "race"] = data.race.apply(self.encode_race)
195
- data = data[features]
196
- data.columns = ["age", "capital_gain", "capital_loss", "education", "final_weight",
197
- "hours_per_week", "marital_status", "native_country", "occupation", "relationship", "sex", "workclass", "over_threshold", "race"]
198
-
199
- return data
200
-
201
  def encode_race(self, race):
202
- return self.race_encoding_dic()[race]
203
-
204
- def decode_race(self, code):
205
- return self.race_decoding_dic()[code]
206
-
207
- def race_decoding_dic(self):
208
- return {
209
- 0: "White",
210
- 1: "Black",
211
- 2: "Asian-Pac-Islander",
212
- 3: "Amer-Indian-Eskimo",
213
- 4: "Other",
214
- }
215
-
216
- def race_encoding_dic(self):
217
- return {
218
- "White": 0,
219
- "Black": 1,
220
- "Asian-Pac-Islander": 2,
221
- "Amer-Indian-Eskimo": 3,
222
- "Other": 4,
223
- }
224
-
225
- def encode_sex(self, sex):
226
- return self.sex_encoding_dic()[sex]
227
-
228
- def decode_sex(self, code):
229
- return self.sex_decoding_dic()[code]
230
-
231
- def sex_encoding_dic(self):
232
- return {
233
- "Male": 0,
234
- "Female": 1
235
- }
236
-
237
- def sex_decoding_dic(self):
238
- return {
239
- 0: "Male",
240
- 1: "Female"
241
- }
 
1
  """Adult: A Census Dataset"""
2
 
3
  from typing import List
4
+ from functools import partial
5
 
6
  import datasets
7
 
 
40
  "relationship",
41
  "sex",
42
  "workclass",
43
+ "over_threshold",
44
  ]
45
+ _ENCODINGS = {
46
+ "sex": {
47
+ "Male": 0,
48
+ "Female": 1
49
+ }
50
+ }
51
+ _RACE_ENCODING = {
52
+ "White": 0,
53
+ "Black": 1,
54
+ "Asian-Pac-Islander": 2,
55
+ "Amer-Indian-Eskimo": 3,
56
+ "Other": 4,
57
+ }
58
+
59
  DESCRIPTION = "Adult dataset from the UCI ML repository."
60
  _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Adult"
61
  _URLS = ("https://huggingface.co/datasets/mstz/adult/raw/adult.csv")
 
97
  "relationship": datasets.Value("string"),
98
  "sex": datasets.Value("int8"),
99
  "workclass": datasets.Value("string"),
100
+ "over_threshold": datasets.ClassLabel(num_classes=2, names=("no", "yes"))},
101
  "income-no race": {"age": datasets.Value("int64"),
102
  "capital_gain": datasets.Value("float64"),
103
  "capital_loss": datasets.Value("float64"),
 
110
  "relationship": datasets.Value("string"),
111
  "sex": datasets.Value("int8"),
112
  "workclass": datasets.Value("string"),
113
+ "over_threshold": datasets.ClassLabel(num_classes=2, names=("no", "yes"))},
114
  "race": {"age": datasets.Value("int64"),
115
  "capital_gain": datasets.Value("float64"),
116
  "capital_loss": datasets.Value("float64"),
 
139
  # dataset versions
140
  DEFAULT_CONFIG = "income"
141
  BUILDER_CONFIGS = [
142
+ AdultConfig(name="encoding",
143
+ description="Encoding dictionaries."),
144
  AdultConfig(name="income",
145
  description="Adult for income threshold binary classification."),
146
  AdultConfig(name="income-no race",
 
168
  ]
169
 
170
  def _generate_examples(self, filepath: str):
171
+ if self.config.name == "encoding":
172
+ data = self.encodings()
173
+ elif self.config.name in ["income", "income-no race", "race"]:
174
+ data = pandas.read_csv(filepath)
175
+ data = self.preprocess(data, config=self.config.name)
176
+
177
+ for row_id, row in data.iterrows():
178
+ data_row = dict(row)
179
+
180
+ yield row_id, data_row
181
+ else:
182
+ raise ValueError(f"Unknown config: {self.config.name}")
183
+
184
+ def encodings(self):
185
+ data = [pandas.DataFrame([(feature, original_value, encoded_value)
186
+ for original_value, encoded_value in d.items()],
187
+ columns=["feature", "original_value", "encoded_value"])
188
+ for feature in _ENCODINGS]
189
+ data.append(pandas.DataFrame([("race", original_value, encoded_value)
190
+ for original_value, encoded_value in _RACE_ENCODING.items()],
191
+ columns=["feature", "original_value", "encoded_value"]))
192
+ data = pandas.concat(data, axis="rows").reset_index()
193
+ data.drop("index", axis="columns", inplace=True)
194
 
195
+ return data
 
196
 
 
197
 
198
  def preprocess(self, data: pandas.DataFrame, config: str = "income") -> pandas.DataFrame:
199
  data.drop("education", axis="columns", inplace=True)
200
+
201
  data = data[["age", "capital_gain", "capital_loss", "education-num", "final_weight",
202
  "hours_per_week", "marital_status", "native_country", "occupation",
203
+ "race", "relationship", "sex", "workclass", "over_threshold"]]
204
  data.columns = _BASE_FEATURE_NAMES
205
 
206
+ for feature in _ENCODINGS:
207
+ encoding_function = partial(self.encode, feature)
208
+ data.loc[:, feature] = data[feature].apply(encoding_function)
209
 
210
  if config == "income":
211
+ return data[list(features_types_per_config["income"].keys())]
212
  elif config == "income-no race":
213
+ return data[list(features_types_per_config["income-no race"].keys())]
214
  elif config =="race":
215
+ data.loc[:, "race"] = data.race.apply(self.encode_race)
216
+ data = data[list(features_types_per_config["race"].keys())]
217
+
218
  return self.race_preprocessing(data)
219
  else:
220
  raise ValueError(f"Unknown config: {config}")
221
 
222
+ def encode(self, feature, value):
223
+ if feature in _ENCODING_DICS:
224
+ return _ENCODING_DICS[feature][value]
225
+ raise ValueError(f"Unknown feature: {feature}")
 
 
 
 
 
226
 
 
 
 
 
 
 
 
 
 
 
227
  def encode_race(self, race):
228
+ return _RACE_ENCODING[race]