DEV Community

Cover image for Numpy in Python
Shubham Tiwari
Shubham Tiwari

Posted on • Updated on

Numpy in Python

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
Enter fullscreen mode Exit fullscreen mode

Simple Example -

import numpy

arr = numpy.array([1, 2, 3, 4, 5])

print(arr)
Enter fullscreen mode Exit fullscreen mode

Output-

[1 2 3 4 5]
Enter fullscreen mode Exit fullscreen mode

Using Tuple in numpy -

import numpy as np

arr = np.array((1, 2, 3, 4, 5))

print(arr)
Enter fullscreen mode Exit fullscreen mode

Output-

[1,2,3,4,5]
Enter fullscreen mode Exit fullscreen mode

0-D array -

import numpy as np

arr = np.array(42)

print(arr)
Enter fullscreen mode Exit fullscreen mode

Output-

42
Enter fullscreen mode Exit fullscreen mode

1-D array -

import numpy as np

arr = np.array([1,3,5,7,9,11])

print(arr)
Enter fullscreen mode Exit fullscreen mode

Output -

[1,3,5,7,9,11]
Enter fullscreen mode Exit fullscreen mode

2-D array -

import numpy as np

arr = np.array([[1,3,5],[7,9,11]])

print(arr)
Enter fullscreen mode Exit fullscreen mode

Output -

[[1,3,5] [7,9,11]]
Enter fullscreen mode Exit fullscreen mode

3-D array -

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(arr)
Enter fullscreen mode Exit fullscreen mode

Output -

[[[1 2 3]
  [4 5 6]]

 [[1 2 3]
  [4 5 6]]]
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output -

3
Enter fullscreen mode Exit fullscreen mode

Higher Dimensional Arrays -

import numpy as np

arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr)
print('number of dimensions :', arr.ndim)
Enter fullscreen mode Exit fullscreen mode

Output -

[[[[[1 2 3 4]]]]]
number of dimensions : 5
Enter fullscreen mode Exit fullscreen mode

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)