Getting Started with Python’s NumPy Library

StefanBStreet
6 min readOct 24, 2021

--

As the name suggests, NumPy is a standard Python library that can be used for performing different kinds of numerical operations. This module is especially useful for creating arrays and playing with multidimensional arrays which is critical to machine learning. The module extends its support to various trigonometric, algebraic, and statistical functions. Thus, performing a complex numerical operation with NumPy can be a breeze.

After going through this article, you will be able to :

  • Understand the benefits of using the NumPy Module
  • Installing a NumPy Module
  • Creating Arrays with NumPy
  • Check the dimension, size, and shape of a NumPy Array object.

Benefits of using the NumPy Module

Ease of Use

The module provides a lot of functions that can be used to perform complex computations. The user can just use these functions and does not have to bother about the underlying complexities.

For example, to compute a matrix multiplication of two matrices without using numpy, one can code as follows :

for m1, m2 in zip(matrix1,matrix2): 
result += m1 * m2

In the above code, we use a loop to traverse through the matrix elements and then multiply the elements and finally add them. There are three major operations and it becomes really hard to debug the code if something doesn’t work as expected.

On the contrary, using numpy, matrix multiplication can be performed with a single line of code as shown below

numpy.matmul(matrix 1,matrix 2)

The above code looks clean and is easily understandable which is key to being a good programmer or data scientist

Computational Performance

The functions and operations in NumPy Module are mostly implemented in C. Thus, the performance is much better when compared to using the Python loops and statements

Below is a quick comparison for your reference.

#creating two normal arrays 
matrix1=list(range(1000))
matrix2=list(range(0,1000))
#creating numpy arrays
arr1=np.array(matrix1)
arr2=np.array(matrix2)

Let us check the time taken for the computation using Python Loop and Statement

%%time # outputs the time spent on the computation in jupyter result=0 
for m1, m2 in zip(matrix1,matrix2):
result += m1 * m2

The output is

CPU times: user 320 µs, sys: 0 ns, total: 320 µs 
Wall time: 341 µs

Now, let us try using the NumPy function to perform the same computation

%%time 
# outputs the time spent on the computation in jupyter np.matmul(arr1,arr2)

The output is

CPU times: user 32 µs, sys: 7 µs, total: 39 µs 
Wall time: 42.4 µs

Note that using Numpy is almost 10 times faster than using the Python operations by themselves.

Cross Module Support

There are many modules like Pandas, Scikit, etc that rely on NumPy objects. NumPy operations can be used on a pandas data frame, scikit-learn, etc.

Now that we have seen the benefits of NumPy, let us get started on how to use NumPy and NumPy objects.

Installing NumPy Module

To use the NumPy objects and function in your program you have to first install the NumPy Module.

To install NumPy in your system using an anaconda prompt, type the following,

> conda install numpy

When you are prompted, enter y.

To install NumPy in your system using pip, open the terminal window and type,

$ pip install numpy

In order to check if the NumPy module has been installed successfully, enter the below code,

import numpy
numpy.__version__

You should be able to see a version number in the output as shown below

‘1.20.3’

Creating a NumPy Array

There are a lot of functions in the numpy module that can be used to create numpy arrays. In this tutorial let us look at a basic one. The easiest way of creating a NumPy Array is by converting a python list to a numpy array. To do so we use the function array

Creating a 1D array

Passing a list object to the array function creates a one-dimensional array. For example, if you have a python list [1,2,3,4,5], you can pass this to the array function to create a 1D array as shown below

import numpy as np 
# creating a numpy array with the Python list. arr_1d=np.array([1,2,3,4,5]) print(arr_1d)

The output will be a numpy array as shown below

[1 2 3 4 5]

Note that the array object created in NumPy will be of the type ndarray.

To verify the type of the array arr1, enter the below command

type(arr_1d)

The output is

numpy.ndarray

Creating a 2D Array

Passing nested lists to the array function creates a multi-dimensional array. For example, if you have a python list [[1,2,3],[2,3,4]], you can pass this to the array function to create a two-dimensional array as shown below.

import numpy as np 
# creating a numpy array with a nested Python list. arr_2d=np.array([[1,2,3],[2,3,4]]) print(arr_2d)

The output is

[[1 2 3]
[2 3 4]]

Creating a 3D Array

If you have a python list [[[1,2,3],[2,3,4]],[[1,1,1],[2,2,2]]], you can pass this to the array function to create a three-dimensional array as shown below.

import numpy as np 
# creating a numpy array with a nested
# Python list.
arr_3d=np.array([[[1,2,3],[2,3,4]],[[1,1,1],[2,2,2]]]) print(arr_3d)

The Output is

[[[1 2 3] [2 3 4]] [[1 1 1] [2 2 2]]]

Identifying the size of a given array

To identify the size(total number of elements) of a given array, the ndarray attribute size can be used.

Syntax :

ndarray.size

where, ndarray is a numpy array object.

This returns the size of the given array as an integer value.

Examples :

Let us use the arrays we had created earlier(arr_1d,arr_2d,arr_3d) and identify the size of the given array.

arr_1d.size 
# 5
arr_2d.size
# 6
arr_3d.size
# 12

As seen in the above examples, the ndarray attribute size returns the total number of elements in the given array.

Identifying the dimension of a given array

To identify the dimension of a given array, the ndarray attribute ndim can be used.

Syntax :

ndarray.ndim

where, ndarray is a numpy array object.

This returns an integer value that represents the dimension. For example, 1 is returned for a 1D array, 2 is returned for a 2D array.

Examples :

arr_1d.ndim 
# 1
arr_2d.ndim
# 2
arr_3d.ndim
# 3

As seen in the examples above, the ndarray attribute ndim returns the dimension of the numpy array objects.

Identifying the shape of a given array

Shape of an array represents the number of elements along each axis. This can be identified with the shape attribute of a ndarray object.

Syntax :

ndarray.shape

where, ndarray is a numpy array object.

This returns the shape of a given array in the form of a tuple.

Examples :

arr_1d.shape
# (5,)

Note that the shape returns a tuple value. In Python, even tuples with a single element have a comma. (5,) says that the array has one dimension and there are 5 elements along the axis 0.

arr_2d.shape
# (2, 3)

The output says that there are 2 dimensions(axis 0,1) for this array and there are 2 elements along axis 0 and 3 elements along axis 1.

The output says that there are 3 dimensions(axis 0,1,2) for this array and there are 2 elements along axis 0, 2 elements along axis 1, and 3 elements along axis 2.

arr_3d.shape
# (2, 2, 3)

You can extract the individual values from the tuple as shown below:

arr_3d.shape[0] 
# 2
arr_3d.shape[1]
# 2
arr_3d.shape[2]
# 3

You can even store these values in variables as shown below

x,y,z=arr_3d.shape print(x,y,z) 
# 2 2 3

Conclusion

We hope this article has been informative. In this article, we have covered the basic stuff to get started with NumPy. Thank you for reading. Follow along with us for more detailed tutorials.

Till then Happy Pythoning!

Authored by: Anusha Pai
About: Anusha is a Software Engineer with good experience in the IT industry. She has been using Python throughout her career. She has a passion for writing. She loves writing about Windows and Python-related stuff. In her free time, she practices Yoga.

Originally published at http://beapython.dev on October 24, 2021.

--

--

StefanBStreet
StefanBStreet

Written by StefanBStreet

Stefan is a senior SDE at Amazon with 7+ years of experience in tech. He is passionate about sharing the thing he enjoys learning to others

No responses yet