-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataLoader.py
135 lines (102 loc) · 4.57 KB
/
DataLoader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Formats data for ML training.
Formats the UCI Adult data into numeric values.
Jack Amend
3/3/2020
"""
import pandas as pd
class DataLoader:
def __init__(self, file_name):
df = pd.read_csv(file_name, header=None)
cols = ["age", "workclass", "fnlwgt", "education", "education-num", "marital-status",
"occupation", "relationship", "race", "sex", "capital-gain", "capital-loss",
"hours-per-week", "native-country", "label"]
df = df.rename(columns={i: c for i, c in enumerate(cols)})
df = df.drop('fnlwgt', axis="columns")
self.df = df
def get_numeric_data(self, balanced=False):
numeric_col = ['age', 'education-num', 'capital-gain', 'capital-loss',
'hours-per-week']
boolean_cols = ['sex', 'label']
category_cols = ['workclass', 'marital-status', 'occupation',
'relationship',
'race', 'native-country']
df = self.df.copy()
df = df.drop(['education'], axis=1)
if balanced:
df.sex = df.sex.str.strip()
gender_counts = df.sex.value_counts()
diff_needed = gender_counts['Male'] - gender_counts['Female']
df = df.append(df[df.sex == 'Female'].sample(diff_needed, replace=True, random_state=7215))
for bol in boolean_cols:
df[bol] = df[bol].apply(lambda x: 0.0 if x == df[bol][0] else 1.0)
for col in numeric_col:
df[col] = (df[col] - df[col].min()) / (df[col].max() - df[col].min())
df = pd.get_dummies(df, columns=category_cols)
data = df.drop('label', axis=1).values
labels = df['label'].values
protected = df['sex'].values
return data, labels, protected
def get_data_labels(file_name, verbose=1):
if verbose:
print("reading in data")
df = pd.read_csv(file_name, header=None)
cols = ["age", "workclass", "fnlwgt", "education", "education-num",
"marital-status",
"occupation", "relationship", "race", "sex", "capital-gain",
"capital-loss",
"hours-per-week", "native-country", "label"]
df = df.rename(columns={i: c for i, c in enumerate(cols)})
df = df.drop('fnlwgt', axis="columns")
numeric_col = ['age', 'education-num', 'capital-gain', 'capital-loss',
'hours-per-week']
boolean_cols = ['sex', 'label']
category_cols = ['workclass', 'marital-status', 'occupation',
'relationship',
'race', 'native-country']
df = df.drop(['education'], axis=1)
for col in numeric_col:
df[col] = (df[col] - df[col].min()) / (df[col].max() - df[col].min())
for bol in boolean_cols:
df[bol] = df[bol].apply(lambda x: 0.0 if x == df[bol][0] else 1.0)
df = pd.get_dummies(df, columns=category_cols)
print(df.head())
print("shape =", df.shape)
data = df.drop('label', axis=1).values
labels = df['label'].values
protected = df['sex'].values
return data, labels, protected
def get_balanced_data(file_name, verbose=1):
if verbose:
print("reading in data")
df = pd.read_csv(file_name, header=None)
cols = ["age", "workclass", "fnlwgt", "education", "education-num",
"marital-status",
"occupation", "relationship", "race", "sex", "capital-gain",
"capital-loss",
"hours-per-week", "native-country", "label"]
df = df.rename(columns={i: c for i, c in enumerate(cols)})
df = df.drop('fnlwgt', axis="columns")
# Sampling to make balanced data
df.sex = df.sex.str.strip()
gender_counts = df.sex.value_counts()
diff_needed = gender_counts['Male'] - gender_counts['Female']
df = df.append(df[df.sex == 'Female'].sample(diff_needed, replace=True, random_state=7215))
numeric_col = ['age', 'education-num', 'capital-gain', 'capital-loss',
'hours-per-week']
boolean_cols = ['sex', 'label']
category_cols = ['workclass', 'marital-status', 'occupation',
'relationship',
'race', 'native-country']
df = df.drop(['education'], axis=1)
for col in numeric_col:
df[col] = (df[col] - df[col].min()) / (df[col].max() - df[col].min())
for bol in boolean_cols:
df[bol] = df[bol].apply(lambda x: 0.0 if x == df[bol][0] else 1.0)
df = pd.get_dummies(df, columns=category_cols)
print(df.head())
print("shape =", df.shape)
data = df.drop('label', axis=1).values
labels = df['label'].values
protected = df['sex'].values
return data, labels, protected