NumPy Basic tutorial 2 - Python Programming




NumPy Basic tutorial 2 - Python Programming Harsha Navalkar Blog Daily experience
NumPy Basic tutorial 2 - Python Programming 
Open Anaconda Navigator
Launch Sypder

If you have not seen the 
NumPy Basic tutorial 1 - Python Programming  CLICK HERE

PROGRAM 1
# convert list to ndarray 
import numpy as np 
x = [1,2,3,4,5,6] 
arr = np.asarray(x) 
print (arr)

OUTPUT
[1 2 3 4 5 6]

PROGRAM 2
import numpy as np 
x = [1,2,3,4,5,6] 
arr = np.asarray(x, dtype = float) 
print (arr)

OUTPUT
[1. 2. 3. 4. 5. 6.]

PROGRAM 3
import numpy as np 
x = (1,2,3,4,5,6)
arr = np.asarray(x) 
print (arr)

OUTPUT
[1 2 3 4 5 6]

PROGRAM 4
import numpy as np 
x = (1,2,3,4,5,6)
arr = np.asarray(x, dtype = float) 
print (arr)

OUTPUT
[1. 2. 3. 4. 5. 6.]

PROGRAM 5
# create list object using range function 
import numpy as np 
list = range(10) 
print (list)

OUTPUT
range(0, 10)

PROGRAM 6
import numpy as np 
arr = np.arange(10) 
print (arr)

OUTPUT
[0 1 2 3 4 5 6 7 8 9]

PROGRAM 7
import numpy as np 
arr= np.arange(1,24,4) 
print (arr)

OUTPUT
[ 1  5  9 13 17 21]

PROGRAM 8
import numpy as np 
arr = np.linspace(1,24,4) 
print (arr)

OUTPUT 
[ 1.          8.66666667   16.33333333   24.        ]

PROGRAM 9
import numpy as np 
arr = np.arange(10) 
s = slice(2,10,2) 
print (arr[s])

OUTPUT
[2 4 6 8]

PROGRAM 10 
import numpy as np 
arr = np.arange(10) 
b = arr[2:10:2] 
print (arr[s])

OUTPUT
[2 4 6 8]

PROGRAM 11
import numpy as np 
arr = np.arange(10) 
b = arr[3] 
c=arr[6]
print(c)
print (b)

OUTPUT
6
3

PROGRAM 12
import numpy as np 
arr = np.arange(10) 
b = arr[3] 
c=arr[6]
print(arr[2:])
print(arr[:2])
print(arr[:])
print(arr[1:])
print(arr[2:7])

OUTPUT
[2 3 4 5 6 7 8 9]
[0 1]
[0 1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]
[2 3 4 5 6]

PROGRAM 13 
import numpy as np 
arr = np.array([[1,2,3],[4,5,6],[7,8,9]]) 
print (arr)
print ('after slice operation 1')
print( arr[1:])
print ('after slice operation 2')
print( arr[:1])
print ('after slice operation 3')
print( arr[:])
print ('after slice operation 4')
print( arr[1:3])
print ('after slice operation 5')
print( arr[0:3])

OUTPUT
[[1 2 3]
 [4 5 6]
 [7 8 9]]
after slice operation 1
[[4 5 6]
 [7 8 9]]
after slice operation 2
[[1 2 3]]
after slice operation 3
[[1 2 3]
 [4 5 6]
 [7 8 9]]
after slice operation 4
[[4 5 6]
 [7 8 9]]
after slice operation 5
[[1 2 3]
 [4 5 6]
 [7 8 9]]

PROGRAM 14 
x = np.array([[1, 2], [3, 4], [5, 6]]) 
y = x[[0,1,2], [0,1,0]] 
print(x)
print('after the operation')
print (y)

OUTPUT
[[1 2]
 [3 4]
 [5 6]]
after the operation
[1 4 5]

PROGRAM 15 
import numpy as np 
arr = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8]]) 
print ('Matrix:') 
print(arr)
print('corner elemnets are:')
rows = np.array([[0,0],[2,2]])
cols = np.array([[0,2],[0,2]]) 
mat = arr[rows,cols] 
print (mat)

OUTPUT
Matrix:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
corner elemnets are:
[[0 2]
 [6 8]]

PROGRAM 16 
import numpy as np 
arr = np.array([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8]]) 
print ('Matrix:') 
print(arr)
print ('The items greater than 5 are:' )
print (arr[arr > 5])

OUTPUT
Matrix:
[[0 1 2]
 [3 4 5]
 [6 7 8]]
The items greater than 5 are:
[6 7 8]

PROGRAM 17 
import numpy as np 
arr = np.array([2+9j, 5, 3.5+3j, 6, 7, 6+2j, 8, 0]) 
print('original array')
print(arr)
print('Complex Numbers')
print (arr[np.iscomplex(arr)])

OUTPUT
original array
[2. +9.j 5. +0.j 3.5+3.j 6. +0.j 7. +0.j 6. +2.j 8. +0.j 0. +0.j]
Complex Numbers
[2. +9.j 3.5+3.j 6. +2.j]

PROGRAM 18 
import numpy as np 
a = np.array([10,20,30,40]) 
b = np.array([2,3,4,5]) 
print('a')
print(a)
print('b')
print(b)
c = a * b 
print('c')
print(c)
d=a+b
print('d')
print(d)
e=a-b
print('e')
print(e)
f=a/b
print('f')
print(f)

OUTPUT
a
[10 20 30 40]
b
[2 3 4 5]
c
[ 20  60 120 200]
d
[12 23 34 45]
e
[ 8 17 26 35]
f
[5.         6.66666667 7.5        8.        ]

NumPy Basic tutorial 3 - Python Programming  CLICK HERE
NumPy Basic tutorial 4-  Python Programming  CLICK HERE


NumPy Basic tutorial 2 - Python Programming