-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPhreesia_Data_Two.py
More file actions
409 lines (312 loc) · 13.9 KB
/
Copy pathPhreesia_Data_Two.py
File metadata and controls
409 lines (312 loc) · 13.9 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import os
import abc
import numpy as np
import pandas as pd
from pprint import pprint
from typing import List, Dict, AnyStr, Tuple, Set
from sklearn.model_selection import train_test_split
# set up pandas display options
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
# Naïve Bayes Classifier
class ML_ModelInterface(metaclass=abc.ABCMeta):
"""Abstract base class for model creation"""
@abc.abstractmethod
def load_data_set(self, file_path: str, file_name: str, chunk_size: int, **columns) -> pd.DataFrame:
"""Load in the target data set"""
pass
@abc.abstractmethod
def transformations(self, df: pd.DataFrame, **cols) -> List[Dict[str, float]]:
"""Create a transformations for the specified columns"""
pass
@abc.abstractmethod
def categorical_encoders(self, df: pd.DataFrame, sample_size: float) -> None:
"""Set the Encoding type to perform on the categorical data"""
pass
@abc.abstractmethod
def set_target_feature(self, feature: str) -> None:
"""Set the target(y) feature"""
pass
@abc.abstractmethod
def feature_selection(self, df: pd.DataFrame, **measures) -> None:
"""Select the top n features based on a suite of accuracy measures"""
pass
@abc.abstractmethod
def build_model(self) -> None:
"""Create a model """
pass
class ML_Model(ML_ModelInterface):
"""Concrete implementation of ML_ModelInterface"""
def __init__(self):
self.data_set: pd.DataFrame = None # stores the current chunk
self.dummy_df: pd.DataFrame = None # One hot encoded data frame
self.frames: List[pd.DataFrame] = [] # stores the data set in chunks
self.variable_mapping: dict = {}
# model variables
self.y: pd.Series = None # the target variable
self.X: pd.DataFrame = None # the predictors
self.X_train = None
self.X_test = None
self.y_train = None
self.y_test = None
def load_data_set(self, file_path: str, file_name: str, chunk_size: int, **columns):
"""*required method"""
try:
if os.path.isdir(file_path):
if os.path.isfile(os.path.join(file_path, file_name)):
current_chunk = pd.read_csv(
filepath_or_buffer=os.path.join(file_path, file_name),
low_memory=False, engine='c', chunksize=chunk_size,
usecols=[columns[c] for c in columns]
)
# grab only one chunk at a time
for chunk in current_chunk:
# perform data frame transformations
df = self.transformations(chunk, **columns)
self.frames.append(df)
# coerce into a single data frame
self.data_set = pd.concat(self.frames)
return self.data_set
else:
raise OSError(f"OSError: File: {file_name} not found in directory: {file_path}.")
else:
raise OSError(f"OSError: Directory: {file_path} not found.")
except OSError as e:
print(e)
def transformations(self, df: pd.DataFrame, **cols):
"""*required method"""
for col in cols:
if df[cols[col]].dtype == 'object':
unique_values = set(df[cols[col]].values)
d = {list(unique_values)[i]: i+1 for i, _ in enumerate(list(unique_values))}
# maintain a dictionary of conversions
self.variable_mapping.update({col: d})
# if the df has NaN values, fill them
if df[cols[col]].isna().sum() > 0:
df[cols[col]].fillna(value=-99)
# transform the data frame column
df[cols[col]] = [d[item] for item in df[cols[col]]]
return df
def categorical_encoders(self, df: pd.DataFrame, sample_size: float) -> pd.DataFrame:
"""*required method"""
# make sure the sample_size is in the (0, 1] interval
assert sample_size > 0.0 and sample_size <= 1.0
# take a random sample of data from the data set to avoid memory limitations
random_sample = df.sample(frac=sample_size, replace=False)
# get a list of prefixes
dummy_prefix = [p for p in list(random_sample.columns)]
# One hot encode the data to build binary classifiers
self.dummy_df = pd.concat(
[pd.get_dummies(random_sample[col], prefix=p)
for col in random_sample
for p in dummy_prefix
],
axis=1,
keys=random_sample.columns
)
# return the dummy data frame
return self.dummy_df
def set_target_feature(self, feature: str) -> None:
"""*required method"""
self.y = self.data_set[feature]
self.X = self.data_set.drop(columns=feature)
def feature_selection(self, df: pd.DataFrame, **measures):
"""*required method"""
pass
def build_model(self):
"""*required method"""
pass
def main():
file_path = "/Users/williamrobertmurphy/Downloads"
file_name = "Hospital_Inpatient_Discharges__SPARCS_De-Identified___2015.csv"
ml_model = ML_Model()
ml_model.load_data_set(
file_path=file_path, file_name=file_name, chunk_size=200000,
gender='Gender', race='Race', ethnicity='Ethnicity', age_group='Age Group',
type_of_admission='Type of Admission',
severity_of_illness='APR Severity of Illness Description',
risk_of_mortality='APR Risk of Mortality',
css_procedure_description='CCS Diagnosis Description',
css_procedure_code='CCS Procedure Code'
)
ml_model.set_target_feature(feature='CCS Diagnosis Description')
encoded_df = ml_model.categorical_encoders(df=ml_model.X, sample_size=0.1)
pprint(ml_model.y)
if __name__ == '__main__':
main()
import os
import abc
import numpy as np
import pandas as pd
from pprint import pprint
from typing import List, Dict, AnyStr, Tuple, Set
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import VarianceThreshold, SelectKBest, chi2, f_classif
from sklearn.ensemble import RandomForestClassifier
# set up pandas display options
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
# Use Random Forests: Better than Decision Tree(they over fit)
class ML_ModelInterface(metaclass=abc.ABCMeta):
"""Abstract base class for model creation"""
@abc.abstractmethod
def load_data_set(self, file_path: str, file_name: str, chunk_size: int, **columns) -> pd.DataFrame:
"""Load in the target data set"""
pass
@abc.abstractmethod
def transformations(self, df: pd.DataFrame, **cols) -> List[Dict[str, float]]:
"""Create a transformations for the specified columns"""
pass
@abc.abstractmethod
def categorical_encoders(self, df: pd.DataFrame, sample_size: float, feature: str) -> None:
"""Set the Encoding type to perform on the categorical data"""
pass
@abc.abstractmethod
def set_target_feature(self, df: pd.DataFrame, feature: str) -> None:
"""Set the target(y) feature"""
pass
@abc.abstractmethod
def feature_selection(self, df: pd.DataFrame, k: int) -> None:
"""Select the top n features based on a suite of accuracy measures"""
pass
@abc.abstractmethod
def build_model(self) -> None:
"""Create a model """
pass
class ML_Model(ML_ModelInterface):
"""Concrete implementation of ML_ModelInterface"""
def __init__(self):
self.data_set: pd.DataFrame = None # stores the current chunk
self.dummy_df: pd.DataFrame = None # One hot encoded data frame
self.frames: List[pd.DataFrame] = [] # stores the data set in chunks
self.variable_mapping: dict = {}
# model variables
self.y: pd.Series = None # the target variable
self.X: pd.DataFrame = None # the predictors
self.selected_features: pd.DataFrame = None
self.X_train = None
self.X_test = None
self.y_train = None
self.y_test = None
def load_data_set(self, file_path: str, file_name: str, chunk_size: int, **columns):
"""*required method"""
try:
if os.path.isdir(file_path):
if os.path.isfile(os.path.join(file_path, file_name)):
current_chunk = pd.read_csv(
filepath_or_buffer=os.path.join(file_path, file_name),
low_memory=False, engine='c', chunksize=chunk_size,
usecols=[columns[c] for c in columns]
)
# grab only one chunk at a time
for chunk in current_chunk:
# perform data frame transformations
df = self.transformations(chunk, **columns)
self.frames.append(df)
# coerce into a single data frame
self.data_set = pd.concat(self.frames)
return self.data_set
else:
raise OSError(f"OSError: File: {file_name} not found in directory: {file_path}.")
else:
raise OSError(f"OSError: Directory: {file_path} not found.")
except OSError as e:
print(e)
def transformations(self, df: pd.DataFrame, **cols):
"""*required method"""
for col in cols:
if df[cols[col]].dtype == 'object':
unique_values = set(df[cols[col]].values)
d = {list(unique_values)[i]: i+1 for i, _ in enumerate(list(unique_values))}
# maintain a dictionary of conversions
self.variable_mapping.update({col: d})
# if the df has NaN values, fill them
if df[cols[col]].isna().sum() > 0:
df[cols[col]].fillna(value=-99)
# transform the data frame column
df[cols[col]] = [d[item] for item in df[cols[col]]]
return df
def categorical_encoders(self, df: pd.DataFrame, sample_size: float, feature: str) -> pd.DataFrame:
"""*required method"""
# make sure the sample_size is in the (0, 1] interval
assert sample_size > 0.0 and sample_size <= 1.0
# take a random sample of data from the data set to avoid memory limitations
random_sample = df.sample(frac=sample_size, replace=False)
# set the target and predictors
self.set_target_feature(df=random_sample, feature=feature)
# get a list of prefixes
dummy_prefix = [p for p in list(random_sample.columns)]
# One hot encode the data to build binary classifiers
self.dummy_df = pd.concat(
[pd.get_dummies(random_sample[col], prefix=p)
for col in random_sample
for p in dummy_prefix
],
axis=1,
keys=random_sample.columns
)
# set the predictors
self.X = self.dummy_df.drop(columns=feature)
# return the dummy data frame
return self.dummy_df
def set_target_feature(self, df: pd.DataFrame, feature: str) -> None:
"""*required method"""
self.y = df[feature]
def feature_selection(self, df: pd.DataFrame, k: int):
"""*required method"""
feature = SelectKBest(f_classif, k=k)
X_new = feature.fit(df, self.y)
scores = pd.DataFrame()
scores['F score'] = feature.scores_
scores['P value'] = feature.pvalues_
scores['Support'] = feature.get_support()
scores['Attribute'] = df.columns
selected_features = scores[(scores.Support == True)]
attributes = [attr.rstrip('\n') for _, attr in selected_features.Attribute]
# build a new data frame from the selected multi-index features
features = []
for meta in df.columns.levels[0]:
print(meta)
#df.loc[:, ('Age Group', 'Age Group_1')]
pprint(df.loc[meta].head())
# set the selected features
self.selected_features = pd.concat(features)
pprint(self.selected_features.head(10))
return scores
def build_model(self):
"""*required method"""
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
)
def main():
file_path = "/Users/williamrobertmurphy/Downloads"
file_name = "Hospital_Inpatient_Discharges__SPARCS_De-Identified___2015.csv"
ml_model = ML_Model()
ml_model.load_data_set(
file_path=file_path, file_name=file_name, chunk_size=200000,
gender='Gender', race='Race', ethnicity='Ethnicity', age_group='Age Group',
type_of_admission='Type of Admission',
severity_of_illness='APR Severity of Illness Description',
risk_of_mortality='APR Risk of Mortality',
css_procedure_description='CCS Diagnosis Description'
)
# select the target feature(y)
#ml_model.set_target_feature(feature='CCS Diagnosis Description')
# one hot encode the predictors(X)
ml_model.categorical_encoders(df=ml_model.data_set, sample_size=0.15,
feature='CCS Diagnosis Description')
# perform feature selection
#pprint(ml_model.X.head())
#pprint(type(ml_model.X.loc[:, ('Age Group','Age Group_1')]))
#output = ml_model.feature_selection(df=ml_model.X, k=4)
#pprint(ml_model.selected_features.columns)
pprint(list(ml_model.X.columns.levels[0]))
pprint(list(ml_model.X.columns.levels[1]))
#pprint(ml_model.y)
#pprint(ml_model.dummy_df.head())
#pprint(ml_model.dummy_df.columns)
#pprint(ml_model.y.shape)
#pprint(output)
if __name__ == '__main__':
main()