Python

Python NumPy array tutorial

NumPy is a Python Library/ module which is used for scientific calculations in Python programming. In this tutorial, you will learn how to perform many operations on NumPy arrays such as adding, removing, sorting, and manipulating elements in many ways.

NumPy provides a multidimensional array object and other derived arrays such as masked arrays or masked multidimensional arrays.


 

Why using NumPy

The NumPy module provides a ndarray object using which we can use to perform operations on an array of any dimension. The ndarray stands for N-dimensional array where N is any number. That means NumPy array can be any dimension.

NumPy has a number of advantages over the Python lists. We can perform high performance operations on the NumPy arrays such as:

  1. Sorting array members
  2. Mathematical and Logical operations
  3. Input/ output functions
  4. Statistical and Linear algebra operations

 

How to install NumPy?

To install NumPy, you need Python and Pip on your system.

Run the following command on your Windows OS:

pip install numpy

Install NumPy

Now you can import NumPy in your script like this:

import numpy

NumPy array tutorial

 

Add array element

You can add a NumPy array element by using the append() method of the NumPy module.

The syntax of append is as follows:

numpy.append(array, value, axis)

The values will be appended at the end of the array and a new ndarray will be returned with new and old values as shown above.

The axis is an optional integer along which define how the array is going to be displayed. If the axis is not specified, the array structure will be flattened as you will see later.

Consider the following example where an array is declared first and then we used the append method to add more values to the array:

The output will be like the following:

Add array element

 

Add a column

We can use the append() method of NumPy to insert a column.

Consider the example below where we created a 2-dimensional array and inserted two columns:

The output will be like the following:

Add NumPy column

If the axis attribute is not used, the output will be like the following:

Axis without attribute

This is how the structure of the array is flattened.

In NumPy, we can also use the insert() method to insert an element or column. The difference between the insert() and the append() method is that we can specify at which index we want to add an element when using the insert() method but the append() method adds a value to the end of the array.

Consider the example below:

The output will be as follows:

Using insert

Here the insert() method adds the element at index 1. Remember the array index starts from 0.



Append a row

In this section, we will be using the append() method to add a row to the array. It’s as simple as appending an element to the array. Consider the following example:

The output will be as follows:

Append a row

Delete an element

You can delete a NumPy array element using the delete() method of the NumPy module:

This is demonstrated in the example below:

The output is as follows:

Delete an element

In the above example, we have a single dimensional array. The delete() method deletes the element at index 1 from the array.

Delete a row

Similarly, you can delete a row using the delete() method.

Consider the following example, where we have deleted a row from a 2-dimensional array:

The output will be as follows:

Delete a row

In the delete() method, you give the array first and then the index for the element you want to delete. In the above example, we deleted the second element which has the index of 1.


Check if NumPy array is empty

We can use the size method which returns the total number of elements in the array.

In the following example, we have an if statement that checks if there are elements in the array by using ndarray.size where ndarray is any given NumPy array:

The output is as follows:

Check if NumPy array is empty

In the above code, there are three elements, so it’s not empty and the condition will return false.

If there are no elements, the if condition will become true and it will print the empty message.

If our array is equal to:

a = numpy.array([])

The output of the above code will be as below:

Empty NumPy array

 

Find the index of a value

To find the index of value, we can use the where() method of the NumPy module as demonstrated in the example below:

The output will be as follows:

Find the index of a value

The where() method will also return the datatype. If you want to just get the index, use the following code:

Then the output will be:

Using the where method

 

NumPy array slicing

Array slicing is the process of extracting a subset from a given array. You can slice an array using the colon (:) operator and specify the starting and ending of the array index, for example:

array[from:to]

This is highlighted in the example below:

Here we extracted the elements starting from index 2 to index 5. The output will be:

NumPy array slicing

If we want to extract the last three elements. We can do this by using negative slicing as follows:

The output will be:

Negative slicing

 

Apply a function to all array element

In the following example, we are going to create a lambda function on which we will pass our array to apply it to all elements:

The output is as follows:

Apply a function to NumPy array

In this example, a lambda function is created which increments each element by two.


NumPy array length

To get the length of a NumPy array, you can use the size attribute of the NumPy module as demonstrated in the following example:

This code will generate the following result:

NumPy array length

 

Create NumPy array from List

Lists in Python are a number of elements enclosed between square brackets.

Suppose you have a list as:

l = [1, 2, 3, 4, 5]

Now to create an array from this list, we will use the array() method of the NumPy module:

The output will be as follows:

Create NumPy array from List

Similarly, using the array() method, we can create a NumPy array from a tuple. A tuple contains a number of elements enclosed in round brackets as follows:

The output will be:

Create NumPy array from tuple

 

Convert NumPy array to list

To convert an array to a list, we can use the tolist() method of the NumPy module.

Consider the code below:

The output will be as follows:

Convert NumPy array to list

In this code, we simply called the tolist() method which converts the array to a list. Then we print the newly created list to the output screen.


NumPy array to CSV

To export the array to a CSV file, we can use the savetxt() method of the NumPy module as illustrated in the example below:

This code will generate a CSV file in the location where our Python code file is stored. You can also specify the path. When you run the script, the file will be generated as this:

CSV file

The content of this file will be like the following:

CSV file content

You can remove the extra zero padding like this:

numpy.savetxt("myArray.csv", a,fmt='%.2f')

 

Sort NumPy array

You can sort NumPy array using the sort() method of the NumPy module:

The sort() function takes an optional axis (an integer) which is -1 by default. The axis specifies which axis we want to sort the array. -1 means the array will be sorted according to the last axis.

Consider the example below:

In this example, we called the sort() method in the print statement. The array “a” is passed to the sort function. The output of this will be as follows:

Sort NumPy array

 

Normalize array

Normalizing an array is the process of bringing the array values to some defined range. For example, we can say we want to normalize an array between -1 and 1 and so on.

The formula for normalization is as follows:

x = (x xmin) / (xmax xmin)

Now we will just apply this formula to our array to normalize it. To find the maximum and minimum items in the array, we will use the max() and min() methods of NumPy respectively.

The output will be as follows:

Normalize NumPy array

 

Array Indexing

Indexing means refer to an element of the array. In the following examples, we used indexing in single dimensional and 2-dimensional arrays as well:

The output will be as below:

Numpy Array Indexing

Now indexing with a 2-dimensional array:

The output will be:

NumPy multidimensional Array Indexing

The index [1][2] means the second row and the third column (as indexing starts from 0). Therefore, we have 9 on the output screen.

 

Append NumPy array to another

You can append a NumPy array to another NumPy array by using the append() method.

Consider the following example:

The output will be as follows:

Append NumPy array to another

In this example, a NumPy array “a” is created and then another array called “b” is created. Then we used the append() method and passed the two arrays. As the array “b” is passed as the second argument, it is added at the end of the array “a”.

 

As we saw, working with NumPy arrays is very simple. NumPy arrays are very essential when working with most machine learning libraries. So, we can say that NumPy is the gate to artificial intelligence.

Ayesha Tariq
Ayesha Tariq is a full stack software engineer, web developer, and blockchain developer enthusiast. She has extensive knowledge of C/C++, Java, Kotlin, Python, and various others.

Leave a Reply

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