Datasets:
Upload compas.py
Browse files
compas.py
CHANGED
@@ -93,6 +93,12 @@ urls_per_split = {
|
|
93 |
"train": "https://huggingface.co/datasets/mstz/compas/raw/main/compas-scores-two-years-violent.csv",
|
94 |
}
|
95 |
features_types_per_config = {
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
"two-years-recidividity": {
|
97 |
"sex": datasets.Value("int64"),
|
98 |
"age": datasets.Value("int64"),
|
@@ -176,6 +182,8 @@ class Compas(datasets.GeneratorBasedBuilder):
|
|
176 |
# dataset versions
|
177 |
DEFAULT_CONFIG = "two-years-recidividity"
|
178 |
BUILDER_CONFIGS = [
|
|
|
|
|
179 |
CompasConfig(name="two-years-recidividity",
|
180 |
description="Compas binary classification for two-year recidividity."),
|
181 |
CompasConfig(name="two-years-recidividity-no-race",
|
@@ -207,8 +215,6 @@ class Compas(datasets.GeneratorBasedBuilder):
|
|
207 |
data = pandas.read_csv(filepath)
|
208 |
data = self.preprocess(data, config=self.config.name)
|
209 |
|
210 |
-
print("data.columns")
|
211 |
-
print(data.columns)
|
212 |
for row_id, row in data.iterrows():
|
213 |
data_row = dict(row)
|
214 |
|
@@ -257,10 +263,14 @@ class Compas(datasets.GeneratorBasedBuilder):
|
|
257 |
# drop nan values
|
258 |
data = data[~data.days_b_screening_arrest.isna()]
|
259 |
data.loc[:, "days_b_screening_arrest"] = data.days_b_screening_arrest.astype(int)
|
|
|
260 |
data = data[~data.r_days_from_arrest.isna()]
|
|
|
261 |
|
262 |
# transform columns into intervals
|
|
|
263 |
data = data[(~data.in_custody.isna()) & (~data.out_custody.isna())]
|
|
|
264 |
in_dates = data.in_custody.apply(datetime.date.fromisoformat)
|
265 |
out_dates = data.out_custody.apply(datetime.date.fromisoformat)
|
266 |
days_in_custody = [delta.days for delta in out_dates - in_dates]
|
@@ -284,16 +294,14 @@ class Compas(datasets.GeneratorBasedBuilder):
|
|
284 |
"v_decile_score",
|
285 |
"two_year_recid"]]
|
286 |
|
287 |
-
print("data.columns again")
|
288 |
-
print(data.columns)
|
289 |
data.columns = _BASE_FEATURE_NAMES
|
290 |
-
print("data.columns again 1")
|
291 |
-
print(data.columns)
|
292 |
|
293 |
# binarize features
|
294 |
data.loc[:, "sex"] = data.sex.apply(self.encode_sex)
|
295 |
|
296 |
-
if config == "
|
|
|
|
|
297 |
return self.two_years_recidividity_preprocessing(data)
|
298 |
elif config == "two-years-recidividity-no-race":
|
299 |
return self.two_years_recidividity_no_race_preprocessing(data)
|
@@ -304,6 +312,14 @@ class Compas(datasets.GeneratorBasedBuilder):
|
|
304 |
else:
|
305 |
raise ValueError(f"Unknown config: {config}")
|
306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
|
308 |
def two_years_recidividity_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
|
309 |
# categorize features
|
|
|
93 |
"train": "https://huggingface.co/datasets/mstz/compas/raw/main/compas-scores-two-years-violent.csv",
|
94 |
}
|
95 |
features_types_per_config = {
|
96 |
+
"config": {
|
97 |
+
"feature": datasets.Value("string"),
|
98 |
+
"original_value": datasets.Value("string"),
|
99 |
+
"encoded_value": datasets.Value("int8"),
|
100 |
+
},
|
101 |
+
|
102 |
"two-years-recidividity": {
|
103 |
"sex": datasets.Value("int64"),
|
104 |
"age": datasets.Value("int64"),
|
|
|
182 |
# dataset versions
|
183 |
DEFAULT_CONFIG = "two-years-recidividity"
|
184 |
BUILDER_CONFIGS = [
|
185 |
+
CompasConfig(name="encoding",
|
186 |
+
description="Encoding dictionaries for discrete labels."),
|
187 |
CompasConfig(name="two-years-recidividity",
|
188 |
description="Compas binary classification for two-year recidividity."),
|
189 |
CompasConfig(name="two-years-recidividity-no-race",
|
|
|
215 |
data = pandas.read_csv(filepath)
|
216 |
data = self.preprocess(data, config=self.config.name)
|
217 |
|
|
|
|
|
218 |
for row_id, row in data.iterrows():
|
219 |
data_row = dict(row)
|
220 |
|
|
|
263 |
# drop nan values
|
264 |
data = data[~data.days_b_screening_arrest.isna()]
|
265 |
data.loc[:, "days_b_screening_arrest"] = data.days_b_screening_arrest.astype(int)
|
266 |
+
print("dropping from " + data.shape[0])
|
267 |
data = data[~data.r_days_from_arrest.isna()]
|
268 |
+
print("dropped to" + data.shape[0])
|
269 |
|
270 |
# transform columns into intervals
|
271 |
+
print("dropping from " + data.shape[0])
|
272 |
data = data[(~data.in_custody.isna()) & (~data.out_custody.isna())]
|
273 |
+
print("dropped to" + data.shape[0])
|
274 |
in_dates = data.in_custody.apply(datetime.date.fromisoformat)
|
275 |
out_dates = data.out_custody.apply(datetime.date.fromisoformat)
|
276 |
days_in_custody = [delta.days for delta in out_dates - in_dates]
|
|
|
294 |
"v_decile_score",
|
295 |
"two_year_recid"]]
|
296 |
|
|
|
|
|
297 |
data.columns = _BASE_FEATURE_NAMES
|
|
|
|
|
298 |
|
299 |
# binarize features
|
300 |
data.loc[:, "sex"] = data.sex.apply(self.encode_sex)
|
301 |
|
302 |
+
if config == "encoding":
|
303 |
+
return self.encoding_dictionaries()
|
304 |
+
elif config == "two-years-recidividity":
|
305 |
return self.two_years_recidividity_preprocessing(data)
|
306 |
elif config == "two-years-recidividity-no-race":
|
307 |
return self.two_years_recidividity_no_race_preprocessing(data)
|
|
|
312 |
else:
|
313 |
raise ValueError(f"Unknown config: {config}")
|
314 |
|
315 |
+
def encoding_dictionaries(self):
|
316 |
+
race_dic, sex_dic = self.race_decoding_dic(), self.sex_decoding_dic()
|
317 |
+
race_data = [("race", race, code) for race, code in race_dic.items()]
|
318 |
+
sex_data = [("sex", sex, code) for sex, code in sex_dic.items()]
|
319 |
+
data = pandas.DataFrame(race_data + sex_data,
|
320 |
+
columns=["feature", "original_value", "encoded_value"])
|
321 |
+
|
322 |
+
return data
|
323 |
|
324 |
def two_years_recidividity_preprocessing(self, data: pandas.DataFrame) -> pandas.DataFrame:
|
325 |
# categorize features
|