Is “results” a three-dimensional numpy array? If so, could you explain why it’s a three-dimensional array? And why using [:, 0, 0] here?
Thank you!
Is “results” a three-dimensional numpy array? If so, could you explain why it’s a three-dimensional array? And why using [:, 0, 0] here?
Thank you!
Hi @XiyingLiu,
This is how I see it:
forecast is a list of arrays with shape (1,1). The predict() function accepts only a single argument which is usually the data to be tested and returns a numpy array(s) of predictions. In our case, just one value as a (1,1) array. Then when you finish that for loop, you have a list with range(len(series) - window_size) elements (1,1).
From that list, you are only interested on those from split_time-window_size to the end. Let’s define #records as this list length.
Now the tricky part. You flatten the list to an array in order to plot it.
np.array(forecast).shape # List is converted to 3D array with shape (#records,1,1)
np.array(forecast)[:, 0].shape # 2D array with shape (#records,1)
np.array(forecast)[:, 0, 0].shape # 1D array with shape (#records,)
Hope it’s not too confusing…
Best,
Many thanks! You helped a lot!
Two follow-up questions please:
If we are only interested in those [split_time-window_size: ], why don’t we just write this in the for loop? Like for time in range(split_time-window_size_
. The for loop on the training set doesn’t generate any results that we will use, seems a bit waste of time?
Let’s take another example to test, if the forecast is a list of arrays with shape (1, 24), is my understanding below correct?
np.array(forecast).shape # 3D array with shape (#records, 1, 24)
np.array(forecast)[:, 0].shape # 2D array with shape (#records, 1)
np.array(forecast)[:, 0, 0].shape # 1D array with shape (#records, )
Hi @XiyingLiu,
That’s a very good point. I’ve made some changes to the code:
forecast = []
for time in range(split_time-window_size, len(series) - window_size):
forecast.append(model.predict(series[time:time + window_size][np.newaxis]))
results = np.array(forecast)[:, 0, 0]
plt.figure(figsize=(10, 6))
plot_series(time_valid, x_valid)
plot_series(time_valid, results)
…and result is obviously the same:
About your second point, not really:
np.array(forecast).shape # List is converted to 3D array with (#records,1,1,24)
np.array(forecast)[:, 0].shape # 2D array with shape (#records, 1, 24)
np.array(forecast)[:, 0, 0].shape # 1D array with shape (#records, 24)
Try this code to see my point:
forecast = []
for time in range(split_time-window_size, len(series) - window_size):
forecast.append(np.ones((1,24))[np.newaxis])
print(np.array(forecast).shape)
print(np.array(forecast)[:, 0].shape)
print(np.array(forecast)[:, 0, 0].shape)
Best,
@german.mesa
Thank you so much! For updating the codes and giving a new example
After playing around with this example with different shapes, I think I finally got your point.
And, here comes another question: if we don’t add np.newaxis
after forecast.append()
, then we can just use results = np.array(forecast)[:, 0]
to get the flattened results. Am I right? If yes, why did we use it?
Thank you.
Nice try!
Unfortunately, prediction method uses an array of arrays or list of arrays as input. So we have no chance but keeping it - otherwise we would get an error.
What you can try to do is to predict by batches…
times = []
forecast = []
for time in range(split_time-window_size, len(series) - window_size):
times.append(series[time:time + window_size])
forecast = model.predict(np.array(times))
results = np.array(forecast)[:, 0]
Result is the same as can be expected:
At the end we are going to recode it all…
Best,
Nice, thanks for the workaround! @german.mesa
I think my questions have been very well answered, we can take a break, for now …
Cheers,