-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_prophet.py
64 lines (48 loc) · 2.17 KB
/
neural_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
import numpy as np
import pandas as pd
import argparse
from neuralprophet import NeuralProphet, set_random_seed
# Set seed
def set_seed_fn(seed):
np.random.seed(seed)
set_random_seed(seed)
parser = argparse.ArgumentParser()
parser.add_argument('--directory', type=str, default=".", help='Directory with the data')
parser.add_argument('--variant', type=str, default="all", help='Variant')
args = parser.parse_args()
seed = 76456357
runs = 10
# Load time series data
filename = args.directory + "neural_prophet_data_" + args.variant + ".csv"
data = pd.read_csv(filename)
forecast_7 = pd.DataFrame(np.zeros(7, dtype=float))
forecast_14 = pd.DataFrame(np.zeros(14, dtype=float))
forecast_21 = pd.DataFrame(np.zeros(21, dtype=float))
forecast_28 = pd.DataFrame(np.zeros(28, dtype=float))
for run in range(runs):
# Set seed
set_seed_fn(seed + run)
# Build neuralProphet model
m = NeuralProphet(growth = "linear")
m.fit(data)
df_future_7 = m.make_future_dataframe(data, periods=7)
df_future_14 = m.make_future_dataframe(data, periods=14)
df_future_21 = m.make_future_dataframe(data, periods=21)
df_future_28 = m.make_future_dataframe(data, periods=28)
# Forecasting for the desired periods
forecast_7_local = m.predict(df_future_7)
forecast_14_local = m.predict(df_future_14)
forecast_21_local = m.predict(df_future_21)
forecast_28_local = m.predict(df_future_28)
forecast_7 = forecast_7 + pd.DataFrame(np.array(forecast_7_local.loc[:, "yhat1"]))
forecast_14 = forecast_14 + pd.DataFrame(np.array(forecast_14_local.loc[:, "yhat1"]))
forecast_21 = forecast_21 + pd.DataFrame(np.array(forecast_21_local.loc[:, "yhat1"]))
forecast_28 = forecast_28 + pd.DataFrame(np.array(forecast_28_local.loc[:, "yhat1"]))
forecast_7 = forecast_7 / runs
forecast_14 = forecast_14 / runs
forecast_21 = forecast_21 / runs
forecast_28 = forecast_28 / runs
forecast_7.to_csv(args.directory + "forecast_7_" + args.variant + ".csv")
forecast_14.to_csv(args.directory + "forecast_14_" + args.variant + ".csv")
forecast_21.to_csv(args.directory + "forecast_21_" + args.variant + ".csv")
forecast_28.to_csv(args.directory + "forecast_28_" + args.variant + ".csv")