Skip to content

Commit 1a55287

Browse files
committed
Changed all utils
1 parent 92b3b5a commit 1a55287

File tree

5 files changed

+95
-184
lines changed

5 files changed

+95
-184
lines changed

utils/GraphUtil.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
#!/usr/bin/env python2
2-
# -*- coding: utf-8 -*-
3-
"""
4-
Created on Sun Apr 15 09:05:56 2018
5-
@author: thieunv
6-
"""
1+
#!/usr/bin/env python
2+
# ------------------------------------------------------------------------------------------------------%
3+
# Created by "Thieu Nguyen" at 00:51, 29/03/2020 %
4+
# %
5+
6+
# Homepage: https://www.researchgate.net/profile/Thieu_Nguyen6 %
7+
# Github: https://github.com/thieunguyen5991 %
8+
# -------------------------------------------------------------------------------------------------------%
9+
710
#import matplotlib as mpl
811
#mpl.use('Agg')
912
import matplotlib.pyplot as plt
1013

11-
def draw_predict(fig_id=None, y_test=None, y_pred=None, filename=None, pathsave=None):
12-
plt.figure(fig_id)
14+
15+
def draw_predict(y_test=None, y_pred=None, filename=None, pathsave=None):
1316
plt.plot(y_test)
1417
plt.plot(y_pred)
1518
plt.ylabel('CPU')
@@ -19,8 +22,8 @@ def draw_predict(fig_id=None, y_test=None, y_pred=None, filename=None, pathsave=
1922
plt.close()
2023
return None
2124

22-
def draw_predict_with_error(fig_id=None, data=None, error=None, filename=None, pathsave=None):
23-
plt.figure(fig_id)
25+
26+
def draw_predict_with_error(data=None, error=None, filename=None, pathsave=None):
2427
plt.plot(data[0])
2528
plt.plot(data[1])
2629
plt.ylabel('Real value')
@@ -30,6 +33,7 @@ def draw_predict_with_error(fig_id=None, data=None, error=None, filename=None, p
3033
plt.close()
3134
return None
3235

36+
3337
def draw_raw_time_series_data(data=None, label=None, title=None, filename=None, pathsave=None):
3438
plt.plot(data)
3539
plt.xlabel(label["y"])
@@ -39,6 +43,7 @@ def draw_raw_time_series_data(data=None, label=None, title=None, filename=None,
3943
plt.close()
4044
return None
4145

46+
4247
def draw_raw_time_series_data_and_show(data=None, label=None, title=None):
4348
plt.plot(data)
4449
plt.xlabel(label["y"])

utils/IOUtil.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,80 @@
1-
#!/usr/bin/env python2
2-
# -*- coding: utf-8 -*-
3-
"""
4-
Created on Sun Apr 15 09:49:35 2018
5-
@author: thieunv
6-
"""
1+
#!/usr/bin/env python
2+
# ------------------------------------------------------------------------------------------------------%
3+
# Created by "Thieu Nguyen" at 00:51, 29/03/2020 %
4+
# %
5+
6+
# Homepage: https://www.researchgate.net/profile/Thieu_Nguyen6 %
7+
# Github: https://github.com/thieunguyen5991 %
8+
# -------------------------------------------------------------------------------------------------------%
79

810
import numpy as np
911
import pandas as pd
10-
import csv
11-
import os
12+
from csv import DictWriter
13+
from os import getcwd, path, makedirs
14+
15+
16+
def save_all_models_to_csv(item=None, log_filename=None, pathsave=None):
17+
check_directory = getcwd() + "/" + pathsave
18+
if not path.exists(check_directory):
19+
makedirs(check_directory)
20+
with open(pathsave + log_filename + ".csv", 'a') as file:
21+
w = DictWriter(file, delimiter=',', lineterminator='\n', fieldnames=item.keys())
22+
if file.tell() == 0:
23+
w.writeheader()
24+
w.writerow(item)
25+
1226

1327
def save_prediction_to_csv(y_test=None, y_pred=None, filename=None, pathsave=None):
14-
t1 = np.concatenate((y_test, y_pred), axis=1)
15-
np.savetxt(pathsave + filename + ".csv", t1, delimiter=",")
28+
check_directory = getcwd() + "/" + pathsave
29+
if not path.exists(check_directory):
30+
makedirs(check_directory)
31+
32+
temp = np.concatenate((y_test, y_pred), axis=1)
33+
np.savetxt(pathsave + filename + ".csv", temp, delimiter=",")
1634
return None
1735

36+
1837
def save_loss_train_to_csv(error=None, filename=None, pathsave=None):
1938
np.savetxt(pathsave + filename + ".csv", np.array(error), delimiter=",")
2039
return None
2140

22-
def save_all_models_to_csv(item=None, log_filename=None, pathsave=None):
23-
directory = os.getcwd() + "/" + pathsave
24-
if not os.path.exists(directory):
25-
os.makedirs(directory)
26-
with open(pathsave + log_filename + ".csv", "a+") as file:
27-
wr = csv.writer(file, dialect='excel')
28-
wr.writerow(item)
41+
42+
def load_dataset(path_to_data=None, cols=None):
43+
df = pd.read_csv(path_to_data + ".csv", usecols=cols)
44+
return df.values
45+
2946

3047
def save_run_test(num_run_test=None, data=None, filepath=None):
3148
t0 = np.reshape(data, (num_run_test, -1))
3249
np.savetxt(filepath, t0, delimiter=",")
3350

51+
3452
def load_prediction_results(pathfile=None, delimiter=",", header=None):
3553
df = pd.read_csv(pathfile, sep=delimiter, header=header)
3654
return df.values[:, 0:1], df.values[:, 1:2]
3755

56+
3857
def save_number_of_vms(data=None, pathfile=None):
3958
t0 = np.reshape(data, (-1, 1))
4059
np.savetxt(pathfile, t0, delimiter=",")
4160

61+
4262
def load_number_of_vms(pathfile=None, delimiter=",", header=None):
4363
df = pd.read_csv(pathfile, sep=delimiter, header=header)
4464
return df.values[:, 0:1]
4565

4666

47-
4867
def save_scaling_results_to_csv(data=None, path_file=None):
4968
np.savetxt(path_file + ".csv", np.array(data), delimiter=",")
5069
return None
5170

71+
5272
def read_dataset_file(filepath=None, usecols=None, header=0, index_col=False, inplace=True):
5373
df = pd.read_csv(filepath, usecols=usecols, header=header, index_col=index_col)
5474
df.dropna(inplace=inplace)
5575
return df.values
5676

77+
5778
def save_formatted_data_csv(dataset=None, filename=None, pathsave=None):
5879
np.savetxt(pathsave + filename + ".csv", dataset, delimiter=",")
5980
return None

utils/MathUtil.py

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,33 @@
1-
#!/usr/bin/env python2
2-
# -*- coding: utf-8 -*-
3-
"""
4-
Created on Sun Apr 15 10:13:13 2018
5-
@author: thieunv
6-
7-
Link : http://sci-hub.tw/10.1109/iccat.2013.6521977
8-
https://en.wikipedia.org/wiki/Laguerre_polynomials
9-
10-
"""
11-
12-
import numpy as np
1+
#!/usr/bin/env python
2+
# ------------------------------------------------------------------------------------------------------%
3+
# Created by "Thieu Nguyen" at 00:51, 29/03/2020 %
4+
# %
5+
6+
# Homepage: https://www.researchgate.net/profile/Thieu_Nguyen6 %
7+
# Github: https://github.com/thieunguyen5991 %
8+
# -------------------------------------------------------------------------------------------------------%
9+
10+
from numpy import where, maximum, power, multiply, exp
11+
from numpy import tanh as mytanh
1312

1413
def itself(x):
1514
return x
1615
def elu(x, alpha=1):
17-
return np.where(x < 0, alpha * (np.exp(x) - 1), x)
16+
return where(x < 0, alpha * (exp(x) - 1), x)
1817
def relu(x):
19-
return np.maximum(0, x)
18+
return maximum(0, x)
2019
def tanh(x):
21-
return np.tanh(x)
20+
return mytanh(x)
2221
def sigmoid(x):
23-
return 1.0 / (1.0 + np.exp(-x))
22+
return 1.0 / (1.0 + exp(-x))
2423

2524
def derivative_self(x):
2625
return 1
2726
def derivative_elu(x, alpha=1):
28-
return np.where(x < 0, x + alpha, 1)
27+
return where(x < 0, x + alpha, 1)
2928
def derivative_relu(x):
30-
return np.where(x < 0, 0, 1)
29+
return where(x < 0, 0, 1)
3130
def derivative_tanh(x):
32-
return 1 - np.power(x, 2)
31+
return 1 - power(x, 2)
3332
def derivative_sigmoid(x):
34-
return np.multiply(x, 1-x)
35-
36-
37-
def expand_chebyshev(x):
38-
x1 = x
39-
x2 = 2 * np.power(x, 2) - 1
40-
x3 = 4 * np.power(x, 3) - 3 * x
41-
x4 = 8 * np.power(x, 4) - 8 * np.power(x, 2) + 1
42-
x5 = 16 * np.power(x, 5) - 20 * np.power(x, 3) + 5 * x
43-
return np.concatenate( (x1, x2, x3, x4, x5), axis=1 )
44-
45-
def expand_legendre(x):
46-
x1 = x
47-
x2 = 1/2 * ( 3 * np.power(x, 2) - 1 )
48-
x3 = 1/2 * (5 * np.power(x, 3) - 3 * x)
49-
x4 = 1/8 * ( 35 * np.power(x, 4) - 30 * np.power(x, 2) + 3 )
50-
x5 = 1/40 * ( 9 * np.power(x, 5) - 350 * np.power(x, 3) + 75 * x )
51-
return np.concatenate((x1, x2, x3, x4, x5), axis=1 )
52-
53-
def expand_laguerre(x):
54-
x1 = -x + 1
55-
x2 = 1/2 * ( np.power(x, 2) - 4 * x + 2)
56-
x3 = 1/6 * (-np.power(x, 3) + 9 * np.power(x, 2) - 18 * x + 6)
57-
x4 = 1/24 * (np.power(x, 4) - 16 * np.power(x, 3) + 72 * np.power(x, 2) - 96*x + 24)
58-
x5 = 1/120 * (-np.power(x, 5) + 25 * np.power(x, 4) - 200 * np.power(x, 3) + 600 * np.power(x, 2) - 600 * x + 120)
59-
return np.concatenate((x1, x2, x3, x4, x5), axis=1 )
60-
61-
def expand_power(x):
62-
x1 = x
63-
x2 = x1 + np.power(x, 2)
64-
x3 = x2 + np.power(x, 3)
65-
x4 = x3 + np.power(x, 4)
66-
x5 = x4 + np.power(x, 5)
67-
return np.concatenate((x1, x2, x3, x4, x5), axis=1)
68-
69-
def expand_trigonometric(x):
70-
x1 = x
71-
x2 = np.sin(np.pi * x) + np.cos(np.pi * x)
72-
x3 = np.sin(2 * np.pi * x) + np.cos(2 * np.pi * x)
73-
x4 = np.sin(3 * np.pi * x) + np.cos(3 * np.pi * x)
74-
x5 = np.sin(4 * np.pi * x) + np.cos(4 * np.pi * x)
75-
return np.concatenate((x1, x2, x3, x4, x5), axis=1)
33+
return multiply(x, 1-x)

utils/MeasureUtil.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
#!/usr/bin/env python
2+
# ------------------------------------------------------------------------------------------------------%
3+
# Created by "Thieu Nguyen" at 00:51, 29/03/2020 %
4+
# %
5+
6+
# Homepage: https://www.researchgate.net/profile/Thieu_Nguyen6 %
7+
# Github: https://github.com/thieunguyen5991 %
8+
# -------------------------------------------------------------------------------------------------------%
9+
110
from sklearn.metrics import explained_variance_score, mean_absolute_error, mean_squared_error, mean_squared_log_error, median_absolute_error, r2_score
211
import numpy as np
312

4-
class MeasureTimeSeries(object):
13+
14+
class MeasureTimeSeries:
515
def __init__(self, y_true, y_pred, multi_output=None, number_rounding=3):
616
"""
717
:param y_true:

utils/PreprocessingUtil.py

Lines changed: 11 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,18 @@
1+
#!/usr/bin/env python
2+
# ------------------------------------------------------------------------------------------------------%
3+
# Created by "Thieu Nguyen" at 00:51, 29/03/2020 %
4+
# %
5+
6+
# Homepage: https://www.researchgate.net/profile/Thieu_Nguyen6 %
7+
# Github: https://github.com/thieunguyen5991 %
8+
# -------------------------------------------------------------------------------------------------------%
9+
110
import numpy as np
2-
from math import floor
311
from copy import deepcopy
4-
import pandas as pd
5-
import matplotlib.pyplot as plt
6-
7-
class CheckingData(object):
8-
"""
9-
Checking whether data is stationary or non-stationary (trend, seasonality, ...)
10-
https://machinelearningmastery.com/time-series-data-stationary-python/
11-
https://machinelearningmastery.com/difference-time-series-dataset-python/
12-
"""
13-
def __init__(self, pathfile=None):
14-
self.series = pd.Series.from_csv(pathfile, header=0)
15-
16-
def check_by_plot_raw_data(self):
17-
self.series.plot()
18-
plt.show()
19-
20-
def check_by_summary_statistic(self, draw_history=True):
21-
"""
22-
You can split your time series into two (or more) partitions and compare the mean and variance of each group.
23-
If they differ and the difference is statistically significant, the time series is likely non-stationary.
24-
25-
Because we are looking at the mean and variance, we are assuming that the data conforms to a Gaussian
26-
(also called the bell curve or normal) distribution. ==> Stationary
27-
"""
28-
X = self.series.values
29-
split = int(len(X) / 2)
30-
X1, X2 = X[0:split], X[split:]
31-
mean1, mean2 = X1.mean(), X2.mean()
32-
var1, var2 = X1.var(), X2.var()
33-
34-
self.series.hist()
35-
print('mean1=%f, mean2=%f' % (mean1, mean2))
36-
print('variance1=%f, variance2=%f' % (var1, var2))
3712

38-
if draw_history:
39-
self.series.hist()
40-
plt.show()
4113

42-
43-
class TimeSeries(object):
44-
def __init__(self, dataset=None, data_idx=None, sliding=None, output_index=None,
45-
method_statistic=0, minmax_scaler=None):
14+
class TimeSeries:
15+
def __init__(self, dataset=None, data_idx=None, sliding=None, output_index=None, method_statistic=0, minmax_scaler=None):
4616
'''
4717
:param data_idx:
4818
:param sliding:
@@ -199,56 +169,3 @@ def _preprocessing_3d__(self):
199169
# y_valid = y_valid.flatten()
200170
# y_test = y_test.flatten()
201171
return X_train, y_train, X_valid, y_valid, X_test, y_test, self.minmax_scaler
202-
203-
204-
def __difference__(self, interval = 1):
205-
"""
206-
:param interval: https://machinelearningmastery.com/time-series-forecasting-long-short-term-memory-network-python/
207-
:return:
208-
"""
209-
diff = list()
210-
for i in range(interval, len(self.original_dataset)):
211-
value = self.original_dataset[i] - self.original_dataset[i - interval]
212-
diff.append(value)
213-
#return Series(diff)
214-
215-
# invert differenced value
216-
def __inverse_difference__(self, history, yhat, interval=1):
217-
return yhat + history[-interval]
218-
219-
220-
class MiniBatch(object):
221-
def __init__(self, X_train, y_train, batch_size):
222-
self.X_train = X_train
223-
self.y_train = y_train
224-
self.batch_size = batch_size
225-
226-
def random_mini_batches(self, seed=None):
227-
X, Y = self.X_train.T, self.y_train.T
228-
mini_batch_size = self.batch_size
229-
230-
m = X.shape[1] # number of training examples
231-
mini_batches = []
232-
np.random.seed(seed)
233-
234-
# Step 1: Shuffle (X, Y)
235-
permutation = list(np.random.permutation(m))
236-
shuffled_X = X[:, permutation]
237-
shuffled_Y = Y[:, permutation].reshape((Y.shape[0], m))
238-
239-
# Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.
240-
num_complete_minibatches = int(floor(m / mini_batch_size)) # number of mini batches of size mini_batch_size in your partitionning
241-
for k in range(0, num_complete_minibatches):
242-
mini_batch_X = shuffled_X[:, k * mini_batch_size: (k+1) * mini_batch_size]
243-
mini_batch_Y = shuffled_Y[:, k * mini_batch_size: (k+1) * mini_batch_size]
244-
mini_batch = (mini_batch_X, mini_batch_Y)
245-
mini_batches.append(mini_batch)
246-
247-
# Handling the end case (last mini-batch < mini_batch_size)
248-
if m % mini_batch_size != 0:
249-
mini_batch_X = shuffled_X[:, num_complete_minibatches * mini_batch_size: m]
250-
mini_batch_Y = shuffled_Y[:, num_complete_minibatches * mini_batch_size: m]
251-
mini_batch = (mini_batch_X, mini_batch_Y)
252-
mini_batches.append(mini_batch)
253-
254-
return mini_batches

0 commit comments

Comments
 (0)