NumPy Basic tutorial 4 - Python Programming |
Open
Anaconda Navigator
Launch
Sypder
If you have not seen the earlier tutorials
PROGRAM
1
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print ('Original array' )
print (arr)
print ('amin() function 1')
print (np.amin(arr,0) )
print (' amin() function 2' )
print (np.amin(arr,1) )
print (' amax() function 1' )
print (np.amax(arr, axis = 0))
print (' amax() function 2' )
print (np.amax(arr) )
print ('ptp() function 1')
print (np.ptp(arr))
print (' ptp() function along axis 0')
print (np.ptp(arr, axis = 0))
print ('ptp() function along axis 1')
print (np.ptp(arr, axis = 1))
OUTPUT
Original array
[[1 2 3]
[4 5 6]
[7 8 9]]
amin() function 1
[1 2 3]
amin() function 2
[1 4 7]
amax() function 1
[7 8 9]
amax() function 2
9
ptp() function 1
8
ptp() function along axis 0
[6 6 6]
ptp() function along axis 1
[2 2 2]
PROGRAM 2
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print ('Original array' )
print (arr)
#percentile
print ('percentile() function')
print (np.percentile(arr,50))
print ('percentile() function along axis 1')
print (np.percentile(arr,50, axis = 1))
print (' percentile() function along axis 0')
print (np.percentile(arr,50, axis = 0))
#median
print (' median() function:')
print (np.median(arr))
print (' median() function along axis 0')
print (np.median(arr, axis = 0))
OUTPUT
Original array
[[1 2 3]
[4 5 6]
[7 8 9]]
percentile() function
5.0
percentile() function along axis 1
[2. 5. 8.]
percentile() function along axis 0
[4. 5. 6.]
median() function:
5.0
median() function along axis 0
[4. 5. 6.]
PROGRAM 3
import numpy as np
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print ('Original array' )
print (arr)
print ('mean() function')
print (np.mean(arr))
print ('mean() function along axis 0')
print (np.mean(arr, axis = 0))
print ('mean() function along axis 1')
print (np.mean(arr, axis = 1))
print (' average() function' )
print (np.average(arr))
OUTPUT
Original array
[[1 2 3]
[4 5 6]
[7 8 9]]
mean() function
5.0
mean() function along axis 0
[4. 5. 6.]
mean() function along axis 1
[2. 5. 8.]
average() function
5.0
PROGRAM 4
import numpy as np
print (np.std([1,2,3])) #standard
print (np.var([1,2,3])) #variance
OUTPUT
0.816496580927726
0.6666666666666666
PROGRAM 5
import numpy as np
arr = np.array([[8,0,3],[4,9,7]])
print('original array')
print(arr)
print ('sort() function')
print (np.sort(arr))
print ('Sort along axis 0')
print (np.sort(arr, axis = 0))
print ('Sort along axis 1')
print (np.sort(arr, axis = 1))
OUTPUT
original array
[[8 0 3]
[4 9 7]]
sort() function
[[0 3 8]
[4 7 9]]
Sort along axis 0
[[4 0 3]
[8 9 7]]
Sort along axis 1
[[0 3 8]
[4 7 9]]
PROGRAM 6
import numpy as np
arr = np.array([6, 2, 9])
print ('original array')
print (arr)
print ('argsort()' )
arsort = (np.argsort(arr))
print (arsort)
print ('Reconstruct original array in sorted order')
print (arr[arsort])
print ('Reconstruct the original array using loop')
for i in arsort:
print (arr[i])
OUTPUT
original array
[6 2 9]
argsort()
[1 0 2]
Reconstruct original array in sorted order
[2 6 9]
Reconstruct the original array using loop
2
6
9
PROGRAM 7
import numpy as np
arr = np.array([[6, 2, 9],[1,0,3],[8,4,7]])
print ('argmax() function')
print (np.argmax(arr))
print ('Index of maximum number in flattened array')
print (arr.flatten())
print ('Array containing indices of maximum along axis 0')
m = np.argmax(arr, axis = 0)
print (m)
print ('Array containing indices of maximum along axis 1')
m = np.argmax(arr, axis = 1)
print (m)
print ('argmin() function')
m = np.argmin(arr)
print (m)
print ('Flattened array')
print (arr.flatten()[m])
print ('Flattened array along axis 0')
m= np.argmin(arr, axis = 0)
print (m)
print ('Flattened array along axis 1' )
m = np.argmin(arr, axis = 1)
print (m)
OUTPUT
argmax() function
2
Index of maximum number in flattened array
[6 2 9 1 0 3 8 4 7]
Array containing indices of maximum along axis 0
[2 2 0]
Array containing indices of maximum along axis 1
[2 2 0]
argmin() function
4
Flattened array
0
Flattened array along axis 0
[1 1 1]
Flattened array along axis 1
[1 1 1]
If you have not seen the earlier tutorials