Array of integers which appear to be objects

Viewed 127

I have a strange situation which I do not understand. I wand to use an array as indices.

batch_index = np.arange(min(mb_size,len(D)), dtype=np.int32)
Q_target[batch_index, actions] = rewards + gamma*np.max(Q_next, axis=1)*dones

But when I run this I get an error:

IndexError: arrays used as indices must be of integer (or boolean) type

It appears to be an array of objects:

In: actions
Out: array([2, 2, 2, 2], dtype=object)

The same in variable explorer:

Type: Array of object Value: ndarray object of numpy module

In the same time:

In:type(actions[0])
Out: numpy.int64

I know that I can use:

actions = np.array(actions, dtype=np.int32)

But I do not understand why do I have to do it.

P.S. here is how I get actions

D = deque()
D.append((state, action, reward, state_new, done))
#type(action) == int64
minibatch = np.array(random.sample(D, min(mb_size,len(D))))
actions = minibatch[:,1]
1 Answers

In my opinion it's because actions is of mixed type (i.e. done is Boolean, reward is most probably some sort of numeric etc.) because you get it from queue object. In python in general, and specifically in NumPy, when you have a mixed, non-numeric object types - the array is cast to an Object (or other unifying type, that all items agree on). So in your case, the reason you can't index the data with this array is not due to the batch_index, but because of the actions array.

Related