Magic Of NumPy.

  April 18, 2020


NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.



Why do we use NumPy arrays?


There are basically three reasons of using NumPy:

  • it uses less memory
  • it is fast
  • it is convinient

In this article, we'll look practically each of the above mentioned reasons that how and why NumPy arrays are more powerful than normal arrays in Python.


How NumPy arrays use less memory?


import numpy as np
import time
import sys

Normal Arrays
ls = range(1000) 	
print(sys.getsizeof(5)*len(ls))

NumPy Arrays
ds = np.arange(1000)
print(ds.size*ds.itemsize)



# EXPLANATION

# We have taken integers from 0 to 999 using range() and arange() function

# getsizeof(5) refers to the size of one digit(we could have passed any digit in getsizeof(...)),
# then we are performing mutiplication with total number of items in ls
# ds.size gives the total number of elements in ds and ds.itemsize gives the size of one element

Output:


28000
8000

In the above example, it takes memory size of 28000 for storing 1000 elements in normal arrays whereas NumPy arrays take only a memory size of 8000. So, it is clear that NumPy arrays are more memory efficient.


How NumPy arrays perform faster and are convinient?


import numpy as np
import time
import sys

size = 10000000

L1 = range(size) 
# declaring a List L1 for integers upto 10000000
L2 = range(size) 
# declaring another List L2 for integers upto 10000000

A1 = np.arange(size) 
# declaring a NumPy array A1 for integers upto 10000000
A2 = np.arange(size)
# declaring another NumPy array A1 for integers upto 10000000

start = time.time()
result = [(x + y) for x, y in zip(L1, L2)]  
# iterating over L1 and L2 and adding each corresponding element
print((time.time() - start)*1000)

start = time.time()
result = A1 + A2  
# directly adding NumPy arrays
print((time.time() - start)*1000) 

# By multiplying with 1000, the time is converted from seconds to milliseconds

Output:


1277.080774307251
280.78198432922363

In the above example, adding two normal arrays take 1277.0807... milliseconds whereas adding two NumPy arrays take 280.7819... milliseconds. Also, the way of adding two NumPy arrays is far better than adding two normal arrays and this makes NumPy arrays more convinient in use.


And we can do a lot with NumPy.


import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,3*np.pi,0.1)

y = np.sin(x)
plt.plot(x,y)
plt.show()

y = np.cos(x)
plt.plot(x,y)
plt.show()

y = np.exp(x)
plt.plot(x,y)
plt.show()

Output:






Conclusion


NumPy arrays are more powerful than the normal arrays we use in Python. It is widely used in Machine Learning, Data Analytics, Artificial Intellingence, etc.



Go to Blogs       Any Feedback?   

Share: