Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways

Practice Numpy

Numpy is one of the basic and most used packages in Python. It is mostly used for scientific computing.

Most important Data Type in Numpy is Array.

import numpy as np

And you have installed the numpy package in your system. Now, check the important and basic functions present in Numpy package to play around your dataset.

1. Import numpy
import numpy as n

2. Create an array in python

arr = np.array([0,1,2,3,45,6])
print(arr)
[ 0 1 2 3 45 6]

3. Create a two dimensional array

arr2 = np.array([[1,2,3],[5,6,7]])
print(arr2.shape)
(2, 3)

4. Create an array of all ones of 3 rows and 4 columns

one = np.ones((3,4))
print(one)
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

5. Create an array with all zeros in 3 rows and and 4 columns

zero = np.zeros((3,4))
print(zero)
[[0. 0. 0. 0.]  [0. 0. 0. 0.]  [0. 0. 0. 0.]]

6. Create an array with random values for 3 rows and 4 columns

rand = np.random.random((3,4))
print(rand)
[[0.15340682 0.57965139 0.15538918 0.35086011]
 [0.35453516 0.96230823 0.49873964 0.022157  ]
 [0.35144323 0.42548206 0.96434164 0.67721939]]

7. Statistical functions available in Numpy
a. Min
b. Max
c. Mean
d. Median
e. Standard Deviation

First of all, we will be creating a series of random numbers using np.random.normal function which gives you the flexibility to choose the range in which you want to have your random numbers, irony is that you can restrict the randomness of your random number 😛

import numpy as np
stat = np.random.normal(8,1.5,10)
print(stat)

[8.84371443 7.7014931  6.72789277 5.73700496 8.46005265 9.08922936  8.71789028 6.84561345 8.20465228 7.26850749]

There are three parameters in the function separated by a comma.
8 is the base value
1.5 is the range you which you are providing to the function i.e. the values will vary in the range 6.5 to 9.5
10 is the number of random values

8. Now let’s get the min, max, mean, standard deviation from “stat”

print(min(stat))
5.737004956818833

print(max(stat))
9.089229356076414

print(np.mean(stat))
7.759605075083963

print(np.std(stat))
1.0385313768435045

9. Matrix multiplication in numpy

x = [[1,2],[4,5]]
y = [[6,7],[8,9]]
z = np.matmul(x,y)
print(z)

[[22 25]  [64 73]]



About TheDataMonkGrand Master

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)

Follow Me