2
$\begingroup$

Here I want predict value every 60 minutes. So I have data 540 with three inputs. so I wrote an code with time steps and it gave me this error. Can anyone help me to solve this problem?

my code :

y=data['y1'].astype(int)
cols=['x1', 'x2', 'x3']
x=data[cols].astype(int)
n = x.shape[0]
p = x.shape[1]
x  = x.values
y = y.values
train_start = 0
train_end = int(np.floor(0.8*n))
test_start = train_end+1 
test_end = n
x_train = x[np.arange(train_start, train_end), :]
x_test = x[np.arange(test_start, test_end), :]
y_train = y[np.arange(train_start, train_end), :]
y_test = y[np.arange(test_start, test_end), :]
x_train=x_train.reshape(x_train.shape +(1,))
x_test=x_test.reshape(x_test.shape + (1,))

num_time_steps = 9
num_features = x.shape[1]

x_train = np.zeros((x_train.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")
x_test = np.zeros((x_test.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")

for i in range(len(x_train)):
for timestep in range(num_time_steps):
    x_train[i][timestep] = x_train[i + timestep]

for i in range(len(x_test)):
for timestep in range(num_time_steps):
    x_test[i][timestep] = x_test[i + timestep]

 y_train = y_train[num_time_steps - 1:]
 y_test = y_test[num_time_steps - 1:]

enter image description here

enter image description here

change code:

train_end = 80
x_train=x[0: train_end ,]
x_test=x[train_end +1: ,]
y_train=y[0: train_end]
y_test=y[train_end +1:] 
x_train=x_train.reshape(x_train.shape +(1,))
x_test=x_test.reshape(x_test.shape + (1,))
num_time_steps = 9
num_features = x.shape[1]
x_train_n = np.zeros((x_train.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")
x_test_n = np.zeros((x_test.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")
for i in range(len(x_train_n)):
for timestep in range(num_time_steps):
    x_train_n[i][timestep] = x_train[i + timestep]
for i in range(len(x_test_n)):
for timestep in range(num_time_steps):
    x_test_n[i][timestep] = x_test[i+timestep]
y_train_n = y_train[num_time_steps - 1:]
y_test_n = y_test[num_time_steps - 1:]

Error, enter image description here

$\endgroup$
  • 1
    $\begingroup$ can you share the exception message, and the line which error occurs? $\endgroup$ – gunes Feb 12 at 10:32
  • 1
    $\begingroup$ @gunes I paste the image , that is showing the error that is given $\endgroup$ – user59020 Feb 12 at 10:47
1
$\begingroup$

First of all, you should use separate variables for the 3D and 2D x_trains:

x_train_n = np.zeros((x_train.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")
x_test_n = np.zeros((x_test.shape[0] - num_time_steps + 1, num_time_steps, num_features), dtype="float32")

And, your outer loop should span the new x_train, i.e. x_train_n:

for i in range(len(x_train_n)):
   for timestep in range(num_time_steps):
       x_train_n[i][timestep] = x_train[i + timestep].squeeze()

Change the other loop (the one for the test case) as well.

$\endgroup$
  • 1
    $\begingroup$ Thank you for the fast reply . I changed the code according to your code and it gives me this error "too many indices for array" Here I paaste the image of the error. Can you help me to solve this problem? $\endgroup$ – user59020 Feb 12 at 16:40
  • 2
    $\begingroup$ That’s another error. $y$ seems to be 1D. Remove the second index with semicolon. $\endgroup$ – gunes Feb 12 at 17:48
  • $\begingroup$ Yes I changed my code as you mentioned. Then it gave me this error,"could not broadcast input array from shape (3,1) into shape (3)" Can you please help me to solve this problem? Here I paste the image of the error and change code. $\endgroup$ – user59020 Feb 13 at 4:01
  • 1
    $\begingroup$ I've updated the answer with squeeze command. $\endgroup$ – gunes Feb 13 at 5:15
  • 1
    $\begingroup$ Okay I got it. Thank you for helping me to solve this problem. $\endgroup$ – user59020 Feb 13 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy