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:
- Sorting array members
- Mathematical and Logical operations
- Input/ output functions
- 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
Now you can import NumPy in your script like this:
import numpy
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:
a = numpy.array([1, 2, 3])
newArray = numpy.append (a, [10, 11, 12])
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:
a = numpy.array([[1, 2, 3], [4, 5, 6]])
b = numpy.array([[400], [800]])
newArray = numpy.append(a, b, axis = 1)
If the axis attribute is not used, the output will be like the following:
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:
a = numpy.array([1, 2, 3])
newArray = numpy.insert(a, 1, 90)
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:
a = numpy.array([[1, 2, 3], [4, 5, 6]])
newArray = numpy.append(a, [[50, 60, 70]], axis = 0)
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:
a = numpy.array([1, 2, 3])
newArray = numpy.delete(a, 1, axis = 0)
The output is as follows:
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:
a = numpy.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
newArray = numpy.delete(a, 1, axis = 0)
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:
import numpy
a = numpy.array([1, 2, 3])
if(a.size == 0):
print("The given Array is empty")
else:
print("The array = ", a)
The output is as follows:
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:
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:
import numpy
a = numpy.array([1, 2, 3, 4, 5])
print("5 is found at index: ", numpy.where(a == 5))
The where() method will also return the datatype. If you want to just get the index, use the following code:
import numpy
a = numpy.array([1, 2, 3, 4, 5])
index = numpy.where(a == 5)
print("5 is found at index: ", index[0])
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:
import numpy
a = numpy.array([1, 2, 3, 4, 5, 6, 7, 8])
print("A subset of array a = ", a[2:5])
Here we extracted the elements starting from index 2 to index 5. The output will be:
If we want to extract the last three elements. We can do this by using negative slicing as follows:
import numpy
a = numpy.array([1, 2, 3, 4, 5, 6, 7, 8])
print("A subset of array a = ", a[-3:])
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:
import numpy
addition = lambda x: x + 2
a = numpy.array([1, 2, 3, 4, 5, 6])
print("Array after addition function: ", addition(a))
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:
import numpy
a = numpy.array([1, 2, 3, 4, 5, 6])
print("The size of array = ", a.size)
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:
import numpy
l = [1, 2, 3, 4, 5]
a = numpy.array(l)
print("The NumPy array from Python list = ", a)
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:
import numpy
t = (1, 2, 3, 4, 5)
a = numpy.array(t)
print("The NumPy array from Python Tuple = ", a)
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:
import numpy
a = numpy.array([1, 2, 3, 4, 5])
print("Array to list = ", a.tolist())
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:
import numpy
a = numpy.array([1, 2, 3, 4, 5])
numpy.savetxt("myArray.csv", a)
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:
The content of this file will be like the following:
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:
import numpy
a = numpy.array([16, 3, 2, 6, 8, 10, 1])
print("Sorted array = ", numpy.sort(a))
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:
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.
import numpy
x= numpy.array([400, 800, 200, 700, 1000, 2000, 300])
xmax = x.max()
xmin = x.min()
x = (x - xmin)/(xmax - xmin)
print("After normalization array x = \n", x)
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:
import numpy
a = numpy.array([20, 13, 42, 86, 81, 9, 11])
print("Element at index 3 = ", a[3])
Now indexing with a 2-dimensional array:
import numpy
a = numpy.array([[20, 13, 42], [86, 81, 9]])
print("Element at index a[1][2] = ", a[1][2])
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:
import numpy
a = numpy.array([1, 2, 3, 4, 5])
b = numpy.array([10, 20, 30, 40, 50])
newArray = numpy.append(a, b)
print("The new array = ", newArray)
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.
I also recently saw a challenge on HackerRank which involved
NumPy
and reading input in the form of a string. You had to read in a full line, split it into anarray
and then convert each of the split strings into anint
. Pretty interesting, and relevant if you’re ever in the situation where you have to read in external input in this form.