python - shuffle vs permute numpy -
what difference between numpy.random.shuffle(x)
, numpy.random.permutation(x)
?
i have read doc pages not understand if there difference between 2 when want randomly shuffle elements of array.
to more precise suppose have array x=[1,4,2,8]
.
if want generate random permutations of x, difference between shuffle(x)
, permutation(x)
?
np.random.permutation
has 2 differences np.random.shuffle
:
- if passed array, return shuffled copy of array;
np.random.shuffle
shuffles array inplace - if passed integer, return shuffled range i.e.
np.random.shuffle(np.arange(n))
if x integer, randomly permute np.arange(x). if x array, make copy , shuffle elements randomly.
the source code might understand this:
3280 def permutation(self, object x): ... 3307 if isinstance(x, (int, np.integer)): 3308 arr = np.arange(x) 3309 else: 3310 arr = np.array(x) 3311 self.shuffle(arr) 3312 return arr
Comments
Post a Comment