Skip to content

Difference between np.random.shuffle and np.random.permutation accepted inputs #8250

@bamattsson

Description

@bamattsson

I came across this when using np.random.permutation and np.random.shuffle on a list of tuples looking like this (int, np.ndarray). np.random.shuffle works fine, whereas np.random.permutation crashes. This since np.random.permutation uses np.array() which fails for the mentioned data.

Test script:

import numpy as np

N = 4
A = np.arange(N)[:,None]
A = np.concatenate((A,A,A,A), axis = 1)
B = range(N)
c = list(zip(B,A))

np.random.shuffle(c) # Works fine here
c = np.random.permutation(c) # Fails here
np.array(c) # Fails here

This might very well be the desirable behavior, there's no pretty way to convert a tuple containing both a 0-dim integer and a 1-dim ndarray to a ndarray. But the docs for np.random.permutation and np.random.shuffle really doesn't reflect this difference in behavior. They have the same accepted input under parameters for example. Maybe adding list under parameters for shuffle would make it clearer?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions