[Python] Randomly shuffle items in each row of numpy array

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a numpy array like the following:

    Xtrain = np.array([[1, 2, 3],
    [4, 5, 6],
    [1, 7, 3]])


    I want to shuffle the items of each row separately, but do not want the shuffle to be the same for each row (as in several examples just shuffle column order).

    For example, I want an output like the following:

    output = np.array([[3, 2, 1],
    [4, 6, 5],
    [7, 3, 1]])


    How can I randomly shuffle each of the rows randomly in an efficient way? My actual np array is over 100000 rows and 1000 columns.

    Continue reading...

Compartilhe esta Página