NumPy Basic tutorial 1 - Python Programming

NumPy Basic tutorial 1 - Python Programming


OPEN Anaconda Navigator
Launch spyder

PROGRAM 1
import numpy as np 
arr = np.array([1,2,3,4,5,6,7]) 
print (arr)

OUTPUT
[1 2 3 4 5 6 7]  //displays the array

PROGRAM 2
import numpy as np 
arr = np.array([[1, 2,3], [3, 4, 5],[5,6, 7]]) 
print (arr)

OUTPUT
[[1 2 3]     // displays the 3x3  matrix 
 [3 4 5]
 [5 6 7]]

PROGRAM 3
import numpy as np 
arr = np.array([5,6,7,8], dtype = complex) 
print (arr)

OUTPUT
[5.+0.j 6.+0.j 7.+0.j 8.+0.j]  //displays complex numbers 

PROGRAM 4
import numpy as np 
employee = np.dtype([('ename','S10'), ('eage', 'i1'), ('esalary', 'f4')]) 
print (employee)

OUTPUT
[('ename', 'S10'), ('eage', 'i1'), ('esalary', '<f4')] //S is string, i is integer and f is float and the numbers adjacent to it are the lengths  

PROGRAM 5
import numpy as np 
employee = np.dtype([('ename','S10'), ('eage', 'i1'), ('esalary', 'f4')]) 
print (employee)
arr = np.array([('Harsha', 21, 500),('Sobi', 18, 759)], dtype = employee) 
print (arr)

OUTPUT
[('ename', 'S10'), ('eage', 'i1'), ('esalary', '<f4')]
[(b'Harsha', 21, 500.) (b'Sobi', 18, 759.)]

PROGRAM 6
import numpy as np 
arr = np.array([[0,0,1],[1,0,0]]) 
print (arr.shape)

OUTPUT
(2, 3) //returns (2,3) because the matrix has 2 rows and 3 columns

PROGRAM 7
import numpy as np 
arr = np.array([[0,0,1],[1,0,0],[2,3,4]]) 
print (arr.shape)

OUTPUT
(3, 3) //returns (3,3) because the matrix has 3 rows and 3 columns

PROGRAM 8
import numpy as np 
arr = np.arange(20) 
print (arr)

OUTPUT
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

PROGRAM 9
import numpy as np 
x = np.array([1,2,3], dtype = np.int8) 
print (x.itemsize)

OUTPUT
1

PROGRAM  10
import numpy as np 
x = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype = np.int8) 
print (x.itemsize)

OUTPUT
1

PROGRAM  11
import numpy as np 
x = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype = np.float32) 
print (x.itemsize)

OUTPUT
4

PROGRAM 12
import numpy as np 
arr = np.array([1,2,3,4,5,6,7,8,9]) 
print (arr.flags)

OUTPUT
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

PROGRAM 13
import numpy as np 
arr = np.zeros(10) 
print (arr)

OUTPUT
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

PROGRAM 14
import numpy as np 
arr = np.zeros(10,dtype = np.int) 
print (arr)

OUTPUT
[0 0 0 0 0 0 0 0 0 0]

PROGRAM  15
import numpy as np 
arr = np.zeros((3,3), dtype = [('x', 'i4'), ('y', 'i4'),('z','f4')])  
print (arr)

OUTPUT
[[(0, 0, 0.) (0, 0, 0.) (0, 0, 0.)]
 [(0, 0, 0.) (0, 0, 0.) (0, 0, 0.)]
 [(0, 0, 0.) (0, 0, 0.) (0, 0, 0.)]]

PROGRAM 16
import numpy as np 
arr = np.zeros(10) 
print (arr)

OUTPUT
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

PROGRAM 17
import numpy as np 
arr = np.ones(10,dtype = np.int) 
print (arr)

OUTPUT
[1 1 1 1 1 1 1 1 1 1]

PROGRAM  18
import numpy as np 
arr = np.ones((3,3), dtype = [('x', 'i4'), ('y', 'i4'),('z','f4')])  
print (arr)

OUTPUT
[[(1, 1, 1.) (1, 1, 1.) (1, 1, 1.)]
 [(1, 1, 1.) (1, 1, 1.) (1, 1, 1.)]
 [(1, 1, 1.) (1, 1, 1.) (1, 1, 1.)]]

PROGRAM  19
import numpy as np 
arr = np.zeros((3,3), dtype = [('x', 'i4'), ('y', 'i4'),('z','i4')])  
print (arr)

OUTPUT
[[(0, 0, 0) (0, 0, 0) (0, 0, 0)]
 [(0, 0, 0) (0, 0, 0) (0, 0, 0)]
 [(0, 0, 0) (0, 0, 0) (0, 0, 0)]]

PROGRAM  20
import numpy as np 
arr = np.ones((3,3), dtype = [('x', 'i4'), ('y', 'i4'),('z','i4')])  
print (arr)

OUTPUT
[[(1, 1, 1) (1, 1, 1) (1, 1, 1)]
 [(1, 1, 1) (1, 1, 1) (1, 1, 1)]
 [(1, 1, 1) (1, 1, 1) (1, 1, 1)]]

ALSO CHECK
NumPy Basic tutorial 2 - Python Programming  TUTORIAL 2
NumPy Basic tutorial 3 - Python Programming  TUTORIAL 3
NumPy Basic tutorial 4 - Python Programming  TUTORIAL 4


NumPy Basic tutorial 1 - Python Programming