[Python] How to split NumPy array with dimensionality reduction?

Discussão em 'Python' iniciado por Stack, Maio 31, 2025.

  1. Stack

    Stack Membro Participativo

    This script:

    import numpy as np

    a = np.arange(8).reshape(2, 2, 2)
    b = np.split(a, 2)
    print(b[0].shape)


    produces:

    (1, 2, 2)


    I would like to split array a into constituent subarrays with shape (2, 2), reducing their dimension in the process. Instead, I'm getting subarrays of (1, 2, 2) shape. Is there a way to get what I want without removing the extra dimension in additional step?

    Continue reading...

Compartilhe esta Página