Eugene Fitzher
Eugene Fitzher

Reputation: 163

Python TypeError: only integer scalar arrays can be converted to a scalar index

I'm new to Python & Deep Learning.

I'm trying to make a simple learning, but I got an error:

only integer scalar arrays can be converted to a scalar index. on t_batch = t_label[batch_mask]

t_label example: ['circle', 'circle', 'rectangle', 'triangle', ..., 'pentagon']

batch_mask example: [2 0 2 1 2 2 1 0 0 2 2 1 2 0 0 1 0 0 1 0 1 0].

train_size = 3 # x_train.shape[0]
batch_size = 22
for i in range(242): # iters_num = 242
   batch_mask = np.random.choice(train_size, batch_size)
   print( t_train, batch_mask )
   x_batch = x_train[batch_mask]
   t_batch = t_label[batch_mask]

I loaded images and labels using following codes. Thanks for your help.

data_list = glob('dataset\\training\\*\\*.jpg')
def _load_img():

    for v in data_list:
   #     print("Converting " + v + " to NumPy Array ...")       
        data = np.array(Image.open(v))
        data = data.reshape(-1, img_size)

    return data

def _load_label():
    labels = []
    for path in data_list:
        labels.append(get_label_from_path(path))

    return labels

Upvotes: 1

Views: 941

Answers (1)

FredrikHedman
FredrikHedman

Reputation: 1253

Assuming t_label is created using _load_label() it is a list and this type does not support indexing with another list (your batch_mask). Hence your error.

If you want to use this type of indexing you need to create t_label as a NumPy array. More specifiaclly:

def load_label(data_list):
    labels = []
    for path in data_list:
        labels.append(get_label_from_path(path))
    return np.array(labels)

def load_label_variation(data_list):
    # Use a list comprehension to build list of labels
    labels = [get_label_from_path(path) for path in data_list]
    return np.array(labels)

Upvotes: 1

Related Questions