# 5 Incredible Uses of Numpy Shuffle With Examples

Hello geeks, and welcome! This article will cover NumPy shuffle, also known as NumPy random shuffle(). We will also look at its syntax and parameters for a better overall understanding. Then, we will see some examples to understand the topic better. But at first, let us try to get a brief understanding of the function through its definition.

Shuffle is a common word that we use daily, from shuffling a deck of cards to different chits in a bowl. Shuffling generally means arranging the objects randomly or in a jumbled fashion. The Function NumPy shuffle helps us by allowing us to change the positions of elements in a NumPy array.

This will become a lot clearer after looking at some examples. But first, let us look at its syntax and parameters.

Contents

## Syntax and Parameter

``np.random.shuffle(x)``

This is the general syntax associated with the function. Unlike many other NumPy functions, not many parameters are associated with it.

X: ndarray or changeable sequence

This parameter represents the array, list, or changeable sequence on which the user wants to apply the function.

## Return:

NumPy random shuffle(), the function itself returns None as it works on place.

## Examples

Now, we are done with all the theory parts. We have covered its syntax and parameters in detail. Itâ€™s time to discuss various examples that will help us understand the topic better. Let us start with an elementary-level example, and as we move ahead, we will gradually increase the level of the example.

### 1. For 1-d array

```import numpy as ppool

a= ppool.arange(8)
print(a)
ppool.random.shuffle(a)
print(a)
```

Output:

``````[0 1 2 3 4 5 6 7]
[7 5 2 1 0 3 4 6]``````

Here, we can see a fundamental example of the NumPy shuffle. To perform this, we first imported the NumPy Module. Following this we have used another NumPy function called `arange`“. This function provides us with spaced values within the specified limit. Next, we used the shuffle function and finally printed the result. Here, we can observe the changes and the effect of the shuffle function.

For 1-D arrays, we can use the array function to define an array. Apart from this, we need to follow all the similar steps. You can see an example using this below.

```import numpy as ppool

a= ppool.array([1,5,7,8])
print(a)
ppool.random.shuffle(a)
print(a)
```

Output:

``````[1 5 7 8]
[5 1 7 8]``````

### 2. NumPy shuffle for Multidimensional array

```import numpy as ppool
a=ppool.arange(12).reshape((6, 2))
print(a)
ppool.random.shuffle(a)
print(a)
```

Output:

``````#input
[[ 0  1]
[ 2  3]
[ 4  5]
[ 6  7]
[ 8  9]
[10 11]]
#output
[[10 11]
[ 0  1]
[ 4  5]
[ 6  7]
[ 8  9]
[ 2  3]]
``````

Here, we can see an example of the multidimensional array. We have followed similar steps to the previous examples. Also besides the “`arange`” function, we have used the “reshape” function of NumPy. The reshape function helps us create an array as per our choices. For the rest, you can spot the difference between the results. Before the shuffle and after the shuffle.

### 3. Shuffle for a list

```import numpy as ppool
list =["PYTHON","PPOOL","WEBSITE"]
print(list)
ppool.random.shuffle(list)
print(list)
```

Output:

``````['PYTHON', 'PPOOL', 'WEBSITE']
['PPOOL', 'WEBSITE', 'PYTHON']``````

Here, we have used the NumPy shuffle function for a list. The list here contains multiple strings. The shuffle function here also creates a jumbled output for it.

### 4. NumPy shuffle rows

```import numpy as ppool
a= ppool.random.random((3,4))
print(a)
ppool.random.shuffle(a)
print(a)
```

Output:

``````[[0.10643543 0.8306541  0.94395107 0.62732634]
[0.01638072 0.57944138 0.62131248 0.92049832]
[0.03665327 0.42699486 0.37938069 0.47964699]]

[[0.10643543 0.8306541  0.94395107 0.62732634]
[0.03665327 0.42699486 0.37938069 0.47964699]
[0.01638072 0.57944138 0.62131248 0.92049832]]``````

In the above example, we can see how a row-wise shuffle occurs. To get an array to perform shuffle, we have used the `random.random` function. Then we used our function, and at last, we can observe the difference in output.

### 5. NumPy shuffle 2 arrays

```from sklearn.utils import shuffle
import numpy as np
X = np.array([[11, 10], [212, 10], [5, 6]])
y = np.array([9,1,0])
X, y = shuffle(X, y)
print(X, y)
```

Output:

``````[[ 11  10]
[  5   6]
[212  10]] [9 0 1]``````

As if now, all the examples that we have seen were concerned with just one array. Through this example, we can generate an idea of how the function can be applied to 2 arrays simultaneously. To this, we have at first imported the shuffle from sklearn utils. Then, we have imported the NumPy function. Then, we defined 2 arrays, after which we used the shuffle function. Here, the output verifies our code.

## Conclusion

This article covers the NumPy shuffle, also known as the NumPy random shuffle(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that NumPy shuffle is a function that helps in reshuffling a NumPy array.

I hope this article was able to clear all doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.

If you’re done reading this, why not read the unhashable type: list error next?

Subscribe
Notify of