Numpy Practice Quesions Set-2
Example
Python
import numpy as np
a = np.arange(1,37).reshape(6,6)
a
Output
PowerShell
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 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]])
Example
Python
print(f"a[2][3] ==> {a[2][3]}")
print(f"a[2,3] ==> {a[2,3]}")
Output
PowerShell
a[2][3] ==> 16
a[2,3] ==> 16
Example
Python
oned_array = a[0,1:5]
twod_array = a[0:1,1:5]
print(f"using with indexing : {oned_array}")
print(f"using with slicing : {twod_array}")
Output
PowerShell
using with indexing : [2 3 4 5]
using with slicing : [[2 3 4 5]]
Example
Python
oned_array = a[0,1:5]
twod_array = a[0:1,1:5]
print(f"using with indexing : {oned_array}")
print(f"using with slicing : {twod_array}")
Output
PowerShell
using with indexing : [2 3 4 5]
using with slicing : [[2 3 4 5]]
array([[ 1, 2, 3, 4, 5, 6],
[31, 32, 33, 34, 35, 36]])
Example
Python
a[::5,:]
Output
PowerShell
array([[ 1, 2, 3, 4, 5, 6],
[31, 32, 33, 34, 35, 36]])
array([[ 9, 10],
[15, 16],
[21, 22]])
Example
Python
a[1:4,2:4]
Output
PowerShell
array([[ 9, 10],
[15, 16],
[21, 22]])
Example
Python
a[a%2==0]
Output
PowerShell
array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36])
Example
Python
a[a%5==0]
Output
PowerShell
array([ 5, 10, 15, 20, 25, 30, 35])
Example
Python
# a[[row_indices],[column_indices]]
a[[1,2,4,5],[1,4,1,4]]
Output
PowerShell
array([ 8, 17, 26, 35])
Example
Python
# np.amin(a)
# np.min(a)
# a.min()
print(f"np.amin(a) ==> {np.amin(a)}")
print(f"np.min(a) ==> {np.min(a)}")
print(f"a.min() ==> {a.min()}")
Output
PowerShell
np.amin(a) ==> 1
np.min(a) ==> 1
a.min() ==> 1
Example
Python
# np.amax(a)
# np.max(a)
# a.max()
print(f"np.amax(a) ==> {np.amax(a)}")
print(f"np.max(a) ==> {np.max(a)}")
print(f"a.max() ==> {a.max()}")
Output
PowerShell
np.amax(a) ==> 36
np.max(a) ==> 36
a.max() ==> 36
Example
Python
print(f"np.sum(a) ==> {np.sum(a)}")
print(f"a.sum() ==> {a.sum()}")
Output
PowerShell
np.sum(a) ==> 666
a.sum() ==> 666
Example
Python
print(f"np.sum(a,axis=0) ==> {np.sum(a,axis=0)}")
print(f"a.sum(axis=0) ==> {a.sum(axis=0)}")
Output
PowerShell
np.sum(a,axis=0) ==> [ 96 102 108 114 120 126]
a.sum(axis=0) ==> [ 96 102 108 114 120 126]
Example
Python
print(f"np.mean(a) ==> {np.mean(a)}")
print(f"a.mean() ==> {a.mean()}")
Output
PowerShell
np.mean(a) ==> 18.5
a.mean() ==> 18.5
Example
Python
print(f"np.median(a) ==> {np.median(a)}")
Output
PowerShell
np.median(a) ==> 18.5
Example
Python
print(f"np.var(a) ==> {np.var(a)}")
print(f"a.var() ==> {a.var()}")
Output
PowerShell
np.var(a) ==> 107.91666666666667
a.var() ==> 107.91666666666667
Example
Python
print(f"np.std(a) ==> {np.std(a)}")
print(f"a.std() ==> {a.std()}")
Output
PowerShell
np.std(a) ==> 10.388294694831615
a.std() ==> 10.388294694831615