[Python] Why does numpy indexing with a colon vs. range result in weird behavior?

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    Could someone explain the following behavior?

    test = np.random.rand(1000, 10)
    test.shape
    (1000, 10)

    idxs = np.random.randint(0, 10, 1000)
    idxs.shape
    (1000,)

    test[:, idxs].shape
    (1000, 1000)

    test[range(test.shape[0]), idxs].shape
    (1000,)


    I would expect these to be equivalent. What am I missing?

    Continue reading...

Compartilhe esta Página