-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass_ML_Project.py
365 lines (298 loc) · 14.8 KB
/
Class_ML_Project.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
class for the project
"""
import pandas as pd
import os
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.impute import SimpleImputer
from sklearn.base import BaseEstimator, TransformerMixin, ClassifierMixin
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler, MinMaxScaler, LabelEncoder
from sklearn.tree import DecisionTreeClassifier , export_graphviz
from sklearn.model_selection import cross_val_score ,StratifiedGroupKFold , train_test_split, StratifiedKFold
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, confusion_matrix, make_scorer
from sklearn.impute import KNNImputer
from sklearn.multiclass import OneVsRestClassifier
import random
import string
import pickle
import copy
import glob
import re
from imblearn.over_sampling import SMOTENC
from imblearn.ensemble import BalancedRandomForestClassifier
import lightgbm as lgb
######################################### class ########################################
class FeatureRemover(BaseEstimator, TransformerMixin):
#Remove features with irellevant information
##### Feature selection and Imputation using a pipline #####
"""There are reasons to exclude features:
1. Irrelevant based on domain
2. >50% missing values
3. Low variance in the feature cannot contribute to the model
"""
def __init__(self, features_to_remove):
self.features_to_remove = features_to_remove
def fit(self, X, y=None):
# Remove features with >50% missing values
#if self.features_to_remove is None:
# Calculate the percentage of missing values in each column
missing_percentages = (X.isnull().sum() / len(X)) * 100
# Filter out features (columns) where missing percentage is above 50%
self.features_to_remove += missing_percentages[missing_percentages > 50].index.tolist()
# Low variance
# Check if each column has more than two unique values
for column in X.columns:
if len(X[column].unique()) < 2:
self.features_to_remove.append(column)
return self
def transform(self, X):
return X.drop(columns=self.features_to_remove)
class DataFrameImputer(BaseEstimator, TransformerMixin):
"""
A wrapper for SimpleImputer that returns a pandas DataFrame
required for our custome OHE.
"""
def __init__(self, strategy='mean', fill_value=None, columns=None):
self.strategy = strategy
self.fill_value = fill_value
self.columns = columns
self.imputer = None
def fit(self, X, y=None):
if self.columns is not None:
# Impute only specified columns
self.imputer = SimpleImputer(strategy=self.strategy, fill_value=self.fill_value)
self.imputer.fit(X[self.columns])
else:
# Impute all columns
self.imputer = SimpleImputer(strategy=self.strategy, fill_value=self.fill_value)
self.imputer.fit(X)
return self
def transform(self, X):
# Apply imputation
if self.columns is not None:
# Impute only specified columns
X_transformed = X.copy()
X_transformed[self.columns] = self.imputer.transform(X[self.columns])
result_df = X_transformed
else:
# Apply imputation to all columns
result = self.imputer.transform(X)
result_df = pd.DataFrame(result, columns=X.columns, index=X.index)
return result_df
class NumericalTransformer(BaseEstimator, TransformerMixin):
# a class for numerical scaling
# optional log2 for chosen features by name
def __init__(self, columns=None, log_column='num_medications'):
"""_summary_
columns: list of column names for Min-Max scaling
log_column: specific column name to apply log2 transformation before Min-Max scaling
"""
self.columns = columns
self.log_column = log_column
self.scalers = {} # To store individual scalers per column
def fit(self, X, y=None):
# Fit scaler to each specified column individually
for col in self.columns:
scaler = StandardScaler()
if col == self.log_column:
# Log-transform then fit scaler
# Convert to numpy array for numpy log function
data = X[col].values.reshape(-1, 1).astype(float)
data = np.log2(data + 1)
self.scalers[col] = scaler.fit(data)
else:
# Fit scaler directly
self.scalers[col] = scaler.fit(X[[col]])
return self
def transform(self, X):
X_transformed = X.copy()
for col in self.columns:
if col == self.log_column:
data = X[col].values.reshape(-1, 1).astype(float)
data = np.log2(data + 1)
X_transformed[col] = self.scalers[col].transform(data)
else:
X_transformed[col] = self.scalers[col].transform(X[[col]])
return X_transformed
class SMOTENC_NS(BaseEstimator, TransformerMixin):
def __init__(self, categorical_features,
sampling_strategy = 'auto', k_neighbors=5, seed =42):
"""
Parameters:
categorical_features: array of ints corresponding to the indices specifying the categorical features
- sampling_strategy: Determine oversampling ratios, for multiclass dict is preferable.
defualts to "auto" and will oversample all classes to the majority class
- k_neighbors: Number of nearest neighbors used for the algorithm.
- seed: Random pseudoseed for KNN
"""
self.categorical_features = categorical_features
self.k_neighbors = k_neighbors
self.seed = seed
self.sampling_strategy = sampling_strategy
def fit_resample(self, X, y):
"""
Fits the SMOTENC resampler to the data and resamples it.
Parameters:
- X: Features matrix
- y: Target vector
Returns:
- X_resampled: The resampled features matrix
- y_resampled: The resampled target vector
"""
if self.categorical_features is None:
raise ValueError("Categorical feature indexes are not specified.")
# Encode your target variable if it's categorical
# Initialize SMOTENC with user-specified parameters
self.smotenc = SMOTENC(categorical_features=self.categorical_features,
k_neighbors=self.k_neighbors,
random_state=self.seed,
sampling_strategy = self.sampling_strategy)
# Fit SMOTENC and resample the data
X_resampled, y_resampled = self.smotenc.fit_resample(X, y)
return X_resampled, y_resampled
class CustomOHEncoder(BaseEstimator, TransformerMixin):
"""_summary_
OHE categorical features in different manners
OHE_regular_cols - columns to reguular OHE with sklearn class
OHE_4_to_2_cols - columns to change 4 values to 2 values
all medication were reduced to 1 - changed dose / 0 - stable/NaN
change_col - column to chnage after chage in OHE_4_to_2_cols
specifically for "Change" column to see if there was a change in medication based
based on a "Yes" and a lack of change of dosage in other medications
diag_cols - coulmns to be expanded specifically diagnoses columns where each pateint
had more than 2 diagnoses so expand to column per disease and drops diabetes
"""
def __init__(self, OHE_regular_cols=[], OHE_4_to_2_cols=[], change_col=None, diag_cols=[]):
self.OHE_regular_cols = OHE_regular_cols
self.OHE_4_to_2_cols = OHE_4_to_2_cols
self.change_col = change_col
self.diag_cols = diag_cols
self.ohe = OneHotEncoder(drop='if_binary')
self.unique_diagnoses = None
def fit(self, X, y=None):
# Fit the regular OHE encoder
if self.OHE_regular_cols:
self.ohe.fit(X[self.OHE_regular_cols])
# Prepare unique diagnoses for diagnosis encoding
if self.diag_cols:
melted_disease = pd.melt(X[self.diag_cols])
self.unique_diagnoses = melted_disease['value'].unique()
return self
def transform(self, X):
result = X.copy()
# Apply regular OHE
if self.OHE_regular_cols:
transformed = self.ohe.transform(result[self.OHE_regular_cols]).toarray()
result = result.drop(columns=self.OHE_regular_cols)
result = result.join(pd.DataFrame(transformed, columns=self.ohe.get_feature_names_out(), index=result.index))
# Apply 4-to-2 encoding - for medication
if self.OHE_4_to_2_cols:
result[self.OHE_4_to_2_cols] = result[self.OHE_4_to_2_cols].replace({'No': 0, 'Steady': 0, 'Up': 1, 'Down': 1})
# Apply "change" transformation
#based on medication swap and not dosage change
if self.change_col and self.OHE_4_to_2_cols:
# checks if dosage was not chamges among medication exisitng in dataset
dosage_changed_bool = result[self.OHE_4_to_2_cols].apply(lambda x: sum(x > 0) == 0, axis=1)
# iterate to over all records ,mark 1 where doasge was not changed but medication was
new_change = [(1 if i and ch == 'Ch' else 0) for i, ch in zip(dosage_changed_bool, result[self.change_col])]
result[self.change_col] = new_change
# Apply disease diagnosis encoding
if self.diag_cols and self.unique_diagnoses is not None:
# prepare a zero matrix to count for dieases per patient
ohe_diagnosis = pd.DataFrame(np.zeros((result.shape[0], len(self.unique_diagnoses))),
columns=self.unique_diagnoses, index=result.index)
#get disease diagnosis per record
record_disease = result[self.diag_cols].apply(lambda x: x.value_counts().index.values, axis=1)
# iterate over pateint diagnosis and add one to ohe_diagnosis in the relevant place
for row, diag in enumerate(record_disease):
for dis in diag:
if dis in ohe_diagnosis.columns:
ohe_diagnosis.loc[row, dis] = 1
# drop Diabetes since they all have it
ohe_diagnosis.drop(['Diabetes'], axis=1, inplace=True, errors='ignore')
ohe_diagnosis = ohe_diagnosis.iloc[:, :-1] # Drop last column for None diagnosis
# remove input columns
result = result.drop(columns=self.diag_cols)
# add the untouched columns
result = result.join(ohe_diagnosis)
return result
class MultiColumnLabelEncoder(BaseEstimator, TransformerMixin):
def __init__(self, columns=None):
# List of column names to encode
self.columns = columns
def fit(self, X, y=None):
# Create a dictionary to store label encoders for each column
self.encoders = {}
if self.columns is not None:
for col in self.columns:
le = LabelEncoder()
le.fit(X[col].astype('category')) # Fit a label encoder
self.encoders[col] = le
return self
def transform(self, X):
"""
Transform columns of X specified in self.columns using LabelEncoder().
Non-specified columns are passed without modification.
"""
if self.columns is not None:
for col in self.columns:
encoder = self.encoders[col]
X[col] = pd.Series(encoder.transform(X[col].astype('category')), index=X.index).astype('category')
return X
def inverse_transform(self, X):
"""
Inverse transform columns of X specified in self.columns using the inverse_transform method of LabelEncoder.
"""
X = X.copy()
if self.columns is not None:
for col in self.columns:
encoder = self.encoders[col]
X[col] = encoder.inverse_transform(X[col]) # Inverse transform the data
return X
class MultiModelCV(BaseEstimator, ClassifierMixin):
def __init__(self, models, folds=5, balance_threshold=0.2, score=None):
"""
Fits several models to the data and runs a cross-validation to comapre their preformace
models: Dictionary of model names and model instances, excluding the RandomForest variant.
balance_threshold: Threshold to determine if the dataset is balanced, based on the ratios of different classes to the largest class.
folds: number of Kfolds for CV evaluation
score: type of scorer to use in the cross validtion - if None than the defualt one will be set by SKlearn
OUTPUT: pandas DataFrame of all models abd average chosen score across all cross validation folds
"""
self.models = models
self.folds = folds
self.balance_threshold = balance_threshold
self.score = score or custom_avg_precision_score
self.results_ = None
def fit(self, X, y):
# Check balance for multiclass labels
_, counts = np.unique(y, return_counts=True)
ratios = counts / np.max(counts)
is_balanced = sum(ratios > self.balance_threshold) == len(counts)
# Dynamically select and add RandomForestClassifier based on balance
rf_model_name = 'BalancedRandomForestClassifier' if not is_balanced else 'RandomForestClassifier'
rf_model = BalancedRandomForestClassifier(random_state=42) if not is_balanced else RandomForestClassifier(random_state=42)
self.models[rf_model_name] = rf_model
# Scorer
if callable(self.score):
scoring = make_scorer(self.score, needs_proba=True)
else:
scoring = self.score
cv = StratifiedKFold(n_splits=self.folds, shuffle=True, random_state=42)
results = []
for name, model in self.models.items():
print("\n_____________\n",name,"\n_____________\n")
if name != 'Tree':
ovm = OneVsRestClassifier(model)
else:
ovm = model
scores = cross_val_score(ovm, X, y, cv=cv, scoring=scoring, verbose=3)
results.append({'Model': name, 'Score': scores.mean()})
self.results_ = pd.DataFrame(results).sort_values(by='Score', ascending=False)
return self
def get_results(self):
return self.results_