-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTS_transformations.py
100 lines (73 loc) · 2.49 KB
/
TS_transformations.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
import numpy as np
import matplotlib.pyplot as plt
class TSTransformations:
data = -1
# init function
def __init__(self, data):
self.data = data
# remove trend from TS variable
def de_trending(self, variable, plot=False):
from sklearn.linear_model import LinearRegression
total = []
x = [i for i in range(0, len(self.data[variable]))]
x = np.reshape(x, (len(x), 1))
y = self.data[variable].values
model = LinearRegression()
model.fit(x, y)
# calculate trend
trend = model.predict(x)
# de_trend
de_trended = [y[i] - trend[i] for i in range(0, len(self.data[variable]))]
[total.append(i) for i in de_trended]
if plot:
plt.plot(y)
plt.plot(trend)
plt.show()
plt.plot(de_trended)
plt.show()
return total
# calculate log of TS
def log(self, variable, plot=False):
log_trans = np.log(self.data[variable])
if plot:
plt.plot(self.data[variable])
plt.show()
plt.plot(log_trans)
plt.show()
return list(log_trans)
# Box-Cox transformation
def box_cox(self, variable, lmbda, plot=False):
from scipy.stats import boxcox
transformed = boxcox(self.data[variable], lmbda=lmbda)
if plot:
plt.plot(self.data[variable])
plt.show()
plt.plot(transformed)
plt.show()
return list(transformed)
# smooth of the TS, to reduce noise
def smoothing(self, variable, window, plot=False):
rolling_mean = self.data[variable].rolling(window=window, min_periods=1).mean()
if plot:
plt.plot(rolling_mean)
self.data[variable].plot()
plt.show()
return list(rolling_mean)
# power transformations
def power(self, variable, power, plot=False):
values = self.data[variable].values
transformation = [i**power for i in values]
if plot:
plt.plot(transformation)
plt.plot(values)
plt.show()
return list(transformation)
# exponential transformation
def exponential(self, variable, numerator, plot=False):
values = self.data[variable].values
transformation = [np.exp(numerator/i) for i in values]
if plot:
plt.plot(transformation)
plt.plot(values)
plt.show()
return transformation