Hello Guys today i am going to show you how to use python numpy library for fast array computation.
What is NumPy?
NumPy is a Python library used for working with arrays.
It also has functions for working in domain of linear algebra, fourier transform, and matrices.
NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.
NumPy stands for Numerical Python.
Why Use NumPy?
In Python we have lists that serve the purpose of arrays, but they are slow to process.
NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
This behavior is called locality of reference in computer science.
This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.
Arrays are very frequently used in data science, where speed and resources are very important.
Install -
C:\Users\Your Name>pip install numpy
Simple Example -
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
Output-
[1 2 3 4 5]
Using Tuple in numpy -
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
Output-
[1,2,3,4,5]
0-D array -
import numpy as np
arr = np.array(42)
print(arr)
Output-
42
1-D array -
import numpy as np
arr = np.array([1,3,5,7,9,11])
print(arr)
Output -
[1,3,5,7,9,11]
2-D array -
import numpy as np
arr = np.array([[1,3,5],[7,9,11]])
print(arr)
Output -
[[1,3,5] [7,9,11]]
3-D array -
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
Output -
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
Check Number of Dimensions -
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr.ndim)
Output -
3
Higher Dimensional Arrays -
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)
Output -
[[[[[1 2 3 4]]]]]
number of dimensions : 5
This is just a basic introduction of Numpy , You can read the full documentation at below link
Numpy Documentation - https://numpy.org/
THANK YOU FOR READING THIS POST AND IF YOU FIND ANY MISTAKE OR WANT TO GIVE ANY SUGGESTION PLEASE KINDLY MENTION IT IN THE COMMENT SECTION.
Top comments (0)