If you are starting your Data Science journey or want to integrate Data Science into your product, you must have heard of the name NumPy library.
Didn’t heard of it?
Ok, let’s talk about it first! NumPy is a library for Python, which is essential for scientific computation. Actually, it’s a multidimensional array of object. It’s almost similar to Python list but there are some differences between Python list & Numpy array which make NumPy best.
Let’s talk about the differences & why choose NumPy array instead of the Python built-in list:
- Numpy array can only contain the object of same data type object. On the other hand, the Python list can contain multiple data type objects.
- It is faster & uses memory efficiently.
- It has built-in powerful attributes & methods, which make scientific computation much easy & perfect.
Why is NumPy faster in computation & how does it use memory efficiently than the Python list?
Python list is the array of pointers to objects. Which takes at least 16 bytes of memory for each object (4 bytes for the pointer, 4 bytes for reference count, 4 bytes for value and so on …).
On the other hand, numpy array is the array of uniform values. Here, the single precision number takes 4 bytes of memory and the double precision takes 8 bytes.
Due to this memory occupying fact numpy is better than the list in memory efficiency & can compute faster than list. Moreover, you get a lot of operational tool for vector & matrix which allow someone avoid unnecessary work during their scientific computation with it.
Interesting fact about Mathematical manipulation in Python
Let’s say, you want to perform some calculation with the list like divide one list with another (let alone complex ones), you can’t do that with the list directly. You must use there numpy array.
import numpy as np x = np.arange(1,11) y = np.arange(2,21,2) print(y/x)
output: [ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
x = list(range(1,11)) y = list(range(2,21,2)) print(y/x)
output: Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> print(y/x) TypeError: unsupported operand type(s) for /: 'list' and 'list'
That’s all. You may also experiment with execution time for numpy array & list. There also you will find that numpy beats the list.
Hope, you enjoyed the article. Please Comment your thought & Share the article. Thanks for reading