Introduction to NumPy
NumPy is a core library for scientific and numerical computing in Python. It is an abbreviation for Numerical Python. It provides high-performance multi-dimensional array object. It provides tools to work with arrays.
There are so many other modules in Python that are built on NumPy. Hence, it is very important to learn the fundamentals of NumPy so that understanding other modules gets easier. The main idea to use NumPy is to make use of multi-dimensional arrays which is a table of elements of same type.
Why to use NumPy array over Python list?
We use NumPy array instead of a list because of below three reasons:
The very first reason to use NumPy is that it takes less memory as compared to list. NumPy arrays were optimized over the years for memory use, to make it work faster which provides performance benefits and are convenient to work with.
import sys import numpy as np num = range(100) print(sys.getsizeof(2)*len(num))
num2 = np.arange(100) print(num2.size * num2.itemsize)
The above output shows that the memory allocated by list is 2800 whereas the memory allocated by the NumPy array is just 800. This shows that there is significant difference between the two and NumPy arrays show optimization in memory usage.
Next, let's talk about how Python NumPy is fast and convenient when compared to list:
import time import sys import numpy as np size = 100000 list1 = range(size) list2 = range(size) array1 = np.arange(size) array2 = np.arange(size) start = time.time() result = [(x+y) for x,y in zip(list1, list2)] print('Time taken by Python list: ', (time.time()-start)*1000)
Output: Time taken by Python list: 16.619205474853516
start=time.time() result = array1+array2 print('Time taken by NumPy array: ', (time.time()-start)*1000)
Output: Time taken by NumPy array: 2.499103546142578
In the above code, we have two lists and two numpy arrays. Time taken by lists for execution is 16ms whereas the NumPy array took almost 2ms. Hence, NumPy array is faster than list.
Also, when we notice that we need to write 'for' loop code for concatenation in list whereas in NumPy arrays, we only used '+' sign to add the arrays.
That's why working with NumPy is easy and convenient when compared to lists.
Python NumPy Operations:
ndim helps to find whether the array is single dimension or multi-dimension.
import numpy as np a = np.array([(1,2,3),(4,5,6)]) print(a.ndim)
itemsize helps to find the size of each element.
import numpy as np a = np.array([(1,2,3)]) print(a.itemsize)
dtype helps to find the datatype along with the size.
import numpy as np a = np.array([(1,2,3)]) print(a.dtype)
size and shape
size and shape helps to find the size and shape of the array.
import numpy as np a = np.array([(1,2,3,4,5,6)]) print(a.size) print(a.shape)
Output: 6 (1,6)
max/ min/ sum
max, min and sum help to find the maximum, minimum as well as the sum of the numpy array.
import numpy as np a=np.array([1,2,3]) print(a.min()) print(a.max()) print(a.sum())
Output: 1 3 6
addition/ subtraction/ multiplicat