# could not broadcast input array from shape (2,3) into shape (3) while using timestamp to build neural network in python

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:]


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,

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

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.

• 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? – kas Feb 12 at 16:40
• That’s another error. $y$ seems to be 1D. Remove the second index with semicolon. – gunes Feb 12 at 17:48
• 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. – kas Feb 13 at 4:01
• I've updated the answer with squeeze command. – gunes Feb 13 at 5:15
• Okay I got it. Thank you for helping me to solve this problem. – kas Feb 13 at 5:22