Python rare-py

Hello there !! Today I’m gonna list the rare functions that are used at times in Numpy that might be useful for you at times when you find yourself stranded in the midst of problems ! Less talks, more works

Note : Stuff will be random and hence use the ctrl+v and ctrl+f or else simply go with the flow

Random : We know random.randint which we commonly use. But there are other cool stuffs random can do. For instance we can set a random value to the seed and get a order of random elements.

Seed :

Import numpy as np

np.random.seed(20)

np.random.randint(100,200,size=(4,5))

This will give you an order of random 20 values in an array which will be in an order. You can set this in any notebook to get a particular order to work with

Randn :

np.random.randn(1)

This will give you a random array with values inside 1 and it will print number of elements based on the value you give inside randn

Cool right?

Now you can use round() function to round off the values to a particular value and then bang !! you have rounded up value !

Print options :
In print you can choose how much stuff should the np array must display. For that you can use the function called as np.set_printoptions() function that is available. In case you are wondering :

np.arange(10) will give you values like (0,1,2,3,4,5,6,7,8,9) but using np.set_printoptions() you can limit the number of values that are shown

np.set_printoptions(threshold=3) or any value you wanna display. It will display (0,1,….,9) now

Intersect, Union, Set difference : This is the same stuff we saw in the sets in python but here the same is done in numpy too but only difference being the functions are varied.

  • Union : To find the unique values of two arrays, use the union1d() method.
  • Intersection : To find only the values that are present in both arrays, use the intersect1d() method.
  • Set difference : To find only the values in the first set that is NOT present in the seconds set, use the setdiff1d() method.
  • Symmetric difference : To find only the values that are NOT present in BOTH sets, use the setxor1d() method.

Source : W3schools

How to flip?

Flip is a sort of Transpose but different kind. It reverses the entire stuff. In a given array it changes rows to columns and vice versa. This is very useful when you are struck in a python program like me. So how to get over it? simple.

np.flip(data,axis=0). — flips the rows

np.flip(data, axis=1) — flips the columns

By using this we can flip the given array into what I said earlier. :D

Argsort()

Argsort is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as arr that that would sort the array.

For instance :

x = np.random.randint(20, size=10)
print(x)
[ 9 4 15 0 17 16 17 8 9 0]
Result:
[4 2 6 0 8 7 9 3 5 1]

What should we do here :

first take x and then do a argsort () and store it in a variable. Repeat the argsort for the given variable

a= np.argsort(x)

np.argsort(a) will give the desired output.

What to do for 2d arrays? smash the 2d array into 1d array. This can be done using the ravel function. Ravel is simply like demolishing a building, here you demolish the entire 2d or 3d array as 1d array

a = 2d array

np.ravel(2d array)

np.argsort().argsort() and then you reshape it and you have the desired value

These are some simple functions in numpy which can be very useful at times when you need them. I’m attaching a link for a lot of numpy exercises which will be useful for practice !

Thank you !! happy learning

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store