mstz commited on
Commit
b9af923
1 Parent(s): 1e44067

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +30 -1
  2. compas.py +56 -92
README.md CHANGED
@@ -11,10 +11,39 @@ 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
  - two-years-recidividity
15
  - two-years-recidividity-no-race
16
  - priors-prediction
17
  - priors-prediction-no-race
 
18
  ---
19
  # Compas
20
- The [Compas dataset](https://github.com/propublica/compas-analysis) 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
  - two-years-recidividity
16
  - two-years-recidividity-no-race
17
  - priors-prediction
18
  - priors-prediction-no-race
19
+ - race
20
  ---
21
  # Compas
22
+ The [Compas dataset](https://github.com/propublica/compas-analysis) for recidivism prediction.
23
+
24
+ # Configurations and tasks
25
+ - `encoding` Encoding of non-numerical variables.
26
+ - `two-years-recidividity` Binary classification of violent recidivism.
27
+ - `two-years-recidividity-no-race` As above, but the `race` feature is removed.
28
+ - `priors-prediction` Regression on the number of prior offenses of defendant.
29
+ - `priors-prediction-no-race` As above, but the `race` feature is removed.
30
+ - `race` Multiclass classification, predict `race` out of other features.
31
+
32
+ # Features
33
+
34
+ |**Feature** |**Type** |**Description** |
35
+ |---------------------------------------|-----------|---------------------------------------|
36
+ |`sex` |`int64` | |
37
+ |`age` |`int64` | |
38
+ |`race` |`int64` | |
39
+ |`number_of_juvenile_fellonies` |`int64` | |
40
+ |`decile_score` |`int64` |Criminality score |
41
+ |`number_of_juvenile_misdemeanors` |`int64` | |
42
+ |`number_of_other_juvenile_offenses` |`int64` | |
43
+ |`number_of_prior_offenses` |`int64` | |
44
+ |`days_before_screening_arrest` |`int64` | |
45
+ |`is_recidivous` |`int64` | |
46
+ |`days_in_custody` |`int64` |Days spent in custody |
47
+ |`is_violent_recidivous` |`int64` | |
48
+ |`violence_decile_score` |`int64` |Criminality score for violent crimes |
49
+ |`two_years_recidivous` |`int64` | |
compas.py CHANGED
@@ -1,6 +1,7 @@
1
  """Compas Dataset"""
2
 
3
  from typing import List
 
4
  import datetime
5
 
6
  import datasets
@@ -81,6 +82,20 @@ _BASE_FEATURE_NAMES = [
81
  "violence_decile_score",
82
  "two_year_recidivous",
83
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  DESCRIPTION = "COMPAS dataset for recidivism prediction."
86
  _HOMEPAGE = "https://github.com/propublica/compas-analysis"
@@ -163,6 +178,24 @@ features_types_per_config = {
163
  "two_year_recidivous": datasets.Value("int64"),
164
  "number_of_prior_offenses": datasets.Value("int64"),
165
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  }
167
  features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
168
 
@@ -207,8 +240,14 @@ class Compas(datasets.GeneratorBasedBuilder):
207
  ]
208
 
209
  def _generate_examples(self, filepath: str):
210
- data = pandas.read_csv(filepath)
211
- data = self.preprocess(data, config=self.config.name)
 
 
 
 
 
 
212
 
213
  for row_id, row in data.iterrows():
214
  data_row = dict(row)
@@ -287,95 +326,20 @@ class Compas(datasets.GeneratorBasedBuilder):
287
 
288
  data.columns = _BASE_FEATURE_NAMES
289
 
290
- # binarize features
291
- data.loc[:, "sex"] = data.sex.apply(self.encode_sex)
292
-
293
- if config == "encoding":
294
- return self.encoding_dictionaries()
295
- elif config == "two-years-recidividity":
296
- return self.two_years_recidividity_preprocessing(data)
297
- elif config == "two-years-recidividity-no-race":
298
- return self.two_years_recidividity_no_race_preprocessing(data)
299
- elif config == "priors-prediction":
300
- return self.priors_prediction_preprocessing(data)
301
- elif config == "priors-prediction-no-race":
302
- return self.priors_prediction_no_race_preprocessing(data)
303
- else:
304
- raise ValueError(f"Unknown config: {config}")
305
 
306
- def encoding_dictionaries(self):
307
- race_dic, sex_dic = self.race_encoding_dic(), self.sex_encoding_dic()
308
- race_data = [("race", race, code) for race, code in race_dic.items()]
309
- sex_data = [("sex", sex, code) for sex, code in sex_dic.items()]
310
- data = pandas.DataFrame(race_data + sex_data,
311
- columns=["feature", "original_value", "encoded_value"])
312
-
313
  return data
314
-
315
- def two_years_recidividity_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
316
- # categorize features
317
- data.loc[:, "race"] = data.race.apply(self.encode_race)
318
-
319
- return data
320
-
321
- def two_years_recidividity_no_race_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
322
- # categorize features
323
- data.drop("race", axis="columns", inplace=True)
324
-
325
- return data
326
-
327
- def priors_prediction_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
328
- # categorize features
329
- data.loc[:, "race"] = data.race.apply(self.encode_race)
330
-
331
- return data
332
-
333
- def priors_prediction_no_race_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
334
- # categorize features
335
- data.drop("race", axis="columns", inplace=True)
336
-
337
- return data
338
-
339
- def encode_race(self, race):
340
- return self.race_encoding_dic()[race]
341
-
342
- def decode_race(self, code):
343
- return self.race_decoding_dic()[code]
344
-
345
- def race_decoding_dic(self):
346
- return {
347
- 0: "Caucasian",
348
- 1: "African-American",
349
- 2: "Hispanic",
350
- 3: "Asian",
351
- 4: "Other",
352
- 5: "Native American",
353
- }
354
-
355
- def race_encoding_dic(self):
356
- return {
357
- "Caucasian": 0,
358
- "African-American": 1,
359
- "Hispanic": 2,
360
- "Asian": 3,
361
- "Other": 4,
362
- "Native American": 5,
363
- }
364
-
365
- def encode_sex(self, sex):
366
- return self.sex_encoding_dic()[sex]
367
-
368
- def decode_sex(self, code):
369
- return self.sex_decoding_dic()[code]
370
-
371
- def sex_encoding_dic(self):
372
- return {
373
- "Male": 0,
374
- "Female": 1
375
- }
376
-
377
- def sex_decoding_dic(self):
378
- return {
379
- 0: "Male",
380
- 1: "Female"
381
- }
 
1
  """Compas Dataset"""
2
 
3
  from typing import List
4
+ from functools import partial
5
  import datetime
6
 
7
  import datasets
 
82
  "violence_decile_score",
83
  "two_year_recidivous",
84
  ]
85
+ _ENCODING_DICS = {
86
+ "sex": {
87
+ "Male": 0,
88
+ "Female": 1
89
+ },
90
+ "race": {
91
+ "Caucasian": 0,
92
+ "African-American": 1,
93
+ "Hispanic": 2,
94
+ "Asian": 3,
95
+ "Other": 4,
96
+ "Native American": 5,
97
+ }
98
+ }
99
 
100
  DESCRIPTION = "COMPAS dataset for recidivism prediction."
101
  _HOMEPAGE = "https://github.com/propublica/compas-analysis"
 
178
  "two_year_recidivous": datasets.Value("int64"),
179
  "number_of_prior_offenses": datasets.Value("int64"),
180
  },
