-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompData.py
495 lines (326 loc) · 14.1 KB
/
CompData.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# System Libraries
import os
import sys
Cred_Path = '/' + sys.path[0][1:27] + 'Credentials/Credentials.csv' # Create credentials path
# Database Connector
import pymysql
# Data Manipulator
import pandas as pd
import numpy as np
import random
pd.set_option('display.max_columns', None)
# Accessory Libraries
import time
###################################################################################################################
# Creates the query for the final Data set we will be using
def complete_data(Base_Columns, Base_Table, *argv):
'''
Function:
---------
Takes in the base table columns and the base table in a string format and
returns a structured SQL query as a string for our project.
Future Functionality:
---------------------
Adding more tables to the query's final form.
Description:
------------
This will return our query for the final construction of the data table.
'''
if len(argv) == 0:
Bureau_Trim_Filter = ''
else:
Bureau_Trim_Filter = '\n WHERE ' + argv[0]
Comp_Query = '''
WITH Base AS
(
SELECT {0}
FROM {1}
), Credit_Trimmed AS
(
SELECT SK_ID_PREV, SK_ID_CURR,
SUM(SK_DPD) AS TOTAL_SK_DPD,
SUM(SK_DPD_DEF) AS TOTAL_SK_DPD_DEF,
SUM(CASE WHEN SK_DPD > 0 THEN 1 ELSE 0 END) AS CNT_PAST_DUE,
SUM(CASE WHEN SK_DPD_DEF > 0 THEN 1 ELSE 0 END) AS CNT_PAST_DUE_DEF
FROM Credit_Card_Balance
GROUP BY 1, 2
), Bureau_Partitioned AS
(
SELECT SK_ID_CURR, CREDIT_ACTIVE, CREDIT_CURRENCY,
DAYS_CREDIT_UPDATE,
AMT_CREDIT_SUM,
AMT_CREDIT_SUM_DEBT,
AMT_CREDIT_SUM_OVERDUE,
DENSE_RANK() OVER(PARTITION BY SK_ID_CURR
ORDER BY AMT_CREDIT_SUM DESC) AS RANKING
FROM Bureau
WHERE AMT_CREDIT_SUM_DEBT != 0
), Bureau_Trim AS
(
SELECT SK_ID_CURR, CREDIT_ACTIVE, CREDIT_CURRENCY,
SUM(CASE WHEN AMT_CREDIT_SUM > 0 THEN 1 ELSE 0 END) AS CNT_CREDIT_SUM,
SUM(CASE WHEN AMT_CREDIT_SUM_DEBT > 0 THEN 1 ELSE 0 END) AS CNT_CREDIT_SUM_DEBT,
SUM(CASE WHEN AMT_CREDIT_SUM_OVERDUE > 0 THEN 1 ELSE 0 END) AS CNT_CREDIT_SUM_OVERDUE
FROM Bureau {2}
GROUP BY 1, 2, 3
), Bureau_Base_Credit AS
(
SELECT a.*,
b.CREDIT_ACTIVE, b.CREDIT_CURRENCY,
b.CNT_CREDIT_SUM, b.CNT_CREDIT_SUM_DEBT, b.CNT_CREDIT_SUM_OVERDUE,
c.DAYS_CREDIT_UPDATE, c.AMT_CREDIT_SUM,
c.AMT_CREDIT_SUM_DEBT, c.AMT_CREDIT_SUM_OVERDUE,
d.SK_ID_PREV, d.CNT_PAST_DUE,
d.CNT_PAST_DUE_DEF
FROM Base a LEFT JOIN Bureau_Trim b
ON a.SK_ID_CURR = b.SK_ID_CURR
LEFT JOIN (SELECT *
FROM Bureau_Partitioned
WHERE RANKING = 1) c
ON a.SK_ID_CURR = c.SK_ID_CURR
LEFT JOIN Credit_Trimmed d
ON a.SK_ID_CURR = d.SK_ID_CURR
)
SELECT *
FROM Bureau_Base_Credit
ORDER BY SK_ID_CURR;
'''.format(Base_Columns, Base_Table, Bureau_Trim_Filter)
return Comp_Query
# Generalized Querying Module
def get_data(Query):
'''
Function:
---------
Takes in a MySQL Query in a string format and return Pandas Data Frame.
Future Functionality:
---------------------
We will be able to change the DB and the host + port locatons.
Description:
------------
This will be an isolated instance querying function where it takes in a SQL query
and returns a Pandas Data Frame. The query should be in a string format.
'''
# Pulling in Credentials
Credentials = eval(pd.read_csv(Cred_Path, header = None).iloc[0,0])
# Establishing Connection
connector = pymysql.connect(port = Credentials['port'],
user = Credentials['user'], passwd = Credentials['pass'],
db = 'Housing', charset = 'utf8mb4')
Start = time.perf_counter()
Complete = pd.read_sql_query(Query, connector)
End = time.perf_counter()
print('This query took {0} seconds to run.'.format(round(End - Start, 2)))
connector.close()
return Complete
# Used to get the tables within a Data Base
def get_tables():
'''
Function:
---------
Takes in no inputs since we are focued on one Data Base.
Future Functionality:
---------------------
We will be able to change the DB and the host + port locatons.
Description:
------------
At the present moment, this function takes in no parameters but will return
all available tables within the Data Base in a Pandas Data Frame.
'''
# Pulling in Credentials
Credentials = eval(pd.read_csv(Cred_Path, header = None).iloc[0,0])
# Establishing Connection
connector = pymysql.connect(port = Credentials['port'],
user = Credentials['user'], passwd = Credentials['pass'],
db = 'Housing', charset = 'utf8mb4')
Tables = '''
SHOW TABLES;
'''
Table_Choices = pd.read_sql_query(Tables, connector)
connector.close()
return Table_Choices
# Extracts only the important columns that we need for table creation
def colextract(Data):
'''
Function:
---------
Takes Data Frame Object and returns a SQL format string of truncated columns.
Future Functionality:
---------------------
Flexibility to choose the columns to truncate.
Description:
------------
This will take in the columns and trim them down to the important ones.
It will return a string formated output for easy processing with SQL.
'''
Structure = pd.DataFrame(data = (list(Data.columns), list(Data.dtypes)),
index = ['colnames', 'dtypes'])
Structure = Structure.T
Structure['dtypes'] = np.where(Structure['dtypes'] == 'object', 'VARCHAR(200)', Structure['dtypes'])
Structure['dtypes'] = np.where(Structure['dtypes'] == 'float64', 'FLOAT', Structure['dtypes'])
Structure['dtypes'] = np.where(Structure['dtypes'] == 'int64', 'BIGINT', Structure['dtypes'])
Structure['Concat'] = ['{0} {1}'.format(Structure.iloc[i,0], Structure.iloc[i,1]) for i in range(len(Structure))]
Columns = str(tuple(Structure['Concat']))
return Columns.replace("'", "")
# Extracts only the important columns that we need for querying
def get_core_columns(Col_List, *argv):
'''
Function:
---------
Takes in columns dictionary generated by the get_table_keys_columns function and returns
a string of trimmed columns.
Future Functionality:
---------------------
Flexibility to choose the columns to truncate.
Description:
------------
This will take in a table name of choice and the column dictionary created by the
get_table_key_columns function and returns a stringed set of columns for the SQL query
to execute.
'''
if len(argv) > 0:
Table_Columns = Col_List[argv[0]]
else:
Table_Columns = Col_List
# Trimming Base Table by the following column names
App_Train_Trim = [i for i in Table_Columns if '_MODE' not in i]
App_Train_Trim = [i for i in App_Train_Trim if '_MEDI' not in i]
App_Train_Trim = [i for i in App_Train_Trim if 'FLAG_DOCUMENT_' not in i]
App_Train_Trim = [i for i in App_Train_Trim if 'REGION_RATING_CLIENT' != i]
Application_Column_Tapper = str(App_Train_Trim)
Application_Column_Tapper = Application_Column_Tapper.replace("'", '')\
.replace('[', '')\
.replace(']', '')
return Application_Column_Tapper
# Used to explore a table in SQL
def explore(Table):
'''
Function:
---------
Takes in Table Names in your Data Base and returns first 3 rows of data
in a Pandas Data Frame.
Future Functionality:
---------------------
We will be able to change the DB and the host + port locatons.
Description:
------------
This function will allow us to explore a table in the MySQL database without
the need to query the entire table which will take some time to retrive.
The function will return a Pandas Data Frame with 3 rows of data.
'''
# Pulling in Credentials
Credentials = eval(pd.read_csv(Cred_Path, header = None).iloc[0,0])
# Establishing Connection
connector = pymysql.connect(port = Credentials['port'],
user = Credentials['user'], passwd = Credentials['pass'],
db = 'Housing', charset = 'utf8mb4')
Query = '''
SELECT *
FROM {0}
LIMIT 3;
'''.format(Table)
explored_table = pd.read_sql_query(Query, connector)
connector.close()
return explored_table
# Grabs the keys and the columns of our table of choice
def get_table_keys_columns(Tables):
'''
Function:
---------
Takes in the table name in string format and returns the keys/columns for that table.
Future Functionality:
---------------------
Dependent of the explore function and more updates will be done on modularizing the code.
Description:
------------
This is take in a tablename, process the information within the table and return the keys and
all the columns within the data table.
'''
# Placeholder to store the keys
keys = {}
columns = {}
# Looping over all of the Data sets and grabbing the keys
for i in list(Tables['Tables_in_housing']):
columns[i] = list(explore(i).columns)
keys[i] = [j for j in columns[i] if 'SK_ID_' in j]
Key_DataFrame = pd.DataFrame(index = keys.keys(), columns = ['Key Columns'])
Key_DataFrame['Key Columns'] = list(keys.values())
return columns, keys
def get_missing(Data):
'''
Function:
---------
Takes in a Pandas Data Frame and outputs another for missing value information.
Future Functionality:
---------------------
Relatively few, should be good for now but may add more metrics for consideration.
Description:
------------
This will return a Pandas Data Frame computing missing values and displaying
data types.
'''
missing_table = pd.DataFrame(columns = ['Missing Count', 'Missing Percent', 'Data Types'],
index = list(Data.columns))
missing_table['Missing Count'] = list(Data.isnull().sum())
missing_table['Missing Percent'] = ['{0}%'.format(round((100 * i)/len(Data), 2))
for i in missing_table['Missing Count']]
missing_table['Data Types'] = list(Data.dtypes)
return missing_table.sort_values(by = 'Missing Count', ascending = False).T
def get_missing_partition(Data):
'''
Function:
---------
Takes in a Pandas Data Frame of missing values generated by the get_missing
function and returns 3 lists of missing value columns.
Future Functionality:
---------------------
Adding more data types as they come to cover more cases.
Description:
------------
This will return 3 lists of missing value column names and give us the breakdown.
The order in which it is returned is Floats, Objects, and then Integers.
'''
Missing_Floats = [Data.columns[i]
for i in range(len(Data.columns))
if Data.iloc[0,i] > 0 and 'float' in str(Data.iloc[2,i])]
Missing_Objects = [Data.columns[i]
for i in range(len(Data.columns))
if Data.iloc[0,i] > 0 and 'object' in str(Data.iloc[2,i])]
Missing_Integers = [Data.columns[i]
for i in range(len(Data.columns))
if Data.iloc[0,i] > 0 and 'int' in str(Data.iloc[2,i])]
print('''\nThere are {0} missing float, {1} missing object, and {2} missing integer columns.'''\
.format(len(Missing_Floats), len(Missing_Objects), len(Missing_Integers)))
return Missing_Floats, Missing_Objects, Missing_Integers
def fill_missing(data, column_of_interest):
'''
Function:
---------
Takes in two parameters, the Pandas DataFrame and the Column that we are
trying to correct in a string format.
Future Functionality:
---------------------
None at the present moment, but as we proceed we will think of new things
to implement.
Description:
------------
This will return a list of values that will be used to fill in the missing
terms in the Pandas DataFrame. The idea is pretty simple, sample from the
DataFrame where the values are not missing, then take those values and shift
it by a same factor and randomly subtract or add this term to the sampled
point. This will generate a new point and maintain the distribution shape.
'''
Sample = random.choices(list(data.loc[data[column_of_interest].isnull() == False,
column_of_interest]),
k = data[column_of_interest].isnull().sum())
Sign = random.choices([-2, -1, 1, 2], k = data[column_of_interest].isnull().sum())
def osicallation(point, sign, random_osicallation_point):
return (point + sign * random_osicallation_point)
Sample_Oscilated = []
for i, j in zip(Sample, Sign):
osicallation_point = random.random()
if osicallation(i, j, osicallation_point) > 0:
Sample_Oscilated.append(osicallation(i, j, osicallation_point))
else:
Sample_Oscilated.append(0)
return Sample_Oscilated
###################################################################################################################