How to Create a Random Mask Numpy Array

Boost Your Efficiency with these 6 Numpy Tricks

And have the Power over your Array

Khuyen Tran

Motivation

From time to time, when I showed my friends a couple of Numpy methods I use, they find them helpful. So I decided to share these Numpy methods (or tricks) and hope that you will find them helpful as well.

Photo by Pop & Zebra on Unsplash

Start with importing Numpy

          import numpy as np        

Mask

Let' say we have an array and we want to pick a certain element in this array: 0, 2 and eliminate other elements. Is there a quick way that we can pick these 2 elements? Mask will do the job.

          >>> A = np.array([0, 1, 2, 3, 4, 5])          >>> mask = np.array([True, False, True, False, False, False])          >>> A[mask]          array([0, 2])        

As we can see, t h e elements that are marked as False are eliminated while the elements that are marked with True are kept.

Random

Creating a quick random array is a useful and quick way to test your code. But there are different np.random methods that you may want to opt one over another for a specific use.

Create random value with a given shape in the range from 0 to 1

          >>> np.random.rand(10,2)          array([[0.38850622, 0.31431385],            
[0.00815046, 0.13334727],
[0.47482606, 0.92837947],
[0.89809998, 0.38608183],
[0.25831955, 0.56582022],
[0.36625782, 0.52753452],
[0.88125428, 0.71624809],
[0.83642275, 0.79315897],
[0.27361664, 0.8250761 ],
[0.89894784, 0.95994016]])

Or create a random array of integers in a specific range with a specific size

          >>> np.random.randint(0,10,(3,3))          array([[0, 9, 6],            
[1, 7, 3],
[6, 4, 7]])

Shape-like Array

Have you ever found yourself create the second array with the same dimension of the first array but with different elements? A common practice is to use shape to capture the dimension of the first array

          >>> A = np.random.randint(0,10,(3,3))          >>> A.shape          (3, 3)          >>> B = np.zeros(A.shape)          array([[0., 0., 0.],            
[0., 0., 0.],
[0., 0., 0.]])

There is nothing wrong with this method. But there is a quicker way to create a similar array with zero or one as elements with zeros_like or ones_like

          >>> A = np.random.randint(0,10,(3,3))          >>> B = np.zeros_like(A)          >>> B          array([[0., 0., 0.],            
[0., 0., 0.],
[0., 0., 0.]])
>>> B = np.ones_like(A) >>> B array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])

As we can see, array B has the same dimension as array A but with different elements.

Reshape

If we want to change the dimension of an array, reshape will be our to-go method

          >>> A = np.arange(9)          >>> A          array([0, 1, 2, 3, 4, 5, 6, 7, 8])          #Reshape to 3x3 matrix          >>> A.reshape((3,3))          >>> A          array([[0, 1, 2],            
[3, 4, 5],
[6, 7, 8]])

Or flatten A

          >>> A.reshape(-1)          array([0, 1, 2, 3, 4, 5, 6, 7, 8])          >>> A.flatten()          array([0, 1, 2, 3, 4, 5, 6, 7, 8])        

Either reshape(-1) or flatten() will do the job of flattening the array.

Transform-Out-Of-Range Array

Let's say we have a matrix B ranging from -3 to 6

          >>> B = np.arange(-3,9).reshape((3,4))          >>> B          array([[-3, -2, -1,  0],            
[ 1, 2, 3, 4],
[ 5, 6, 7, 8]])

But we just want to keep the elements ranging from 0 to 5 and transform elements that are out of range to elements within range. How can we do that? That is when we need np.clip

          >>> np.clip(B,0, 5)          array([[0, 0, 0, 0],            
[1, 2, 3, 4],
[5, 5, 5, 5]])

As we can see from the transformed matrix, the elements below 0 are transformed to 0 and the elements above 5 are transformed to 5, while the elements within the range are kept the same.

Shuffle

Sometimes, we want to work with a random array but our array may not be random. Think about a deck of cards, what we do when we are doubtful that the cards may be ordered in a special way? We shuffle it!

          >>> A = np.arange(9).reshape((3,3))          >>> A          array([[0, 1, 2],            
[3, 4, 5],
[6, 7, 8]])
>>> np.random.shuffle(A) >>> A array([[3, 4, 5],
[0, 1, 2],
[6, 7, 8]])

As we can see, the elements in array A are positioned differently from the original one.

Conclusion

I hope this article is helpful to add some new Numpy tricks for your data science and programming practice. Possessing a couple of useful methods to manipulate an array will significantly boost your workflow and give you the flexibility to transform an array for your specific needs.

Feel free to fork and play with the code for this article in this Github repo.

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:

How to Create a Random Mask Numpy Array

Source: https://towardsdatascience.com/boost-your-efficiency-with-these-6-numpy-tricks-29ca2fe81ecd

0 Response to "How to Create a Random Mask Numpy Array"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel