-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
286 lines (231 loc) · 8.01 KB
/
Copy pathpreprocessing.py
File metadata and controls
286 lines (231 loc) · 8.01 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Contains all functions related to preprocessing the data. """
# To run the whole preprocessing just run the last function preprocess_df()
import numpy as np
import pandas as pd
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from feature_engine.imputation import CategoricalImputer
new_columns = [
"age",
"blood_pressure",
"specific_gravity",
"albumin",
"sugar",
"rbc",
"pus_cell",
"pus_clumps",
"bacteria",
"glucose",
"urea",
"creatinine",
"sodium",
"potassium",
"hemoglobin",
"pcv",
"wbc",
"rbc_count",
"hypertension",
"diabetes",
"coronary_disease",
"appetite",
"edema",
"anemia",
"ckd"
]
categorical_columns = [
"rbc",
"pus_cell",
"pus_clumps",
"bacteria",
"hypertension",
"diabetes",
"coronary_disease",
"appetite",
"edema",
"anemia",
"ckd",
"specific_gravity",
"albumin",
"sugar",
]
numerical_columns = [
"age",
"blood_pressure",
"glucose",
"urea",
"creatinine",
"sodium",
"potassium",
"hemoglobin",
"pcv",
"wbc",
"rbc_count"
]
# categorical (numerical categories, e.g. "1.0", "1.05", "1.1")
# "specific_gravity",
# "albumin",
# "sugar",
redundant_columns = [
"rbc"
]
def rename_columns(df,new_columns=new_columns):
"""Renames the columns of the dataframe with the names given
The column names will be matched in order.
Args:
df: a dataframe
new_columns: a list of the new column names
Returns:
The dataframe with the new column names.
"""
dict_names = {}
col_names =df.columns # passing current column names as a list
if len(col_names) != len(new_columns):
raise ValueError("Lists lenght missmatch") # sanity check to ensure we have added all the new columns names
# create mapping dictionary
dict_names = dict(zip(col_names, new_columns))
#apply renaming
renamed_df = df.rename(columns=dict_names)
#return dictionary to visualize mapping
return renamed_df
def missing_to_nan(df):
"""Converts the missing values ("?") to np.nan.
Args:
df: a dataframe
Returns:
The dataframe with the converted missing values.
"""
cleaned_df = df.replace("?", np.nan)
return cleaned_df
def columns_to_numeric(df, numerical_columns=numerical_columns):
"""Converts the given columns to numeric.
Args:
df: a dataframe
numerical_columns: a list of the column names to be converted
Returns:
The dataframe with the columns converted to numeric
"""
# Convert selected columns to numeric types
for col in numerical_columns:
df[col] = pd.to_numeric(df[col], errors="coerce")
return df
def cleaning_categorical_columns(df,categorical_cols=categorical_columns):
"""Cleans the formatting of the categorical columns of the dataframe
Args:
df: a dataframe
categorical_columns: a list of the categorical column names
Returns:
The dataframe with the column values cleaned.
"""
# Strip leading/trailing whitespace from all string values in these columns
df[categorical_columns] = df[categorical_columns].map(lambda x: x.strip() if isinstance(x, str) else x)
return df
def remove_outliers(df):
"""Remove outliers from the dataset.
The method to select these outliers is visual inspection of pair plots plus
knowledge of the expected values of the variables. The reasoning can be
found in Scientific_Programming.ipynb in Task 2.
Args:
df: a dataframe
Returns:
The dataframe without the ouliers
"""
idxs = df[(df["sodium"] < 5) | (df["potassium"] > 20)].index
no_outliers_df = df.drop(labels=idxs,inplace=False).reset_index(drop=True)
return no_outliers_df
def normalize_df(df, numerical_columns=numerical_columns):
"""Normalizes the numerical columns in the df using 0-1 normalization.
Args:
df: a dataframe
numerical_columns: a list of the numeric column names
Returns:
The dataframe with its numerical columns normalized
"""
df[numerical_columns] = (df[numerical_columns]-df[numerical_columns].min()) / \
(df[numerical_columns].max()-df[numerical_columns].min())
return df
def impute_numerical(df,numerical_columns=numerical_columns,method="lr"):
"""Imputes the missing values of numerical columns in the dataframe.
The method used is multiple imputation (MICE) with linear regression (lr) or
a random forest classifier (rf).
Args:
df: a dataframe
numerical_columns: a list of the numeric column names
method: the method to be used for imputation. Either "lr" or "rf".
"""
if method == "lr":
estimator = LinearRegression()
if method == "rf":
estimator = RandomForestRegressor(n_estimators=100)
# We create the imputer
imp = IterativeImputer(estimator=estimator, verbose=2, max_iter=10)#, tol=1e-10, imputation_order='roman')
# Apply it to the numerical columns
ImputedData = imp.fit_transform(df[numerical_columns])
# Convert the imputed values to a dataframe
Imputed_data = pd.DataFrame(ImputedData)
Imputed_data.columns = numerical_columns
# Substitute the imputed values into the dataframe
imp_df = df.copy()
imp_df[numerical_columns] = Imputed_data
return imp_df
def remove_redundant_columns(df, redundant_columns=redundant_columns):
"""Removes redundant columns from the dataset.
Args:
df: a dataframe
redundant_columns: a list of redundant column names
Returns:
The dataframe without redundant columns.
"""
df_dropped = df.drop(redundant_columns, axis=1, inplace=False)
return df_dropped
def stratified_categorical_imputer(df, categorical_columns=categorical_columns):
"""Performs categorical imputation with a stratified dataframe.
Performs stratified imputation on the categorical variables, using
the most frequent value as imputation and stratifying by age.
The age groups are 0-20, 21-40, 41-60, 61+.
Args:
df: a dataframe
categorical_columns: a list of the categorical column names
Returns
The dataframe with imputed values.
"""
imputed_df = df.copy()
age_groups = [0,20,40,60,200]
for n in range(len(age_groups)-1):
strat_idxs = (df["age"] >= age_groups[n]) & (df["age"] < age_groups[n+1])
strat_df = df[strat_idxs][categorical_columns]
imputer = CategoricalImputer(
variables=categorical_columns,
imputation_method="frequent"
)
imputer.fit(strat_df)
imputed_df.loc[strat_idxs,categorical_columns] = imputer.transform(strat_df)
return imputed_df
def preprocess_df(df):
"""Peforms data cleaning on the dataframe.
This is the entire data cleaning pipeline.
Args:
df: a dataframe
Returns:
The cleaned dataframe.
"""
# Renames all the columns to clearer names
df = rename_columns(df)
# Replaces the missing values (default "?") with np.nan
df = missing_to_nan(df)
# Converts the numeric columns to numeric type (default object)
df = columns_to_numeric(df)
# Cleans the formatting of the categories names in categorical variables
df = cleaning_categorical_columns(df)
# Remove outliers
df = remove_outliers(df)
# Impute numerical
df = impute_numerical(df)
# Impute categorical
df = stratified_categorical_imputer(df)
# Remove redundant columns
df = remove_redundant_columns(df)
# Normalize numerical values 0-1
df = normalize_df(df)
return df