Python

A Brief Tutorial on NumPy in Python

Programmingempire

In this post on A Brief Tutorial on NumPy in Python, I will explain different features of the NumPy package in Python. Basically, this library serves the purpose of manipulating arrays. In fact, Python already has a list data structure for creating arrays. However, NumPy is much faster than lists. Also, displaying an array is straightforward with NumPy in comparison to lists. Although NumPy library is a vast library comprising of lots of functions, I will cover only a few most useful functions in this article. In fact, we use these functions frequently in data analysis

What is the Purpose of NumPy?

To begin with, let us first understand why this library is so useful. Basically, the NumPy library serves the purpose of Numerical and Scientific Computing in Python. In particular, we use arrays with NumPy. Hence, we use the functions of this library for displaying, searching, sorting, and filtering array elements. Let us discuss some examples of all of these features.

Creating and Displaying Arrays

The following example creates a one-dimension, a two-dimensional, and a three-dimensional array and prints its elements. It must be noted that no for loop is required to print the array.

Example of Creating Arrays Using NumPy

In the following example, we create some arrays of the different number of dimensions and just print them. It is important to note that, we use square bracket notation for creating arrays. Also, we print the version of the NumPy package using the __version__ attribute.

import numpy as np
print("Current Version of NumPy: " + str(np.__version__))

myarr=np.array([1,2,3,4,5])
print("Type of Array: " + str(type(myarr)))

m1=np.array([[1,2,3], [4,5,6], [7,8,9]])
m2=np.array([[[1,2], [3,4], [5,6]], [[7,8], [9,10], [11, 12]]])
#Printing 1D Array
print("1D Array...")
print(myarr)

#Printing 1D Array
print("n2D Array...")
print(m1)

#Printing 1D Array
print("n3D Array...")
print(m2)

Output

Creating Arrays with NumPy
Creating Arrays with NumPy

Slicing

Let us discuss the operation of slicing with arrays. Basically, slicing is an operation to retrieve the part of an array. Also, we can slice an array of any dimension. Note the syntax of slicing as given below.

array2 = array1[a:b:c]

Here, a is the start index, b is the end index (not included in the result), and represents the step. Of course, [::] represents the whole array.

import numpy as np
a=np.array([1,2,3,4,5,6,7,8,9,10])

# Slicing from element at index 0 to index 3 (index 4 not included)
a1=a[:4]
print(a1)

# Slicing from element at index 5 to last index (index 9)
a2=a[4:]
print(a2)

# Slicing from element at index 0 to index 9 (all elements)
a3=a[:]
print(a3)

# Slicing from element at index 4 to index 3 (No element included)
a4=a[4:4]
print(a4)

# Slicing from element at index 4 to index 4 (only element at index 4 included)
a4=a[4:5]
print(a4)

# Slicing from element at index 2 to index 5 with a step of 2
a5=a[2:6:2]
print(a5)

# Slicing from element at index 0 to index 9 with a step of 2
a6=a[::2]
print(a6)

# Slicing from element at index 7 to index 3 (index 2 not included)
a7=a[-7:-2]
print(a7)
NumPy Slicing Examples
NumPy Slicing Examples

Slicing a Two-Dimensional Array

Given below is an example of slicing a two-dimensional array. Here we create a 2-Dimensional array by filling it with zeros. Later, we use a for loop to assign values 1 to 100. As can be seen in the output, subsequent slicing operations retrieve the four quarters of the whole array.

import numpy as np

a=np.zeros([10,10])
x=1
for i in range(10):
    for j in range(10):
        a[i][j]=x
        x=x+1

print(a)


a1=a[:5, :5]
print("First Quarter: ")
print(a1)

a1=a[:5, 5:]
print("Second Quarter: ")
print(a1)

a1=a[5:, :5]
print("Third Quarter: ")
print(a1)

a1=a[5:, 5:]
print("Fourth Quarter: ")
print(a1)

Output

Slicing a 2D Array
Slicing a 2D Array

Display Shape of an Array

The shape attribute of a NumPy array gives the size of each dimension of the array. In other words, shape returns a tuple that contains total elements in each dimension. In order to understand the shape attribute consider the following example in which the shape of a 1-dimensional, a 2-dimensional, a 3-dimensional, and a 4-dimensional array is displayed.

Example of Shape

import numpy as np
a=np.array([1,2,3,4])

print(a.shape)

a1=np.array([[1,2],[3,4]])
print(a1.shape)

a2=np.array([[[1,2], [3,4],[5,6]]])
print(a2.shape)

a3=np.array([[[[1,2], [3, 4], [5,6]], [[2,8], [3,1],[7,5]]]])
print(a3.shape)

Output

