-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathprepare_dataset.py
172 lines (132 loc) · 5.49 KB
/
prepare_dataset.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from util import *
def prepare_german_dataset(filename, path_data):
# Read Dataset
df = pd.read_csv(path_data + filename, delimiter=',')
# Features Categorization
columns = df.columns
class_name = 'default'
possible_outcomes = list(df[class_name].unique())
type_features, features_type = recognize_features_type(df, class_name)
discrete = ['installment_as_income_perc', 'present_res_since', 'credits_this_bank', 'people_under_maintenance']
discrete, continuous = set_discrete_continuous(columns, type_features, class_name, discrete, continuous=None)
columns_tmp = list(columns)
columns_tmp.remove(class_name)
idx_features = {i: col for i, col in enumerate(columns_tmp)}
# Dataset Preparation for Scikit Alorithms
df_le, label_encoder = label_encode(df, discrete)
X = df_le.loc[:, df_le.columns != class_name].values
y = df_le[class_name].values
dataset = {
'name': filename.replace('.csv', ''),
'df': df,
'columns': list(columns),
'class_name': class_name,
'possible_outcomes': possible_outcomes,
'type_features': type_features,
'features_type': features_type,
'discrete': discrete,
'continuous': continuous,
'idx_features': idx_features,
'label_encoder': label_encoder,
'X': X,
'y': y,
}
return dataset
def prepare_adult_dataset(filename, path_data):
# Read Dataset
df = pd.read_csv(path_data + filename, delimiter=',', skipinitialspace=True)
# Remove useless columns
del df['fnlwgt']
del df['education-num']
# Remove Missing Values
for col in df.columns:
if '?' in df[col].unique():
df[col][df[col] == '?'] = df[col].value_counts().index[0]
# Features Categorization
columns = df.columns.tolist()
columns = columns[-1:] + columns[:-1]
df = df[columns]
class_name = 'class'
possible_outcomes = list(df[class_name].unique())
type_features, features_type = recognize_features_type(df, class_name)
discrete, continuous = set_discrete_continuous(columns, type_features, class_name, discrete=None, continuous=None)
columns_tmp = list(columns)
columns_tmp.remove(class_name)
idx_features = {i: col for i, col in enumerate(columns_tmp)}
# Dataset Preparation for Scikit Alorithms
df_le, label_encoder = label_encode(df, discrete)
X = df_le.loc[:, df_le.columns != class_name].values
y = df_le[class_name].values
dataset = {
'name': filename.replace('.csv', ''),
'df': df,
'columns': list(columns),
'class_name': class_name,
'possible_outcomes': possible_outcomes,
'type_features': type_features,
'features_type': features_type,
'discrete': discrete,
'continuous': continuous,
'idx_features': idx_features,
'label_encoder': label_encoder,
'X': X,
'y': y,
}
return dataset
def prepare_compass_dataset(filename, path_data):
# Read Dataset
df = pd.read_csv(path_data + filename, delimiter=',', skipinitialspace=True)
columns = ['age', 'age_cat', 'sex', 'race', 'priors_count', 'days_b_screening_arrest', 'c_jail_in', 'c_jail_out',
'c_charge_degree', 'is_recid', 'is_violent_recid', 'two_year_recid', 'decile_score', 'score_text']
df = df[columns]
df['days_b_screening_arrest'] = np.abs(df['days_b_screening_arrest'])
df['c_jail_out'] = pd.to_datetime(df['c_jail_out'])
df['c_jail_in'] = pd.to_datetime(df['c_jail_in'])
df['length_of_stay'] = (df['c_jail_out'] - df['c_jail_in']).dt.days
df['length_of_stay'] = np.abs(df['length_of_stay'])
df['length_of_stay'].fillna(df['length_of_stay'].value_counts().index[0], inplace=True)
df['days_b_screening_arrest'].fillna(df['days_b_screening_arrest'].value_counts().index[0], inplace=True)
df['length_of_stay'] = df['length_of_stay'].astype(int)
df['days_b_screening_arrest'] = df['days_b_screening_arrest'].astype(int)
def get_class(x):
if x < 7:
return 'Medium-Low'
else:
return 'High'
df['class'] = df['decile_score'].apply(get_class)
del df['c_jail_in']
del df['c_jail_out']
del df['decile_score']
del df['score_text']
columns = df.columns.tolist()
columns = columns[-1:] + columns[:-1]
df = df[columns]
class_name = 'class'
possible_outcomes = list(df[class_name].unique())
type_features, features_type = recognize_features_type(df, class_name)
discrete = ['is_recid', 'is_violent_recid', 'two_year_recid']
discrete, continuous = set_discrete_continuous(columns, type_features, class_name, discrete=discrete,
continuous=None)
columns_tmp = list(columns)
columns_tmp.remove(class_name)
idx_features = {i: col for i, col in enumerate(columns_tmp)}
# Dataset Preparation for Scikit Alorithms
df_le, label_encoder = label_encode(df, discrete)
X = df_le.loc[:, df_le.columns != class_name].values
y = df_le[class_name].values
dataset = {
'name': filename.replace('.csv', ''),
'df': df,
'columns': list(columns),
'class_name': class_name,
'possible_outcomes': possible_outcomes,
'type_features': type_features,
'features_type': features_type,
'discrete': discrete,
'continuous': continuous,
'idx_features': idx_features,
'label_encoder': label_encoder,
'X': X,
'y': y,
}
return dataset