Spaces:
Sleeping
Sleeping
Daniel Varga
commited on
Commit
•
3fd575d
1
Parent(s):
83ff599
neuralforecast works, statsforecast placeholder is copy of neuralforecast.
Browse files
v2/test_predictor_neuralforecast.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
|
4 |
+
from neuralforecast import NeuralForecast
|
5 |
+
from neuralforecast.models import LSTM, NHITS, RNN, PatchTST
|
6 |
+
|
7 |
+
|
8 |
+
data = pd.read_csv("terheles_fixed.tsv", sep="\t")
|
9 |
+
data['ds'] = pd.to_datetime(data['Korrigált időpont'])
|
10 |
+
data['y'] = data['Hatásos teljesítmény']
|
11 |
+
|
12 |
+
data = data[['ds', 'y']]
|
13 |
+
data['unique_id'] = 1
|
14 |
+
|
15 |
+
data = data[data['ds'] < '2019-09-01']
|
16 |
+
Y_df = data
|
17 |
+
print(Y_df)
|
18 |
+
|
19 |
+
|
20 |
+
horizon = 4 * 24 * 7 # 7 days
|
21 |
+
|
22 |
+
# Try different hyperparmeters to improve accuracy.
|
23 |
+
models = [
|
24 |
+
PatchTST(h=horizon, # Forecast horizon
|
25 |
+
input_size=2 * horizon, # Length of input sequence
|
26 |
+
max_steps=20, # Number of steps to train
|
27 |
+
scaler_type='standard'), # Type of scaler to normalize data
|
28 |
+
|
29 |
+
NHITS(h=horizon, # Forecast horizon
|
30 |
+
input_size=2 * horizon, # Length of input sequence
|
31 |
+
max_steps=100, # Number of steps to train
|
32 |
+
n_freq_downsample=[2, 1, 1]) # Downsampling factors for each stack output
|
33 |
+
]
|
34 |
+
'''
|
35 |
+
LSTM(h=horizon, # Forecast horizon
|
36 |
+
max_steps=100, # Number of steps to train
|
37 |
+
scaler_type='standard', # Type of scaler to normalize data
|
38 |
+
encoder_hidden_size=64, # Defines the size of the hidden state of the LSTM
|
39 |
+
decoder_hidden_size=64,), # Defines the number of hidden units of each layer of the MLP decoder
|
40 |
+
'''
|
41 |
+
|
42 |
+
|
43 |
+
nf = NeuralForecast(models=models, freq='15min')
|
44 |
+
|
45 |
+
shorter_Y_df = Y_df[Y_df['ds'] < '2019-08-01']
|
46 |
+
print("-=======-")
|
47 |
+
print(len(shorter_Y_df))
|
48 |
+
print(shorter_Y_df)
|
49 |
+
nf.fit(df=shorter_Y_df)
|
50 |
+
|
51 |
+
Y_hat_df = nf.predict()
|
52 |
+
|
53 |
+
print(Y_df)
|
54 |
+
|
55 |
+
Y_hat_df = Y_hat_df.reset_index()
|
56 |
+
|
57 |
+
fig, ax = plt.subplots(1, 1, figsize = (20, 7))
|
58 |
+
|
59 |
+
# plot_df = pd.concat([Y_df, Y_hat_df]).set_index('ds') # Concatenate the train and forecast dataframes
|
60 |
+
# plot_df[['y', 'LSTM', 'NHITS']].plot(ax=ax, linewidth=2)
|
61 |
+
|
62 |
+
plot_Y_df = Y_df[Y_df['ds'] > '2019-07-01']
|
63 |
+
plot_Y_df = plot_Y_df.set_index('ds')[['y']]
|
64 |
+
plot_Y_df.plot(ax=ax, linewidth=1)
|
65 |
+
Y_hat_df.set_index('ds')[['PatchTST', 'NHITS']].plot(ax=ax, linewidth=1)
|
66 |
+
|
67 |
+
|
68 |
+
ax.set_title('AirPassengers Forecast', fontsize=22)
|
69 |
+
ax.set_ylabel('Monthly Passengers', fontsize=20)
|
70 |
+
ax.set_xlabel('Timestamp [t]', fontsize=20)
|
71 |
+
ax.legend(prop={'size': 15})
|
72 |
+
ax.grid()
|
73 |
+
|
74 |
+
plt.savefig("neuralforecast.pdf")
|
v2/test_predictor_statsforecast.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
|
4 |
+
from neuralforecast import NeuralForecast
|
5 |
+
from neuralforecast.models import LSTM, NHITS, RNN, PatchTST
|
6 |
+
|
7 |
+
|
8 |
+
data = pd.read_csv("terheles_fixed.tsv", sep="\t")
|
9 |
+
data['ds'] = pd.to_datetime(data['Korrigált időpont'])
|
10 |
+
data['y'] = data['Hatásos teljesítmény']
|
11 |
+
|
12 |
+
data = data[['ds', 'y']]
|
13 |
+
data['unique_id'] = 1
|
14 |
+
|
15 |
+
data = data[data['ds'] < '2019-09-01']
|
16 |
+
Y_df = data
|
17 |
+
print(Y_df)
|
18 |
+
|
19 |
+
|
20 |
+
horizon = 4 * 24 * 7 # 7 days
|
21 |
+
|
22 |
+
# Try different hyperparmeters to improve accuracy.
|
23 |
+
models = [
|
24 |
+
PatchTST(h=horizon, # Forecast horizon
|
25 |
+
input_size=2 * horizon, # Length of input sequence
|
26 |
+
max_steps=20, # Number of steps to train
|
27 |
+
scaler_type='standard'), # Type of scaler to normalize data
|
28 |
+
|
29 |
+
NHITS(h=horizon, # Forecast horizon
|
30 |
+
input_size=2 * horizon, # Length of input sequence
|
31 |
+
max_steps=100, # Number of steps to train
|
32 |
+
n_freq_downsample=[2, 1, 1]) # Downsampling factors for each stack output
|
33 |
+
]
|
34 |
+
'''
|
35 |
+
LSTM(h=horizon, # Forecast horizon
|
36 |
+
max_steps=100, # Number of steps to train
|
37 |
+
scaler_type='standard', # Type of scaler to normalize data
|
38 |
+
encoder_hidden_size=64, # Defines the size of the hidden state of the LSTM
|
39 |
+
decoder_hidden_size=64,), # Defines the number of hidden units of each layer of the MLP decoder
|
40 |
+
'''
|
41 |
+
|
42 |
+
|
43 |
+
nf = NeuralForecast(models=models, freq='15min')
|
44 |
+
|
45 |
+
shorter_Y_df = Y_df[Y_df['ds'] < '2019-08-01']
|
46 |
+
print("-=======-")
|
47 |
+
print(len(shorter_Y_df))
|
48 |
+
print(shorter_Y_df)
|
49 |
+
nf.fit(df=shorter_Y_df)
|
50 |
+
|
51 |
+
Y_hat_df = nf.predict()
|
52 |
+
|
53 |
+
print(Y_df)
|
54 |
+
|
55 |
+
Y_hat_df = Y_hat_df.reset_index()
|
56 |
+
|
57 |
+
fig, ax = plt.subplots(1, 1, figsize = (20, 7))
|
58 |
+
|
59 |
+
# plot_df = pd.concat([Y_df, Y_hat_df]).set_index('ds') # Concatenate the train and forecast dataframes
|
60 |
+
# plot_df[['y', 'LSTM', 'NHITS']].plot(ax=ax, linewidth=2)
|
61 |
+
|
62 |
+
plot_Y_df = Y_df[Y_df['ds'] > '2019-07-01']
|
63 |
+
plot_Y_df = plot_Y_df.set_index('ds')[['y']]
|
64 |
+
plot_Y_df.plot(ax=ax, linewidth=1)
|
65 |
+
Y_hat_df.set_index('ds')[['PatchTST', 'NHITS']].plot(ax=ax, linewidth=1)
|
66 |
+
|
67 |
+
|
68 |
+
ax.set_title('AirPassengers Forecast', fontsize=22)
|
69 |
+
ax.set_ylabel('Monthly Passengers', fontsize=20)
|
70 |
+
ax.set_xlabel('Timestamp [t]', fontsize=20)
|
71 |
+
ax.legend(prop={'size': 15})
|
72 |
+
ax.grid()
|
73 |
+
|
74 |
+
plt.savefig("neuralforecast.pdf")
|