Example of Shape Attribute
Example of Shape Attribute

Changing the Shape Using Reshape

The Reshape function makes changes in the shape of an array. This function is particularly helpful when we want to make a higher dimensional array as a one-dimensional array or vice-versa in order to simplify computation.

Example of Reshaping

In this example, first, we convert a 2-dimensional array to a 1-dimensional array and then a 1-D array is converted to a 2-D array and a 3-D array respectively.

import numpy as np

print("Original Array: ")
a1=np.array([[1,2,3], [4,5,6]])
print(a1)

print("Reshaping to one-dimensional Array: ")
a1=a1.reshape(-1)
print(a1)

print("Original Array: ")
a1=np.array([1,2,3,4,5,6,7,8,9])
print(a1)
print("Reshaping to two-dimensional Array: ")
a1=a1.reshape(3,3)
print(a1)

print("Original Array: ")
a1=np.array([1,2,3,4,5,6,7,8])
print(a1)
print("Reshaping to three-dimensional Array: ")
a1=a1.reshape(2,2,-1)
print(a1)

Output

Examples of Reshaping
Examples of Reshaping

Joining Arrays

We can join arrays using NumPy using two ways. Firstly, with the help of concatenate() function. Secondly, with the help of stack(), hstack(), vstack(), and dstack() functions. Let us consider few examples as given below.

Examples of Joining Arrays

The following example shows the usage of all of the above-mentioned functions for joining arrays.

import numpy as np

a1=np.array([1,2,3])
a2=np.array([4,5])
print("Original Arrays: ")
print(a1)
print(a2)
print("After Concatenation")
a3=np.concatenate((a1,a2))
print(a3)

print("Conactenating 2D Arrays: ")
a1=np.array([[1,2], [3,4]])
a2=np.array([[7,8],[9,10]])
print("Original Arrays: ")
print(a1)
print(a2)

print("After Concatenation with axis=1")
a3=np.concatenate((a1,a2), axis=1)
print(a3)

print("After Concatenation with axis=0")
a3=np.concatenate((a1,a2))
print(a3)

print("Joining using stack() with axis=0")
a3=np.stack((a1,a2))
print(a3)

print("Joining using stack() with axis=1")
a3=np.stack((a1,a2), axis=1)
print(a3)

print("Joining using hstack()")
a3=np.hstack((a1,a2))
print(a3)

print("Joining using vstack()")
a3=np.vstack((a1,a2))
print(a3)

print("Joining using dstack()")
a3=np.dstack((a1,a2))
print(a3)

Output

Examples of Joining Arrays
Examples of Joining Arrays

Splitting Arrays

If we want to create more than one array from a single array, we can use splitting function of NumPy. In fact, NumPy contains array_split() function that splits an array and creates multiple arrays. Consider following examples of splitting arrays.

Example of Splitting Arrays

In the following example, we split a 1-D array as well as a 2-D array. Further, the 1-D array has 12 elements. Hence, when we split this array in two, then each resulting array gets 6 elements. Similarly, when we split the given array in three, then each of the three arrays gets four elements. However, if we split a 12-element array in five, then the first two arrays get three elements each, and the rest of the three arrays get two elements. each. In fact, the array_split() function evenly distributes the elements in the resulting arrays.

Now consider the splitting of a 2-D array containing four rows and three columns and split this array in three in two ways. Firstly, when we take axis=0, then the first resulting array is a 2 X 3 array, whereas, the rest of the two arrays have size 1 X 3 each. Further, when we take axis=1, then resulting three arrays are uniformly created with dimension 4 X 1 each

import numpy as np
print("Splitting 1D Array:")
a1=np.array([76, 1, -90, 22, 56, 12, -80, 43, -89, 11, 66, 22])
print("Original Array: ")
print(a1)

print("Splittin in two arrays...")
a2=np.array_split(a1, 2)
print(a2)

print("Splittin in three arrays...")
a2=np.array_split(a1, 3)
print(a2)

print("Splittin in five arrays...")
a2=np.array_split(a1, 5)
print(a2)

print("Splitting 2D Array:")
m1=np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]])
print("Original 2D Array: ")
print(m1)

print("Splitting in 3 arrays with axis=0")
m2=np.array_split(m1, 3)
print(m2)

print("Splitting in 3 arrays with axis=1")
m2=np.array_split(m1, 3, axis=1)
print(m2)

Output

Splitting 1-D and 2-D Arrays Using NumPy array_split()
Splitting 1-D and 2-D Arrays Using NumPy array_split()

Searching and Sorting Arrays

NumPy package also contains functions that make searching and sorting simpler. Consider the following examples.

Searching Array