181
+
182
+ "race": {
183
+ "sex": datasets.Value("int64"),
184
+ "age": datasets.Value("int64"),
185
+ "number_of_juvenile_fellonies": datasets.Value("int64"),
186
+ "decile_score": datasets.Value("int64"),
187
+ "number_of_juvenile_misdemeanors": datasets.Value("int64"),
188
+ "number_of_other_juvenile_offenses": datasets.Value("int64"),
189
+ "days_before_screening_arrest": datasets.Value("int64"),
190
+ "is_recidivous": datasets.Value("int64"),
191
+ "days_in_custody": datasets.Value("int64"),
192
+ "is_violent_recidivous": datasets.Value("int64"),
193
+ "violence_decile_score": datasets.Value("int64"),
194
+ "two_year_recidivous": datasets.Value("int64"),
195
+ "number_of_prior_offenses": datasets.Value("int64"),
196
+ "race": datasets.ClassLabel(num_classes=6, names=("Caucasian", "African-American",
197
+ "Hispanic", "Asian", "Other", "Native American")),
198
+ }
199
  }
200
  features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
201
 
 
240
  ]
241
 
242
  def _generate_examples(self, filepath: str):
243
+ if self.config.name not in features_types_per_config:
244
+ raise ValueError(f"Unknown config: {self.config.name}")
245
+
246
+ if self.config.name == "encoding":
247
+ data = self.encoding_dicts()
248
+ else:
249
+ data = pandas.read_csv(filepath)
250
+ data = self.preprocess(data, config=self.config.name)
251
 
252
  for row_id, row in data.iterrows():
253
  data_row = dict(row)
 
326
 
327
  data.columns = _BASE_FEATURE_NAMES
328
 
329
+ for feature in _ENCODING_DICS:
330
+ if feature == "race":
331
+ if config != "race":
332
+ continue
333
+ encoding_function = partial(self.encode, feature)
334
+ data.loc[:, feature] = data[feature].apply(encoding_function)
335
+
336
+ return data[list(features_types_per_config[config].keys())]
337
+
338
+ def encoding_dics(self):
339
+ data = [pandas.DataFrame([(feature, original, encoded) for original, encoded in d.items()])
340
+ for feature, d in _ENCODING_DICS.items()]
341
+ data = pandas.concat(data, axis="rows").reset_index()
342
+ data.drop("index", axis="columns", inplace=True)
343
+ data.columns = ["feature", "original_value", "encoded_value"]
344
 
 
 
 
 
 
 
 
345
  return data