[Python] numpy python - slicing rows and columns at the same time

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a numpy matrix with 130 X 13. Say I want to select a specific set of rows meeting a condition and a subset of columns -

    trainx[trainy==label,[0,6]]


    The above code does not work and throws an error - IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (43,) (2,).

    However if I do it in 2 steps - first subset rows and then columns, it works. Is it something weird or numpy works this way?

    temp1 = trainx[trainy==label,:]
    temp1 = temp1[:,[0,6]]

    Continue reading...

Compartilhe esta Página