0

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?

2
  • With numpy, what do you get if you do name.dtype for all the arrays? Commented Apr 24, 2020 at 13:54
  • @duckboycool I get float64 Commented Apr 24, 2020 at 14:03

1 Answer 1

1

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:

  1. 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)
    
  2. 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)
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.