-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlomika_prep.py
executable file
·179 lines (142 loc) · 6.56 KB
/
lomika_prep.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
173
174
175
176
177
178
#!/usr/bin/env python3
# coding: utf-8
import pickle
import pandas as pd
from util_lomika import *
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
def prepare_adult_dataset(filename, path_data):
## 0 - poor 1 - rich
# Read dataset
df = pd.read_csv(path_data + filename, delimiter=',', skipinitialspace=True, engine='python')
df.rename(columns={'class': 'target', 'education-num': 'education_num', 'marital-status': 'marital_status',
'capital-gain': 'capital_gain', 'capital-loss': 'capital_loss',
'hours-per-week': 'hours_per_week', 'native-country': 'native_country'}, inplace=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]
df_bl = df.copy()
label_le = LabelEncoder()
df['target'] = label_le.fit_transform(df['target'].values)
# Numerical variables
numerical_vars = ['age', 'capital_gain', 'capital_loss', 'hours_per_week']
# Categorical variables
categorical_vars = ['workclass','marital_status', 'occupation', 'education',
'relationship', 'race', 'sex', 'native_country']
df_le, label_encoder = label_encode(df, categorical_vars, label_encoder=None)
df_le.to_csv('/home/dilini/lomika/adult/data_MO/adult.csv', sep=',', encoding='utf-8')
train, test = train_test_split(df_le, test_size=0.2, random_state=0)
dataset = {
'name': filename.replace('.csv', ''),
'df_bl': df_bl,
'categorical_vars': categorical_vars,
'numerical_vars': numerical_vars,
'label_encoder': label_encoder,
'train': train,
'test': test,
'df_le': df_le
}
return dataset
def prepare_german_dataset(filename, path_data):
# Read Dataset
df = pd.read_csv(path_data + filename, delimiter=',')
df.rename(columns={'default': 'target'}, inplace=True)
label_le = LabelEncoder()
df['target'] = label_le.fit_transform(df['target'].values)
numerical_vars = ['duration_in_month', 'credit_amount', 'age']
categorical_vars = ['other_debtors', 'people_under_maintenance', 'personal_status_sex', 'present_res_since',
'credits_this_bank', 'account_check_status', 'job', 'savings', 'credit_history',
'purpose', 'present_emp_since', 'property', 'other_installment_plans', 'housing',
'installment_as_income_perc', 'telephone', 'foreign_worker']
df_le, label_encoder = label_encode(df, categorical_vars, label_encoder=None)
if 'Unnamed: 0' in df_le.columns:
df_le.drop('Unnamed: 0', axis=1, inplace=True)
df_le.to_csv('/home/dilini/lomika/german/data_MO/german.csv',sep=',', encoding='utf-8')
if 'Unnamed: 0' in df_le.columns:
df_le.drop('Unnamed: 0', axis=1, inplace=True)
df_bl = df_le.copy()
train, test = train_test_split(df_le, test_size=0.2, random_state=0)
dataset = {
'name': filename.replace('.csv', ''),
'df_bl': df_bl,
'categorical_vars': categorical_vars,
'numerical_vars': numerical_vars,
'label_encoder': label_encoder,
'train': train,
'test': test,
'df_le': df_le
}
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.rename(columns={'age_cat': 'birthgroup'}, inplace=True)
print(df)
df['c_jail_out'] = pd.to_datetime(df['c_jail_out'])
print(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('int64')
df['days_b_screening_arrest'] = df['days_b_screening_arrest'].astype('int64')
df['is_recid'] = df['is_recid'].apply(str)
df['is_violent_recid'] = df['is_violent_recid'].apply(str)
df['two_year_recid'] = df['two_year_recid'].apply(str)
df['is_recid'] = df['is_recid'].astype(object)
df['is_violent_recid'] = df['is_violent_recid'].astype(object)
df['two_year_recid'] = df['two_year_recid'].astype(object)
def get_class(x):
if x < 7:
return 'Medium_Low'
else:
return 'High'
df['target'] = df['decile_score'].apply(get_class)
del df['c_jail_in']
del df['c_jail_out']
del df['decile_score']
del df['score_text']
df_bl = df.copy()
label_le = LabelEncoder()
df['target'] = label_le.fit_transform(df['target'].values)
numerical_vars = ['age', 'priors_count', 'days_b_screening_arrest', 'length_of_stay']
categorical_vars = ['is_recid',
'c_charge_degree',
'is_violent_recid',
'birthgroup',
'two_year_recid',
'race',
'sex']
df_le, label_encoder = label_encode(df, categorical_vars, label_encoder=None)
if 'Unnamed: 0' in df_le.columns:
df_le.drop('Unnamed: 0', axis=1, inplace=True)
df_le.to_csv('/home/dilini/lomika/compass/data_MO/compass.csv',sep=',', encoding='utf-8')
if 'Unnamed: 0' in df_le.columns:
df_le.drop('Unnamed: 0', axis=1, inplace=True)
df_bl = df_le.copy()
train, test = train_test_split(df_le, test_size=0.2, random_state=0)
dataset = {
'name': filename.replace('.csv', ''),
'df_bl': df_bl,
'categorical_vars': categorical_vars,
'numerical_vars': numerical_vars,
'label_encoder': label_encoder,
'train': train,
'test': test,
'df_le': df_le
}
return dataset