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:
Less memory
Fast
Convenient
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))
Output:
2800
num2 = np.arange(100)
print(num2.size * num2.itemsize)
Output:
800
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
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)
Output:
2
itemsize
itemsize helps to find the size of each element.
import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)
Output:
8
dtype
dtype helps to find the datatype along with the size.
import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)
Output:
int64
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/ multiplication/ division operation
By using the signs +, -, * and /, we can perform addition, subtraction, multiplication and division on two matrices.
import numpy as np
x = np.array([(1,2,3),(4,5,6)])
y = np.array([(1,2,3),(4,5,6)])
print(x+y)
print(x-y)
print(x*y)
print(x/y)
Output:
[[2 4 6]
[8 10 12]]
[[0 0 0]
[0 0 0]]
[[1 4 9]
[16 25 36]]
[[1. 1. 1.]
[1. 1. 1.]]
Conclusion:
NumPy array is very fast compared to Python lists. Multiple programmers optimized numpy arrays over the years to make its working fast and very quick than the basic Python lists.
Lists and NumPy both are useful for insertion, deletion, appending, concatenation, etc. but NumPy array provides lot more functionality than the Python list.
NumPy arrays were optimized for memory use as well, hence we see performance benefits with NumPy arrays. It uses less bytes of memory and utilizes continuous memory which makes it faster to process.
Lists uses memory which is scattered around. It contains pointers to actual information that is scattered around our computer memory. That's why lists are not super fast.
The operations in NumPy are easy, useful and convenient.
Thanks for reading!
Good one Sana!!!