NumPy Basic tutorial 3 - Python Programming

NumPy Baisc tutorial 3 - Python Programming Harsha Navalkar blog Daily Experience
NumPy Baisc tutorial 3 - Python Programming


Open Anaconda Navigator
Launch Sypder

If you have not seen the earlier tutorials 

PROGRAM 1
import numpy as np
arr = np.arange(0,12,2)
print('Original array')
print(arr)
arr = arr.reshape(2,3)

print('Modified array 1')
print(arr)
for x in np.nditer(arr):
   print (x)
   
print ('Transpose of the original array') 
trans = arr.T 
print (trans)
   
print ('Sorted in C-style order')
cstyle = trans.copy(order = 'C')
print (cstyle)
for x in np.nditer(cstyle):
   print (x)

print ('Sorted in F-style order')
fstyle = trans.copy(order = 'F')
print (fstyle)
for x in np.nditer(fstyle):
   print (x)

for x in np.nditer(arr, op_flags = ['readwrite']):
   x[...] = 2*x
print ('Modified array 2')
print (arr)

OUTPUT
Original array
[ 0  2  4  6  8 10]
Modified array 1
[[ 0  2  4]
 [ 6  8 10]]
0
2
4
6
8
10
Transpose of the original array
[[ 0  6]
 [ 2  8]
 [ 4 10]]
Sorted in C-style order
[[ 0  6]
 [ 2  8]
 [ 4 10]]
0
6
2
8
4
10
Sorted in F-style order
[[ 0  6]
 [ 2  8]
 [ 4 10]]
0
2
4
6
8
10
Modified array 2
[[ 0  4  8]
 [12 16 20]]

PROGRAM 2
import numpy as np
arr = np.arange(0,12,2)
print('Original array')
print(arr)
arr = arr.reshape(2,3)

print ('Modified array F' )
for x in np.nditer(arr, flags = ['external_loop'], order = 'F'):
   print (x)

print ('Modified array C' )
for x in np.nditer(arr, flags = ['external_loop'], order = 'C'):
   print (x)

print ('Modified array C' )
for x in np.nditer(arr, flags = ['external_loop'], order = 'A'):
   print (x)

print ('Modified array C' )
for x in np.nditer(arr, flags = ['external_loop'], order = 'K'):

   print (x)

OUTPUT
Original array
[ 0  2  4  6  8 10]
Modified array F
[0 6]
[2 8]
[ 4 10]
Modified array C
[ 0  2  4  6  8 10]
Modified array C
[ 0  2  4  6  8 10]
Modified array C

[ 0  2  4  6  8 10]

PROGRAM 3
import numpy as np 
arr = np.array([7.0,5.55, 1903, 0.568, 25.532,6.1,1.7]) 
print ('Original array') 
print (arr)
print ('Rounding of digits')
print (np.around(arr) )
print (np.around(arr, decimals = 1) )
print (np.around(arr, decimals = -1))
print (np.around(arr, decimals = 2) )
print('Modified array after floor function')
print (np.floor(arr))
print('Modified array after ceil function')
print (np.ceil(arr))

OUTPUT
Original array
[7.0000e+00 5.5500e+00 1.9030e+03 5.6800e-01 2.5532e+01 6.1000e+00
 1.7000e+00]
Rounding of digits
[7.000e+00 6.000e+00 1.903e+03 1.000e+00 2.600e+01 6.000e+00 2.000e+00]
[7.000e+00 5.600e+00 1.903e+03 6.000e-01 2.550e+01 6.100e+00 1.700e+00]
[  10.   10. 1900.    0.   30.   10.    0.]
[7.000e+00 5.550e+00 1.903e+03 5.700e-01 2.553e+01 6.100e+00 1.700e+00]
Modified array after floor function
[7.000e+00 5.000e+00 1.903e+03 0.000e+00 2.500e+01 6.000e+00 1.000e+00]
Modified array after ceil function
[7.000e+00 6.000e+00 1.903e+03 1.000e+00 2.600e+01 7.000e+00 2.000e+00]


PROGRAM  4
import numpy as np 
arr1 = np.arange(9, dtype = np.float_).reshape(3,3) 

print ('array 1')
print (arr)
print ('array 2')
arr2 = np.array([10,20,30]) 
print(arr2)

print ('Addition')
print (np.add(arr1,arr2) )

print ('Subtraction')
print (np.subtract(arr1,arr2) )

print ('Multiplication')
print (np.multiply(arr1,arr2) )  

print ('Division')

print (np.divide(arr1,arr2) )  

OUTPUT
array 1
[7.0000e+00 5.5500e+00 1.9030e+03 5.6800e-01 2.5532e+01 6.1000e+00
 1.7000e+00]
array 2
[10 20 30]
Addition
[[10. 21. 32.]
 [13. 24. 35.]
 [16. 27. 38.]]
Subtraction
[[-10. -19. -28.]
 [ -7. -16. -25.]
 [ -4. -13. -22.]]
Multiplication
[[  0.  20.  60.]
 [ 30.  80. 150.]
 [ 60. 140. 240.]]
Division
[[0.         0.05       0.06666667]
 [0.3        0.2        0.16666667]

 [0.6        0.35       0.26666667]]

PROGRAM 5
import numpy as np 
arr = np.array([10,100,1000]) 
print(arr)

print (' power function 1')
print (np.power(arr,2) )

print (' power function 2')

print (np.power(arr,3) )

OUTPUT
[  10  100 1000]
 power function
[    100   10000 1000000]
 power function

[      1000    1000000 1000000000]

PROGRAM 6
import numpy as np 
arr = np.array([-2.9j, 7.2j, 16. , 18+11j]) 

print ('Original array')
print (arr) 

print ('real() function:') 
print (np.real(arr))

print (' imag() function' )
print (np.imag(arr)) 

print ('conj() function:' )
print (np.conj(arr)) 

print (' angle() function' )
print (np.angle(arr)) 
  
print (' angle() function') 
print (np.angle(arr, deg = True))

print (' angle() function') 

print (np.angle(arr, deg = False))

OUTPUT
Original array
[-0. -2.9j  0. +7.2j 16. +0.j  18.+11.j ]
real() function:
[-0.  0. 16. 18.]
 imag() function
[-2.9  7.2  0.  11. ]
conj() function:
[-0. +2.9j  0. -7.2j 16. -0.j  18.-11.j ]
 angle() function
[-1.57079633  1.57079633  0.          0.5485494 ]
 angle() function
[-90.          90.           0.          31.42956561]
 angle() function

[-1.57079633  1.57079633  0.          0.5485494 ]


Also check NUMPY TUTORIAL 4



NumPy Basic tutorial 3 - Python Programming