-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaly.py
149 lines (117 loc) · 4.44 KB
/
anomaly.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
#%%
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import seaborn as sns
from pylab import rcParams
import matplotlib.pyplot as plt
from matplotlib import rc
from pandas.plotting import register_matplotlib_converters
%matplotlib inline
%config InlineBackend.figure_format='retina'
register_matplotlib_converters()
sns.set(style='whitegrid', palette='muted', font_scale=1.5)
rcParams['figure.figsize'] = 30, 15
np.random.seed(101)
tf.random.set_seed(101)
path = r'C:\Users\jmruizr\Downloads\^GSPC (3).csv'
data = pd.read_csv(path, parse_dates=['Date'], index_col='Date')
df = data['Close']
df = pd.DataFrame(df)
#%%
class AnomalyDetection():
def __init__(self,dataset):
self._dataset = dataset
def fillna(self, metodo):
self._dataset = self._dataset.fillna(method= metodo)
def first_plot(self,etiqueta,colore,titulo):
plt.plot(self._dataset, label=etiqueta,color=colore)
plt.title(titulo)
plt.legend()
def split_train_test(self,partition):
train_sz = int(len(self._dataset) * partition)
test_sz = len(self._dataset) - train_sz
train, test = self._dataset.iloc[0:train_sz], df.iloc[train_sz:len(self._dataset)]
print(train.shape, test.shape)
return train,test
def standar_scaler(self,train,test):
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler = scaler.fit(train[['Close']])
train['Close'] = scaler.transform(train[['Close']])
test['Close'] = scaler.transform(test[['Close']])
return train, test, scaler
def create_dataset(self,X, y, time_steps=1):
Xs, ys = [], []
for i in range(len(X) - time_steps):
v = X.iloc[i:(i + time_steps)].values
Xs.append(v)
ys.append(y.iloc[i + time_steps])
return np.array(Xs), np.array(ys)
def model_arquitecture(self,unids,X_train):
model = keras.Sequential()
model.add(keras.layers.LSTM(
units=64,
input_shape=(X_train.shape[1], X_train.shape[2])
))
model.add(keras.layers.Dropout(rate=0.2))
model.add(keras.layers.RepeatVector(n=X_train.shape[1]))
model.add(keras.layers.LSTM(units=64, return_sequences=True))
model.add(keras.layers.Dropout(rate=0.2))
model.add(keras.layers.TimeDistributed(keras.layers.Dense(units=X_train.shape[2])))
model.compile(loss='mae', optimizer='Adam')
return model
def plot_results(self,history):
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.legend()
# %%
A = AnomalyDetection(df)
A.fillna('ffill')
A.first_plot('close price','blue','S&P 500')
trai_, test_ = A.split_train_test(0.90)
train_sc, test_sc, scaler = A.standar_scaler(trai_,test_)
#%%
train_X, train_y = A.create_dataset(train_sc[['Close']], train_sc.Close,30)
test_X, test_y = A.create_dataset(test_sc[['Close']], test_sc.Close,30)
model = A.model_arquitecture(64,train_X)
history = model.fit(train_X, train_y, epochs=10,batch_size=32,validation_split=0.1,shuffle=False)
A.plot_results(history)
train_X_prediction = model.predict(train_X)
train_mae_loss = np.mean(np.abs(train_X_prediction - train_X), axis=1)
sns.distplot(train_mae_loss, bins=50, kde=True);
test_X_prediction = model.predict(test_X)
test_mae_loss = np.mean(np.abs(test_X_prediction - test_X), axis=1)
#%%
Anomaly_score = 0.75 #Threshold
test_score = pd.DataFrame(index=test_sc[30:].index)
test_score['loss'] = test_mae_loss
test_score['threshold'] = Anomaly_score
test_score['anomaly'] = test_score.loss > test_score.threshold
test_score['Close'] = test_sc[30:].Close
# %%
plt.plot(test_score.index, test_score.loss, label='loss')
plt.plot(test_score.index, test_score.threshold, label='threshold')
plt.xticks(rotation=25)
plt.legend();
# %%
anomalies = test_score[test_score.anomaly == True]
anomalies.head()
# %%
plt.plot(
test_sc[30:].index,
scaler.inverse_transform(test_sc[30:].Close),
label='close price'
);
sns.scatterplot(
anomalies.index,
scaler.inverse_transform(anomalies.Close),
color=sns.color_palette()[3],
s=52,
label='anomaly'
)
plt.xticks(rotation=25)
plt.legend();
# %%