Sunday, February 7, 2021

Python Pool: 5 Incredible Uses of Numpy Shuffle With Examples

Hello geeks and welcome in this article, we will cover NumPy shuffle, also known as NumPy random shuffle(). Along with that, for an overall better understanding, we will look at its syntax and parameter. Then we will see a couple of 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. Shuffle 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 more clearer after looking at some examples. But first, let us look at its syntax and parameter.

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 part. We have covered its syntax and parameters in detail. It’s time to discuss various examples that will help understand the topic better. Let us start with an elementary level example, and as we move ahead, we will gradually increase the level of 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 for the NumPy shuffle. To perform this, we have first imported the NumPy Module. Following which we have used another NumPy function called arange“. This function provides us with spaced values within the specified limit. Next, we have 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 for 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 in creating an array as per choices. Rest you can spot the difference between results. Before 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 is also to create 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 have defined 2 arrays, after which we have used the shuffle function. Here the output verifies our code.

Conclusion

This article covers the NumPy shuffle, also known as 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 in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read unhashable type: list error next.

The post 5 Incredible Uses of Numpy Shuffle With Examples appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...