I'm a begginer in programming I beg your pardon for that question, I just want to learn.

The function



Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test).

But I want a data set like this (x,y) with $x$ a data and $y$ a label. So this is my attempt :

import tensorflow as tf
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
N = np.size(x_train)

training_data = []
for i in range(N):
    training_data.append( (x_train[i], y_train[i]) )

n = np.size(x_test)
test_data =  np.zeros( (n,2) )
for i in range(n):
    test_data[i] = (X_test[i],y_test[i])

net = Network([784, 30, 10])
net.SGD(training_data, 30, 10, 3.0, test_data=test_data)

And the error I get which i don't understand :

training_data.append( (x_train[i], y_train[i]) ) IndexError: index 60000 is out of bounds for axis 0 with size 60000

Thanks you for your help.


1 Answer 1


What you're trying to achieve with loops can be quickly done via the following:

train_data = np.array(list(zip(x_train, y_train)))
test_data = np.array(list(zip(x_test, y_test)))

Also, note that np.size gives you the overall number of elements (floating points) inside the multi-dimensional array, i.e. here $60K\times28\times28$, so you run out of indices.

  • $\begingroup$ Thank you, I'm going to wait some hours to wait if someone can explain me why I messed up then I'll give you the accept :). $\endgroup$
    – CechMS
    Commented Mar 28, 2020 at 13:22
  • 1
    $\begingroup$ it’s in my last paragraph, you messed up because you used np.size, instead you need to use x_train.shape[0] and you’ve other errors in your test loop as well $\endgroup$
    – gunes
    Commented Mar 28, 2020 at 13:29
  • $\begingroup$ Oh okay, x_train is a array of 60Kx28x28 so... If I want to get the 60 I need to do x_train.shape[0]. Good thanks you once again. $\endgroup$
    – CechMS
    Commented Mar 28, 2020 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.