Do you know that numbers in music, images, or data analysis are one of the most useful elements to get desired results? It becomes very important to analyse them properly. The computer has this amazing power to understand and manipulate the world of numbers around us. The numpy.convolve() method in Python is one such magical wand for numbers.

In this article, we will discuss the numpy.convolve() method in Python and its examples. Without any further ado, let's begin by first understanding what NumPy is.

NumPy in Python

NumPy, or Numerical Python, is a Python library that can be used to perform a wide range of mathematical functions on arrays, matrices, etc. NumPy provides support for arrays and contains different methods like numpy.array() and numpy.zeroes(), numpy.ones() etc., for creating arrays.

NumPy is also used to perform various mathematical operations. Let's say you have a bunch of numbers representing your marks in all the subjects, and you want to calculate the average marks by writing just two words in Python. This can be done using NumPy. Not only this, but NumPy contains a bunch of functions that can be used to perform cool operations on a big list of numbers. The different mathematical functions that NumPy includes are numpy.mean(), numpy.min(), numpy.max(), etc.

numpy.convolve() Method

The 'numpy.convolve' method is included in the NumPy library. It is used to perform the convolution operation between two 1-dimensional arrays. The convolution operation is widely used in signal and image processing.

It is a mathematical operation that is used to combine two functions to produce a single function. Imagine you have two lists of numbers, and you want to blend them together in a way that reveals hidden patterns or helps you analyze data more effectively. This can be done using numpy.convolve() method.

The numpy.convolve() takes two one-dimensional arrays as input.

The first array is referred to as the signal, and the second is the kernel.

The last parameter, mode, is used to specify the size of the output array. The value of mode can be 'full', 'valid', or 'same'.

Modes of numpy.convolve() Method

Consider the input arrays as:

Input array 1 ( signal ) : [ 1, 5 ]

Input array 2 ( kernel ) : [ 3, 4, 2 ]

Let's see all three modes and an example of each to understand them in deep.

Mode: 'Full'

The 'Full' mode is used to generate output by considering all the possible convolutional values. The convolution is calculated for each position of overlap between the input arrays, even for those that rely on zero padding. The padding is done to avoid undefined values during the calculation.

Size of the Resultant Array: (Size of input array 1 ) + ( Size of input array 2 ) - 1

Dry Run

First of all, the input array 1 will be reversed, So now:

Input array 1 ( signal ) : [ 5, 1 ]

Input array 2 ( kernel ) : [ 3, 4, 2 ]

Element 1: 1*3 = 3

Element 2: 5*3 + 1*4 = 19

Element 3: 5*4 + 1*2 = 22

Element 4: 5*2 = 10

Resultant Array : [ 3, 19, 22, 10 ]

Code

Python

Python

import numpy as np

a = np.array([1, 5]) print("First input array is: ", a)

v = np.array([3, 4, 2]) print("Second input array is: ", v)

print("\nResultant convolution between a and v using 'full' mode is:") print(np.convolve(a, v, mode='full'))

You can also try this code with Online Python Compiler

The 'Valid' mode is used to generate convolutional values by considering only the overlapping values of input arrays. It does not rely on zero padding. It generates a smaller resultant array as compared to the Full and Same modes.

Size of the Resultant Array: MAX (Size ofInput 1,Size of Input 2 ) - MIN (Size ofInput 1, Size of Input 2 ) - 1

Dry Run

First of all, the input array 1 will be reversed, So now:

Input array 1 ( signal ) : [ 5, 1 ]

Input array 2 ( kernel ) : [ 3, 4, 2 ]

Element 1: 5*3 + 1*4 = 19

Element 2: 5*4 + 1*2 = 22

Resultant Array : [ 19, 22 ]

Code

Python

Python

import numpy as np

a = np.array([1, 5]) print("First input array is: ", a)

v = np.array([3, 4, 2]) print("Second input array is: ", v)

print("\nResultant convolution between a and v using 'valid' mode is:") print(np.convolve(a, v, mode='valid'))

You can also try this code with Online Python Compiler

The 'Same' mode is used to generate convolutional values centred with respect to the original arrays. The aim of this mode is to generate the resultant array of the same size as the larger of the two input arrays. It is performed in such a way that input array 1 ( signal ) is aligned with the centre of the input array 2 ( kernel ).

Size of the Resultant Array: MAX (Size ofInput array 1,Size of Input array 2 )

Dry Run

First of all, the input array 1 will be reversed, So now:

Input array 1 ( signal ) : [ 5, 1 ]

Input array 2 ( kernel ) : [ 3, 4, 2 ]

Element 1: 1*3 = 3

Element 2: 5*3 + 1*4 = 19

Element 3: 5*4 + 1*2 = 22

Resultant Array : [ 3, 19, 22 ]

Code

Python

Python

import numpy as np

a = np.array([1, 5]) print("First input array is: ", a)

v = np.array([3, 4, 2]) print("Second input array is: ", v)

print("\nResultant convolution between a and v using 'same' mode is:") print(np.convolve(a, v, mode='same'))

You can also try this code with Online Python Compiler

What is the difference between convolution and correlation?

Both convolution and correlation perform almost similar operations, but the major difference is convolution involves first reversing the kernel, while correlation doesn't. Convolution and correlation are used separately in different applications.

Can I perform convolution on multi-dimensional arrays in NumPy?

The numpy.convolve() function is specifically designed for one-dimensional arrays. For multi-dimensional convolution, the functions like scipy.signal.convolve2d() can be used.

What are the practical applications of convolution?

Convolution is used in various fields like Image Processing, Computer Vision, Signal Processing, CNN ( Convolutional Neural Networks ), Time Series Analysis, Natural Language Processing, etc.

Conclusion

In this article, we have discussed the NumPy library and an important method, numpy.convolve() Method in Python. We have also seen examples of all three modes of convolution in NumPy.

You can read these articles to learn more about Numpy in Python.

You can also consider our paid courses such asDSA in Python to give your career an edge over others!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problems, interview experiences, and interview bundles.