shuffle vs permute numpy
np.random.permutation
has two differences from np.random.shuffle
:
- if passed an array, it will return a shuffled copy of the array;
np.random.shuffle
shuffles the array inplace - if passed an integer, it will return a shuffled range i.e.
np.random.shuffle(np.arange(n))
If x is an integer, randomly permute np.arange(x). If x is an array, make a copy and shuffle the elements randomly.
The source code might help to 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
Adding on to what @ecatmur said, np.random.permutation
is useful when you need to shuffle ordered pairs, especially for classification:
from np.random import permutationfrom sklearn.datasets import load_irisiris = load_iris()X = iris.datay = iris.target# Data is currently unshuffled; we should shuffle # each X[i] with its corresponding y[i]perm = permutation(len(X))X = X[perm]y = y[perm]
The permutation() method returns a re-arranged array (and leaves the original array un-changed),this method will keep the original array intact and will return a shuffled array, for example x = [1,4,2,8] is the original array and the permutation method will return the rearranged array (lets say [8,4,1,2]).Now,you have two arrays, original array and the rearranged array.
In the other hand,
The shuffle() method makes changes to the original array,for example x = [1,4,2,8] is the original array and the shuffle method will return the shuffled array(lets say shuffled array is [8,4,1,2]). Now , the original array itself got changed to the Shuffled array, and you are only left with the shuffled array.
Reference :-https://www.w3schools.com/python/numpy_random_permutation.asp