In this example, we use the where() function to find the elements of an array that satisfy a certain condition. In fact, we can use where() in two ways. Firstly, we can pass only the condition as a parameter and it returns the index values of elements that satisfy the given condition. Secondly, we pass two more arrays along with the condition, and the function returns the corresponding value from the first array whenever the condition is satisfied. Otherwise, a value from the second array is returned.

It must be noted that both arrays which we pass as parameters should have the same number of elements as the given array. Also, either you must pass two arrays as the parameter after the boolean condition or don’t pass any of these arrays.

Example of Searching Using where()

import numpy as np
a1=np.array([76, 1, -90, 22, 56, 12, -80, 43, -89, 11, 66, 22])
print("Original Array: ")
print(a1)
print("Enter a Number to Search: ")
n=int(input())
x=np.where(a1==n)
print(x)

print("nDisplay 1's where array element is positive or zero...")
y=np.where(a1>=0,[1,1,1,1,1,1,1,1,1,1,1,1],[0,0,0,0,0,0,0,0,0,0,0,0])
print(y)

print("nDisplay those index where array element is positive or zero...")
y=np.where(a1>=0)
print(y)

print("nDisplay 1's where array element is negative...")
y=np.where(a1<0,[1,1,1,1,1,1,1,1,1,1,1,1],[0,0,0,0,0,0,0,0,0,0,0,0])
print(y)

print("nDisplay those index where array element is negative...")
y=np.where(a1<0)
print(y)

Output

Searching an Array using where() Function
Searching an Array using where() Function

Sorting Array

The NumPy package contains a sort() function for sorting the elements of array as given in following example. Moreover, you can use array of any data-type in sorting using the sort() function.

Example of Sorting Array

import numpy as np
a1=np.array([76, 1, -90, 22, 56, 12, -80, 43, -89, 11, 66, 22])
print("Original Array: ")
print(a1)
a1=np.sort(a1)
print("Sorted Array: ")
print(a1)

Output

Sorting Array
Sorting Array

Filtering an Arrays

It is possible to get only certain elements from a NumPy array by filtering it on the basis of boolean values True and False. In other words, filtering operation retrieves certain elements from an existing array and creates a new array. Consider thefollowing Examples.

Example of Filtering Array

The filter operation works as follows. First, we create an array which we want to filter. Next, create another array of boolean values on the basis of a certain condition either by assigning values directly or by using a for a loop. Finally, create the output array by providing the filter-array as the index in the original array.

import numpy as np
a1=np.array([76, 1, -90, 22, 56, 12, -80, 43, -89, 11, 66, 22])
print("Original Array: ")
print(a1)
print("Filtering Positive Numbers: ")

f=[]
for i in a1:
    if i>0:
        f.append(True)
    else:
        f.append(False)

print("Array used for filtering: ")
print(f)
#Filtering...
a2=a1[f]
print("Filtered Array: ")
print(a2)
print("nFiltering Negative Numbers: ")
f=[]
for i in a1:
    if i<0:
        f.append(True)
    else:
        f.append(False)

print("Array used for filtering: ")
print(f)
#Filtering...
a2=a1[f]
print("Filtered Array: ")
print(a2)

Output

Example of Filtering Array
Example of Filtering Array

Summary

To sum up, the NumPy package is very useful in manipulating arrays. Also, it is much faster in comparison to tuples. In fact, it is an important tool in data analysis and allows us to arrange our dataset in many possible ways using functions like reshape(), where(), filtering, and slicing.

Further Reading

Deep Learning Tutorial

Text Summarization Techniques

How to Implement Inheritance in Python

Find Prime Numbers in Given Range in Python

Running Instructions in an Interactive Interpreter in Python

Deep Learning Practice Exercise

Python Practice Exercise

Deep Learning Methods for Object Detection

Understanding YOLO Algorithm

What is Image Segmentation?

ImageNet and its Applications

Image Contrast Enhancement using Histogram Equalization

Transfer Learning and its Applications

Examples of OpenCV Library in Python

Examples of Tuples in Python

Python List Practice Exercise

Understanding Blockchain Concepts

Edge Detection Using OpenCV

Predicting with Time Series

Example of Multi-layer Perceptron Classifier in Python

Measuring Performance of Classification using Confusion Matrix

Artificial Neural Network (ANN) Model using Scikit-Learn

Popular Machine Learning Algorithms for Prediction

Long Short Term Memory – An Artificial Recurrent Neural Network Architecture

Python Project Ideas for Undergraduate Students

Creating Basic Charts using Plotly

Visualizing Regression Models with lmplot() and residplot() in Seaborn

Data Visualization with Pandas

A Brief Introduction of Pandas Library in Python

A Brief Tutorial on NumPy in Python

programmingempire

You may also like...