0

I have a 5 dimensional ndarray called self.q_table. I've got a regular array, the length of which is 4. When I try to find out the maximum value in that row like this...

max = np.max(self.q_table[regular_array])

...I get an IndexError, even though the elements of regular_array are smaller than the dimensions of q_table.

I tried to alter the dimensions of both arrays, but it didn't get better.

Edit:

The error is IndexError: index 11 is out of bounds for axis 0 with size 10

Numpy is imported as np, and the last line in this sample throws the error.

class AgentBase:
def __init__(self, observation_space):
    OBSERVATION_SPACE_RESOLUTION = [10, 15, 15, 15]
    self.q_table = np.zeros([*OBSERVATION_SPACE_RESOLUTION, 4])
    max_val = np.max(self.q_table[self.quantize_state(observation_space, [-150, 100, 3, 3])])
    print(max_val)

@staticmethod
def quantize_state(observation_space, state):
    state_quantized = np.zeros(len(state))
    lin_spaces = []
    for i in range(len(observation_space)):
        lin_spaces.append(np.linspace(observation_space[i][0], observation_space[i][1],
                                     OBSERVATION_SPACE_RESOLUTION[i] - 1, dtype=int))
    for i in range(len(lin_spaces)):
        state_quantized[i] = np.digitize(state[i], lin_spaces[i])
    return state_quantized.astype(int)

self.observation_space is a parameter with the following values: enter image description here

7
  • 1
    Can you turn this into a small working example? That helps clarify the problem and is the basis for answers. This appears to be in an instance method, but the example doesn't have to go that far. Commented Dec 16, 2022 at 0:36
  • 2
    Come on - provide us with some real information! Like the full text of the error? Info on the arrays would also help, such as shape and dtype. Is this regular_array supposed to have one value per dimension, or 4 values on one dimension? Maybe you should be using a tuple rather than an array (that's just a guess based on a poorly worded description). Commented Dec 16, 2022 at 0:40
  • Yeah, sure. I'll edit the post accordingly. Commented Dec 16, 2022 at 9:21
  • Why is it unclear to you why index 11 is out of bounds for an array with size 10? Commented Dec 16, 2022 at 9:50
  • 2
    By "regular array", do you mean Python’s built-in list type? Commented Dec 16, 2022 at 10:53

1 Answer 1

1

When you index an ndarray with a list (or, to quote the numpy docs, "a non-tuple sequence object"), you invoke advanced indexing, so when you index with [3,7,11,11] it tries to apply all those values to the first dimension.

I agree with @hpaulj’s comment that you may want to index with a tuple instead.

Sign up to request clarification or add additional context in comments.

1 Comment

solved it, thanks! I just had to convert the result of quantize_state to tuple

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.