Understanding NumPy arrays in simple way

Sanpreet Singh
6 min readOct 3, 2019

INTRODUCTION

Hello guys, this engrossing tutorial is about numpy in python and will cover the basics of NumPy arrays. At the end of this tutorial, you will come to know about NumPy arrays, the difference between NumPy and list, how to install NumPy, indexing, slicing, and numerical operations on these arrays. I will try my best to make this tutorial useful to you with the help of codes, effective and useful content that can help you to get a job in the data science profession.

Introduction to Numpy Arrays

In one line NumPy is an array processing package. An array is a data structure that contains a group of elements and all these elements are of the same data type. NumPy provides a multidimensional object called ndarray. It utilizes an optimized C API to make array operations particular fast. Using Numpy, logical and mathematical operations on an array can be performed as well as one can play with indexing and slicing. See the below image to know about the internal organization of NumPy array.

Components of Numpy Array
Components of Numpy Array

In NumPy array, data is contained in the data buffer (contiguous block of memory containing fixed-size data) as well as there is another set of data that describes how to interpret this data contained in the data buffer. Please have a look at the below points for such

a.) Data element’s size in bytes.
b.) Start of data in the data buffer.
c.) Number and size of each dimension
d.) Separation between elements in each dimension (stride).
e.) Mode of data buffer.
f.) dtype of data stored in data buffer.

The advantage is that meta information can be changed to bring change in the data buffer without playing with a data buffer. For example, the shape of the data array can be changed from meta-information without changing anything in the data buffer.

Numpy vs List

Let us start this section with a simple difference. List contains data belonging to different data types whereas NumPy contains data belonging to the same data type. Because of this reason, list does not support vectorized operations such as element-wise addition and multiplication. Another technical difference is since there are different elements belonging to different dtypes stored in a list, hence python has to store type information for these elements. It has to execute type dispatching code while operating on these elements. This in turn, restricts very less list operation to be carried out by c loops. Another key difference is that arrays have a fixed size at creation whereas list can grow dynamically. If one changes the size of ndarray, the previous array is deleted and a new is created. Let us see the speed comparison between array and list using the below screenshot.

Execution time Numpy vs List
Execution time Numpy vs List

Installing Numpy arrays

This section will deal with installing NumPy array. I will demonstrate this section using pip. Two important things to focus on here is pip and virtualenv. pip is a package management system. It is used to install and manage software packages written in python. Virtualenv is an environment where you install python packages or operate on some code without interfering with system python. I personally believe that the best way to work on some code in python is to create virtualenv. So installing NumPy will be done in two phases.

Phase 1: Installing and creating virtual environment
Phase 2: installing NumPy in virtual environment using pip

Phase 1: I am creating these steps with respect to ubuntu. Steps will differ if a user works on window. Install virtual environment using below command

sudo apt-get install virtualenv

If this is already installed, then you will have output similar to this.

Virtual Environment for python
Virtual Environment for python

Now, you can create virtual environment with either python 2 or python 3 using the below command

virtualenv --python python2 **name_of_env** (python2)
virtualenv --python python3 **name_of_env** (python3)

Once the virtual environment is created, it is time to activate it. Please follow the below instructions to activate it.

source  **name_of_env**/bin/activate

Phase 2: You have successfully activated the virtual environment in phase 1. Now it is time to install NumPy using pip. Please type the below instruction in the terminal

pip install numpy

Indexing and Slicing NumPy arrays in n dimensions

This section will cover operations such as indexing and slicing NumPy arrays in 1, 2, and 3 dimensions. NumPy arrays for these dimensions will be created and operations will be explained with the help of live codes.

Indexing and Slicing in 1-dimensional NumPy array

Indexing and Slicing in 1 dimensional numpy array
Indexing and Slicing in 1-dimensional NumPy array

Indexing and Slicing in 2-dimensional NumPy array

Indexing and Slicing in 2 dimensional numpy array
Indexing and Slicing in 2-dimensional NumPy array

Indexing and Slicing in 3-dimensional NumPy array

Indexing and Slicing in 3 dimensional numpy array
Indexing and Slicing in 3-dimensional NumPy array

Numerical operations on arrays

Let us start with a one-dimensional array

Numerical Operations in 1D NumPy Array
Numerical Operations in 1D NumPy Array

Let us proceed with two-dimensional arrays

Numerical Operations in 2D NumPy Array
Numerical Operations in 2D NumPy Array

Let us proceed with three-dimensional arrays

Numerical Operations in 3D NumPy Array
Numerical Operations in 3D NumPy Array

Conclusion

I hope readers enjoyed reading this article and got a good understanding of NumPy arrays, their internal structure, why they are faster than list. Also I tried to explain in a simple sense how to install NumPy with virtual environment along with the importance of virtual environment. In the end, I went with indexing, slicing, and numerical operations with NumPy arrays. Please practice with your own code so that better insights can be developed. Many people get confused with axis 0, axis 1, and axis 2 while summing NumPy arrays so make your example while creating the code. Thanks a lot for going through this article. For any doubts please message me at my Gmail sany26.com@gmail.com or provide comment in the comment section as your comment is very valuable to me. You may also follow me at Sanpreet Github account. I am also active at Sanpreet linkedln, please do catch me there. I am attaching some references, hope this may also help you in getting better knowledge and understanding about NumPy arrays. There is also a crucial need to study data structures along with NumPy array. I too have taken a course from udemy well explained by the instructor because it is important to know the time and space complexity of various algorithms. Have a look at the certificate too.

Data Structures and Algorithms: In-Depth using Python

References

Numpy in python
Python Numpy Tutorial
The basics of Numpy arrays
Python Numpy array Tutorial
Numerical Operations on arrays
NumPy array Object
NumPy Internal Organization

Originally published at http://ersanpreet.wordpress.com on October 3, 2019.

--

--