-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformations.py
More file actions
301 lines (235 loc) · 7.57 KB
/
Copy pathtransformations.py
File metadata and controls
301 lines (235 loc) · 7.57 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
# -*- coding: utf-8 -*-
import sklearn.preprocessing as preprocessing
import scipy.stats as stats
from scipy.special import inv_boxcox
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import csv
import pandas as pd
import read_db
accountUsageDict = dict()
try:
with open('normUsageDict.csv', 'r') as file:
accountUsageDict['root'] = 1.0
reader = csv.reader(file, delimiter='|')
for line in list(reader)[1:]:
accountUsageDict[line[0]] = float(line[1])
except:
pass
def normalize(train_data, test_data):
"""
normalize
Parameters
----------
train_data : ARRAY
Array of training data. Used to fit the standard scaler.
test_data : ARRAY
Array of testing data.
Returns
-------
ARRAY, ARRAY
Returns two arrays, each standardized according to train_data.
"""
scaler = preprocessing.MinMaxScaler()
scaler.fit(train_data)
return scaler.transform(train_data), scaler.transform(test_data)
def standardize(train_data, test_data):
"""
standardize
Parameters
----------
train_data : ARRAY
Array of training data. Used to fit the standard scaler.
test_data : ARRAY
Array of testing data.
Returns
-------
ARRAY, ARRAY
Returns two arrays, each standardized according to train_data.
"""
scaler = preprocessing.StandardScaler()
scaler.fit(train_data)
return scaler.transform(train_data), scaler.transform(test_data)
def memory_to_gigabytes(memory_str):
"""
memory_to_gigabytes
Parameters
----------
memory_str : STRING
String in format xxxxU where xxxx is a number and U is a character
representing the unit of memory used.
Raises
------
ValueError
Unit of measurement is not recognized.
Returns
-------
FLOAT
Returns a float representing the memory of memory_str converted to
gigabytes.
"""
if memory_str.endswith('T'):
return float(memory_str[:-1]) * 1024
elif memory_str.endswith('G'):
return float(memory_str[:-1])
elif memory_str.endswith('M'):
return float(memory_str[:-1]) / 1024
elif memory_str == "0":
return float(0)
else:
raise ValueError(f"Unknown memory unit in {memory_str}")
def time_to_seconds(time_str):
"""
time_to_seconds
Parameters
----------
time_str : STRING
String in the format D-H:M:S or H:M:S where D represents time in
days, H represents time in hours, M represents time in minutes, and
S represents time in seconds.
Returns
-------
INT
Returns the time in seconds of time_str.
"""
if '-' in time_str:
d_hms = time_str.split('-')
days = int(d_hms[0])
h, m, s = map(int, d_hms[1].split(':'))
else:
days = 0
h, m, s = map(int, time_str.split(':'))
return days * 86400 + h * 3600 + m * 60 + s
def boxcox(train_data, test_data):
"""
Fits a Box-Cox transformation to train_data and then applies it to
train_data and test_data.
Parameters
----------
train_data : ARRAY TYPE
Array of data which will serve to fit and be applied by the
Box-Cox transformation.
test_data : ARRAY TYPE
Array of data which will have the Box-Cox transformation applied to
it with the lambda value resulting from train_data.
Returns
-------
train_data_transformed : ARRAY TYPE
Transformed version of train_data.
test_data_transformed : ARRAY TYPE
Transformed version of test_data.
"""
train_data_transformed, fit_lambda = stats.boxcox(train_data)
test_data_transformed = stats.boxcox(test_data, lmbda=fit_lambda)
return train_data_transformed, test_data_transformed, fit_lambda
def inverse_boxcox(data, lmbda):
"""
Wrapper for scipy.stats.inv_boxcox() function
Parameters
----------
data : ARRAY OR FLOAT TYPE
Number or set of numbers to be converted back to their pre-transformed
value.
lmbda : FLOAT
Float ranging from -5.0 to 5.0 as a result of applying the Box-Cox
transformation.
Returns
-------
ARRAY OR FLOAT TYPE
Positive number or set of numbers as a result of reversing the transformation
for the given lambda.
"""
return inv_boxcox(data, lmbda)
def scale_min_max(X_train, X_test):
"""
Applys a min max scaler to X_train and X_test fit to X_trian
Parameters
----------
X_train : ARRAY TYPE
Array of floats.
X_test : ARRAY TYPE
Array of floats.
Returns
-------
X_train : ARRAY TYPE
Array of floats scaled between 0 and 1.
X_test : ARRAY TYPE
Array of floats roughly scaled between 0 and 1.
"""
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
return X_train, X_test
def scale_min_max_test(X_test):
scaler = MinMaxScaler()
scaler.fit(X_test)
X_test = scaler.transform(X_test)
return X_test
def scale_log(X_train, X_test, threshold=300):
variances = np.var(X_train, axis=0)
high_variance_columns = variances > threshold
X_train[:, high_variance_columns] = np.log1p(X_train[:, high_variance_columns])
X_test[:, high_variance_columns] = np.log1p(X_test[:, high_variance_columns])
return X_train, X_test
def scale_log_test(X_test):
# if min(X_test) == 0:
X_test += 1
return np.log(X_test)
def accountToNormUsage(account: str):
"""
accountToNormUsage
Should be used with df apply
Ex: df.apply(accountToNormUsage)
Parameters
----------
account : str
String representation of account.
Returns
-------
Normalized value representing the proprotion of resources used by said account.
"""
if account in accountUsageDict.keys():
return accountUsageDict[account]
print(account)
return 0.0
def make_one_hot(df, col_name, new_col_limit=0):
"""
make_one_hot
Converts the values within the column matching col_name to onehot encoded values,
drops the original column, and appends the new one hot columns to the
dataframe before returning it.
Parameters
----------
df : DATAFRAME
Dataframe containing column matching col_name.
col_name : STRING
String representing the name of the column to convert to one hot encoding.
new_col_limit : INT
Number of one hot columns to create not including other. If new_col_limit
is 0, includes all columns and does not create an other column.
Returns
-------
df : DATAFRAME
Updated dataframe containing one hot encoded values.
"""
# TODO: Potentially change all values of 'dmachi' in qos to 'other'
# as the last dmachi was in 2023-08-17. Outdated feature
value_counts = df[col_name].value_counts()
if new_col_limit:
top_categories = value_counts.nlargest(new_col_limit).index.tolist()
else:
top_categories = value_counts.index.tolist()
other_name = 'other'
df[col_name] = df[col_name].apply(lambda x: x if x in top_categories else other_name)
one_hot = pd.get_dummies(df[col_name])
one_hot = one_hot.add_prefix(col_name + "_")
df = df.join(one_hot)
df = df.replace({True: 1, False: 0})
return df
if __name__ == "__main__":
# Just used to test some functions
df = read_db.read_to_df("jobs_all_2", False, 100000)
df = make_one_hot(df, "partition")
df = make_one_hot(df, "qos", 4)
print(df.columns)