-
Notifications
You must be signed in to change notification settings - Fork 0
/
prophet.py
executable file
·163 lines (134 loc) · 6.11 KB
/
prophet.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
import pandas as pd
from fbprophet import Prophet
import matplotlib.pyplot as plt
from fbprophet.diagnostics import cross_validation,performance_metrics
import numpy as np
import sys
import config as cf
from decorator import suppress_stdout_stderr
from utils import incGeneralStats
pd.options.mode.chained_assignment = None
statsProphet = {"general":{}, "host":{}}
def AnomalyChecker(actual,predicted,hostProphet, client,categories,metric,influxQuery):
t_type = hostProphet.getMeasure()
host_mac = hostProphet.getIP()
ifid = hostProphet.getIFid()
incGeneralStats(statsProphet, host_mac, t_type, "ip")
checked=''
if actual['y'] > predicted['yhat_upper'] or actual['y'] < predicted['yhat_lower']:
if cf.checkCat:
if(influxQuery(client, actual['ds'], categories, ifid, host_mac, metric)>0):
checked='+NDPI'
if(not cf.checkCat or checked == '+NDPI'):
statsProphet["general"][t_type]['anomalies']+=1
statsProphet["host"][host_mac][t_type]['anomalies']+=1
if not (hostProphet.isAnomalous() and cf.verbose):
print("%-8s %-18s %-25s %-5s %-25s %-16s\n"
% ("START",t_type, host_mac, ifid, actual['ds'],"PROPHET"+checked),end='',flush=True)
hostProphet.setAnomalous(True)
elif hostProphet.isAnomalous() and cf.verbose:
if cf.checkCat:
meth = "PROPHET+NDPI"
else:
meth = "PROPHET"
print("%-8s %-18s %-25s %-5s %-25s %-16s\n"
% ("END",t_type, host_mac,ifid,actual['ds'],meth),end='',flush=True)
hostProphet.setAnomalous(False)
def isWeekendDay(ds):
date = pd.to_datetime(ds)
return date.dayofweek >= 5
def noNegative(val):
val = val if val >=0 else 0
return val
def rmse(y_true, y_pred):
y_true, y_pred = np.array(y_true), np.array(y_pred)
return np.sqrt(np.mean((y_true - y_pred) ** 2))
def dateConverter(ds):
return pd.to_datetime(ds)
def prophet(df,dimVL,frequency, hostProphet, client, categories, metric, influxQuery, showGraph=False):
df['weekend'] = df['ds'].apply(isWeekendDay)
df['no_weekend'] = ~df['ds'].apply(isWeekendDay)
df_training = df[:-(dimVL+cf.predictedPoint)]
df_test = df[-(dimVL+cf.predictedPoint):-cf.predictedPoint]
df_test['ds'] = df_test['ds'].apply(dateConverter)
italianHolidays2019 = pd.DataFrame({
'holiday': 'italianHolidays2019',
'ds': pd.to_datetime(['2019-01-01', '2019-01-06', '2019-04-21',
'2019-04-22','2019-04-25','2019-05-01',
'2019-06-02','2019-08-15','2019-11-01','2019-12-08',
'2019-12-24','2019-12-25','2019-12-26','2019-12-31'])
})
if (hostProphet.getTrend() == None or hostProphet.getTotalCheck() == cf.validationTime):
fr, cp, ss = modelSelection(df_training, df_test, min(2*dimVL,cf.dim_vlset),
dimVL,italianHolidays2019,frequency)
hostProphet.setTrend(cp)
hostProphet.setFourier(fr)
hostProphet.setSeasonality(ss)
hostProphet.resetTotalCheck()
fcst,m = fit_predict(df[:-cf.predictedPoint],italianHolidays2019,
hostProphet.getFourier(), hostProphet.getTrend(), hostProphet.getSeasonality() ,cf.totPredPoint,frequency)
j=cf.predictedPoint
i=cf.totPredPoint
point = []
while i > 0 and j > 0:
if(pd.to_datetime(df.iloc[-j]['ds']) == fcst.iloc[-i]['ds']):
point.append(df.iloc[-j])
AnomalyChecker(df.iloc[-j], fcst.iloc[-i], hostProphet, client,categories,metric,influxQuery)
i-=1
j-=1
elif pd.to_datetime(df.iloc[-j]['ds']) > fcst.iloc[-i]['ds']:
i-=1
else:
j-=1
if(showGraph):
m.plot(fcst)
plt.plot([pd.to_datetime(point[i]['ds']) for i in range(0,len(point))],
[point[i]['y'] for i in range(0,len(point))],"ro-",ms=3)
plt.savefig(cf.graphDir+ hostProphet.getIP() +"_"+ hostProphet.getMeasure() +".png")
plt.close()
def modelSelection(df_training, df_test,dimTotTest,dimTest, holiday,frequency):
fr_hpar = [(7,13)]
cp_hpar = [0.05, 0.2]
ss_hpar = [10, 25]
b_rmse = float('inf')
for i in range(len(fr_hpar)):
for j in range (len(cp_hpar)):
for k in range (len(ss_hpar)):
fcst,_ = fit_predict(df_training,holiday,fr_hpar[i],cp_hpar[j],ss_hpar[k],dimTest,frequency)
test, pred = checkDate(df_test,fcst[-dimTotTest:],dimTotTest, dimTest)
rmse_val = rmse(test, pred)
if (rmse_val < b_rmse):
b_hpar = [i,j,k]
b_rmse = rmse_val
return fr_hpar[b_hpar[0]], cp_hpar[b_hpar[1]], ss_hpar[b_hpar[2]]
def checkDate(df_test, df_fc, dimTotTest, dimTest):
j = dimTest
i = dimTotTest
test = []
pred = []
while i > 0 and j > 0:
if(df_test.iloc[-j]['ds'] == df_fc.iloc[-i]['ds']):
test.append(df_test.iloc[-j]['y'])
pred.append(df_fc.iloc[-i]['yhat'])
i-=1
j-=1
elif pd.to_datetime(df_test.iloc[-j]['ds']) > df_fc.iloc[-i]['ds']:
i-=1
else:
j-=1
return (test,pred)
def fit_predict(df_training, holiday, fr, cp, ss, dimPred, frequency):
m = Prophet(holidays=holiday, interval_width=0.99, yearly_seasonality=False, weekly_seasonality=fr[1],
daily_seasonality=False, changepoint_prior_scale=cp, holidays_prior_scale=15,
seasonality_prior_scale=ss, seasonality_mode='multiplicative')
m.add_seasonality(name='work_days', period=1, fourier_order=fr[0], condition_name='no_weekend')
m.add_seasonality(name='weekend_days', period=1, fourier_order=fr[0], condition_name='weekend')
with suppress_stdout_stderr():
m.fit(df_training)
future = m.make_future_dataframe(periods=dimPred, freq=frequency)
future['weekend'] = future['ds'].apply(isWeekendDay)
future['no_weekend'] = ~future['ds'].apply(isWeekendDay)
fcst = m.predict(future)
fcst['yhat'] = fcst['yhat'].apply(noNegative)
fcst['yhat_lower'] = fcst['yhat_lower'].apply(noNegative)
return fcst, m