Python Numpy
Numpy is the python library working with numerical data,this library consists of multiple libraries and a multidimensional array.It is an open source project.
Why Numpy
We have seen that python using the list aims at the serve of array but it is the slow process Numpy provides very fast processing of list (array). Array in numpy is called ndarray.
Numpy provides the function to deal with n dimension array very quickly and easy
Operation in Numpy
Arithmetic Operations
Program
import numpy as abc #initialization of array a =abc.arange(4).reshape(2, 2) #reshape gives the matrix dimension print('first array:') print(a)#print array print('second array:') a1 = abc.array([20, 40]) #initialization array a1 print(a1) print('add array') print(abc.add(a, a1))#add two array print('subtract arrays:') print(abc.subtract(a, a1))#similarly subtract array print('multiply array:') print(abc.multiply(a, a1))#multiply two array print('divide array:') print(abc.divide(a, a1))#divide two array
Output
first array: [[0 1] [2 3]] second array: [20 40] add array: [[20 41] [22 43]] subtract two array: [[-20 -39] [-18 -37]] multiply array: [[ 0 40] [ 40 120]] divide array: [[0 0.025] [0.1 0.075]]
Note :numpy consists of the array funciton dimension .
Numpy Power()
import numpy as abc #import numpy and creates objects abc arr = abc.array([5, 10, 15]) #initialization of array print('simple array:') print(arr) print('power function:') print(np.power(arr, 20))#invokes the power functions
Output
simple array: [ 5 10 15] power function: [95367431640625 7766279631452241920 4664335276710460609]
Numpy mod()
import numpy as abc #import numpy and creates the objects abc a = abc.array([10, 25, 18]) #creates two array a and b b = abc.array([8,9,11]) print('first array:') print(a) #print array a and b print(‘second array:') print(b) print('mod function:') print(abc.mod(a,b)) # invokes mod function print('remainder function:') print(abc.remainder(a,b))#invokes remainder function
Mod and remainder function libraries consist by the numpy.
Output
first array: [10 25 18] second array: [ 8 9 11] mod function: [2 7 7] remainder function: [2 7 7]
Slicing
Array slicing is one of the important concepts of Python, It makes little bit confusing to the beginners to understand the concept of the Slicing. Now we will see the Slicing concept along with the demonstration.
Syntax data[from:to]
data[start:stop:increment]
One Dimensional Slicing
Program
#importing numpy from numpy import array #array define data = array([45,53,53,34,53,52,452]) print(data[:1])
Output
[ 45 ]
Program
#simple slicing from numpy import array #array define data = array([45,53,53,34,53,52,452]) print(data[-3:])
Output
[ 53 52 452]
Two Dimensional Slicing
Program
import numpy as np data= np.array([[23,234,23423,42,34], [2,653,6,35,33]]) print(data[1, 1:3])
Output
[653 6]
Program
import numpy as np data= np.array([[23,234,23423,42,34], [2,653,6,35,33]]) print(data[0:1, 1:3])
Output
[[ 234 23423]]
Numpy Indexing
One Dimensional Indexing
Program
import numpy as np arr = np.array([11,22,33,44,55,66,77,88]) print(arr[1])
Output
[2]
Program
import numpy as np arr = np.array([11,22,33,44,55,66,77,88]) print(arr[-5])
Output
[44]
Two Dimensional Indexing
Program 1
import numpy as np arrays = np.array([[11,22,33,44,55], [66,77,88,99,100]]) print('3nd element on 2st dim: ', arrays[1, 2])
Output
3nd element on 2st dim: 88
Program 2
import numpy as np arrays = np.array([[11,22,33,44,55], [66,77,88,99,100]]) print('Backward printing 1nd element on 1st dim: ', arrays[-1,-1])
Output
3nd element on 2st dim: 100