NumPy Introduction

NumPy Introduction

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 Olliphant. 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, i.e. n-dimensional array. It provides a lot of functionality such as:

  • Performing mathematical and logical operations on arrays.
  • Fourier transforms and shape manipulation.
  • Operations related to linear algebra. NumPy has inbuilt functions for linear algebra and random number generation.

Installation

If you have Python and PIP already installed on a system, then installation of NumPy is very easy.
Install it using this command:

pip install numpy

Example 1: Creating NumPy Arrays

NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function.

import numpy as np

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

print(arr)

Output:

[1 2 3 4 5]

To create a NumPy ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:

import numpy as np

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

print(arr)

Output:

[1 2 3 4 5]

Example 2: Dimensions in Arrays

A NumPy array can have any number of dimensions. When the array is created, you can define the number of dimensions by using the ndmin argument.

import numpy as np

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

print(arr)
print('shape of array :', arr.shape)

Output:

[[[[[1 2 3 4]]]]]
shape of array : (1, 1, 1, 1, 4)

It means that the array has 5 dimensions, and one element that is 4 in the last dimension.

Example 3: Accessing Array Elements

Array indexing is the same as accessing an array element.

import numpy as np

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

print(arr[0])

Output:

1

Accessing a 2-D array is simply including an additional set of square brackets for the second dimension of the array.

import numpy as np

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

print('2nd element on 1st dim: ', arr[0, 1])

Output:

2nd element on 1st dim:  2

To access elements from 3-D arrays:

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(arr[0, 1, 2])

Output:

6

Conclusion

NumPy is an efficient library for working with arrays. It provides a lot of functionality that can be used by developers to perform mathematical, and logical operations on arrays. In this tutorial, we discussed how to create a NumPy array, which is the basic datatype provided by NumPy. We also showed how to access array elements and how to differentiate the dimensions in the arrays. These are just some of the things we can do with NumPy, as there is a lot more to explore with NumPy.

Leave a Reply

Your email address will not be published. Required fields are marked *