1

I use np.random.choice to get random index but run into the following error.

TypeError: only integer scalar arrays can be converted to a scalar index. Any suggestion would be much appreciated.

x_train = [0,1,2,3,4]
train_size = 4
batch_size =3
batch_mask = np.random.choice(train_size, batch_size)
print(batch_mask)
x_batch = x_train[batch_mask]
print(x_batch)
applepie
  • 25
  • 1
  • 1
  • 5
  • 1
    Does this answer your question? [TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array](https://stackoverflow.com/questions/50997928/typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-with-1d) – Pranav Hosangadi Jul 16 '21 at 18:16

1 Answers1

3

x_train is a simply python list. You're trying to use a NumPy array as an index; this is not legal. Make x_train a numpy array, instead.

x_train = np.array([0,1,2,3,4])
train_size = 4
batch_size =3
batch_mask = np.random.choice(train_size, batch_size)
print(batch_mask)
x_batch = x_train[batch_mask]
print(x_batch)

Output:

[3 3 2]
[13 13 12]
Prune
  • 76,765
  • 14
  • 60
  • 81