Reputation: 241
I'm following a tutorial on YouTube (https://www.youtube.com/watch?v=y1ZrOs9s2QA&feature=youtu.be), the code can be found here: https://github.com/murtazahassan/Digits-Classification/blob/master/OCR_CNN_Trainning.py
But when I try to run the code below, I get the error: TypeError: only integer scalar arrays can be converted to a scalar index.
x_train = np.array(list(map(preprocess, x_train)))
x_test = np.array(list(map(preprocess, x_test)))
x_validation = np.array(list(map(preprocess, x_validation)))
x_train = x_train.reshape(x_train[0], x_train[1], x_train[2], 1)
x_test = x_test.reshape(x_test[0], x_test[1], x_test[2], 1)
x_validation = x_validation.reshape(x_validation[0], x_validation[1], x_validation[2], 1)
I found this (TypeError: only integer scalar arrays can be converted to a scalar index , while trying kfold cv) and this (TypeError when indexing a list with a NumPy array: only integer scalar arrays can be converted to a scalar index), but it did not help me. What am I doing wrong?
Upvotes: 0
Views: 296
Reputation: 12397
You are passing values to reshape
that are not of type int
:
x_train = x_train.reshape(x_train[0], x_train[1], x_train[2], 1)
Here, for example, x_train[0]
need to be of type int (and so the rest). If you meant to use their shape instead of their values, use:
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
Otherwise, you have two options:
If you know values in x_train
, x_test
and x_validation
are integers, set their dtype to int
(and make sure it remains the same during ML operations:
x_train = np.array(list(map(preprocess, x_train)), dtype=np.int)
x_test = np.array(list(map(preprocess, x_test)), dtype=np.int)
x_validation = np.array(list(map(preprocess, x_validation)), dtype=np.int)
If you need them to be float
but want to call them as int
, use:
x_train = x_train.reshape(int(x_train[0]), int(x_train[1]), int(x_train[2]), 1)
Upvotes: 1