Numpy Practice Quesions Set-1
Example
Python
import numpy as np
# np.zeros(7)
# np.zeros(7,dtype=int)
# np.full(7,0)
print(f"Array of 7 zeros using np.zeros(7) : {np.zeros(7)}")
print(f"Array of 7 zeros using np.zeros(7,dtype=int) : {np.zeros(7,dtype=int)}")
print(f"Array of 7 zeros using np.full(7,0) : {np.full(7,0)}")
Output
PowerShell
Array of 7 zeros using np.zeros(7) : [0. 0. 0. 0. 0. 0. 0.]
Array of 7 zeros using np.zeros(7,dtype=int) : [0 0 0 0 0 0 0]
Array of 7 zeros using np.full(7,0) : [0 0 0 0 0 0 0]
Example
Python
# np.ones(9)
# np.ones(9,dtype=int)
# np.full(9,1)
print(f"Array of 9 ones using np.ones(9) : {np.ones(9)}")
print(f"Array of 9 ones using np.ones(9,dtype=int) : {np.ones(9,dtype=int)}")
print(f"Array of 9 ones using np.full(9,1) : {np.full(9,1)}")
Output
PowerShell
Array of 9 ones using np.ones(9) : [1. 1. 1. 1. 1. 1. 1. 1. 1.]
Array of 9 ones using np.ones(9,dtype=int) : [1 1 1 1 1 1 1 1 1]
Array of 9 ones using np.full(9,1) : [1 1 1 1 1 1 1 1 1]
Example
Python
# np.full(10,4)
# np.zeros(10,dtype=int)+4
# np.ones(10,dtype=int)+3
print(np.full(10,4))
print(np.zeros(10,dtype=int)+4)
print(np.ones(10,dtype=int)+3)
Output
PowerShell
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
Example
Python
print(np.arange(10,41))
Output
PowerShell
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40]
Example
Python
np.arange(10,41,2)
Output
PowerShell
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40])
Example
Python
np.arange(11,41,2)
Output
PowerShell
array([11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39])
Example
Python
# 1st way:
print(np.arange(14,41,7))
Output
PowerShell
[14 21 28 35]
Example
Python
# 2nd way:
a = np.arange(10,41)
print(a)
print(a[a%7==0])
Output
PowerShell
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40]
[14 21 28 35]
Example
Python
# 1st way:
print(f"using np.arange(24,43,2): {np.arange(24,43,2)}")
# 2nd way:
a = np.arange(24,50)
a = a[a%2==0]
print(f"using np.resize(a,10) : {np.resize(a,10)}")
Output
PowerShell
using np.arange(24,43,2): [24 26 28 30 32 34 36 38 40 42]
using np.resize(a,10) : [24 26 28 30 32 34 36 38 40 42]
Example
Python
np.arange(1,17).reshape(4,4)
Output
PowerShell
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
Example
Python
print(f"using np.eye(4) : \n {np.eye(4)}")
print(f"using np.identity(4) :\n {np.identity(4)}")
Output
PowerShell
using np.eye(4) :
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
using np.identity(4) :
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
Example
Python
np.random.randint(1,101,size=(2,3,4))
Output
PowerShell
array([[[51, 52, 93, 82],
[ 3, 83, 48, 19],
[96, 39, 44, 98]],
[[53, 52, 11, 20],
[76, 14, 89, 50],
[69, 65, 71, 65]]])
Example
Python
np.random.rand()
Output
PowerShell
0.8054592849192298
Example
Python
np.random.rand(10)
Output
PowerShell
array([0.57786998, 0.21131385, 0.47052737, 0.11353636, 0.07344479,
0.12596435, 0.51560813, 0.00377141, 0.51092043, 0.51462955])
Example
Python
# np.random.uniform(low=0.0,high=1.0,size=None)
np.random.uniform(10,20,10 )
Output
PowerShell
array([12.28628812, 17.37509048, 18.88105358, 13.83690349, 15.83230432,
12.12287601, 12.81889152, 17.77903863, 12.94707042, 13.30409307])
Example
Python
np.random.randn(10)
Output
PowerShell
array([ 0.71579249, 1.42716022, 0.31241851, -0.37037978, 1.82957797
-2.40593885, 0.62470205, -1.58715091, 0.16443136, 0.54251342])
Example
Python
# normal(loc=0.0, scale=1.0, size=None)
# loc--->mean
# scale--->standard deviation
Output
PowerShell
output nhi h
Example
Python
a = np.random.normal(15,4,10)
a
Output
PowerShell
array([18.29936929, 11.48075769, 11.01075089, 23.83089517, 18.43564922,
16.47381834, 14.684902 , 15.13868843, 11.67061641, 16.88892347])
Example
Python
np.linspace(1,100,10,retstep=True)
Output
PowerShell
(array([1., 12., 23., 34., 45., 56., 67., 78., 89., 100.]), 1 1.0)
Example
Python
np.linspace(0,1,15,retstep=True)
Output
PowerShell
(array([0. , 0.07142857, 0.14285714, 0.21428571, 0.28571429,
0.35714286, 0.42857143, 0.5 , 0.57142857, 0.64285714,
0.71428571, 0.78571429, 0.85714286, 0.92857143, 1. ]),
0.07142857142857142)