1

In this https://colab.research.google.com/drive/1gS2aJo711XJodqqPIVIbzgX1ktZzS8d8?usp=sharing , they used np.max(qtable[new_state, :])

But I did an experiment and I don't understand the need of : . My experiement show the same value, same array shape

import numpy as np

N = 10
grid = np.array([[np.array(k) for i in range(N)] for k in range(N)])
print(grid)
index = 5
d = grid[index]
e = grid[index, :]
print(d)
print(e)
TSR
  • 17,242
  • 27
  • 93
  • 197

1 Answers1

3

As you have noticed, qtable[new_state, :] and qtable[new_state] are indeed equivalent.

If I were writing that code myself, I would also use the : because I believe it makes the code more self-documenting, flagging to me and future readers of the code (maybe me in six months) that there is a second dimension and I'm grabbing all its values.

The colon also makes it explicit that this is a NumPy Array slice operation, and not a simple list element operation.

But it's just a question of style and documentation. (I don't know if there's a performance impact, but I don't expect there is.)

joanis
  • 10,635
  • 14
  • 30
  • 40
  • thanks for that clarification. I wouldn't put that though :) qtable is obviously a grid as the variable name implies, and it is passed inside the max functions which implies a list – TSR Mar 25 '23 at 03:40
  • Yeah, it's true, by the time you understand what a qtable is, the fact that it's a grid is obvious. But while you're learning the algorithm and don't get it yet, maybe that colon helps reinforce the concept? – joanis Mar 25 '23 at 20:47