In this Python tutorial, we will learn about the “Scipy Convolve” and additionally we will cover the following topics using some examples.
- Scipy Convolve 3d
- Scipy Convolve 2d
- Scipy Convolve 1d
- Scipy Convolve fft
- Scipy Convolve gaussian
- Scipy Convolve stride
- Scipy Convolve along axis
- Scipy Convolve kernel
Scipy Convolve 1d
The Scipy has a method convolve1d()
within module scipy.ndimage
that computes the one-dimensional convolution on a specified axis with the provided weights.
The syntax is given below.
scipy.ndimage.convolve1d(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)
Where parameters are:
- input(array): It is array data as input.
- weights(ndarray): It is the one-dimensional sequence of numbers.
- axis(int): This parameter represents the axis of the given array on which to calculate the convolution.
- output(dtype, array): Used to specify the data type of returned array as output.
- mode(): It is used to control the boundaries of the input array when we want to extend its boundary. The modes are
wrap
,constant
,nearest
,mirror
andreflect
. - cval(scalar): It is used to fill the past edges of the input array when the mode is set to
constant
. - origin(int): It is used to put the filter on the given input array, if the value is zero then put the filter in the center. If the value is positive, put the filter on the left side, and if the value is negative, put the filter on the right side.
Let’s understand with an example by following the below steps:
Import the required libraries using the below python code.
from scipy import ndimage
Create an array with several values and weights using the below Python code.
a = [4,6,3,5,6,0,9]
w = [2,6]
Now pass the above-created array and weights to the method convolve1d
.
ndimage.convolve1d(a,weights=w)
Read: Scipy Optimize – Helpful Guide
Scipy Convolve 2d
The Scipy has a method convolve()
withing module scipy.ndimage
that computes the multi-dimensional convolution on a specified axis with the provided weights.
The syntax is given below.
scipy.ndimage.convolve(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)
Where parameters are:
- input(array): It is array data as input.
- weights(ndarray): The array of weights must have a dimension equal to the input array.
- output(dtype, array): Used to specify the data type of returned array as output.
- mode(): It is used to control the boundaries of the input array when we want to extend its boundary. The modes are wrap, constant, nearest, mirror, and reflect.
- cval(scalar): It is used to fill the past edges of the input array when the mode is set to constant.
- origin(int): It is used to put the filter on the given input array, if the value is zero then put the filter in the center. If the value is positive, put the filter on the left side, and if the value is negative, put the filter on the right side.
Let’s understand with an example by following the below steps:
Import the required libraries using the below python code.
from scipy import ndimage
import numpy as np
Create a two-dimensional array with several values and weights using the below Python code.
array_data = np.array([(5,4,6,8),(1,0,7,4)])
weight = np.array([[0,0,1],[1,1,1]])
Now pass the above-created two-dimensional array and weights to the method convolve
.
ndimage.convolve(array_data,weights=weight,cval=0.1,mode='constant')
Read: Scipy Rotate Image
Scipy Convolve 3d
The Scipy has a method convolve()
withing module scipy.ndimage
that computes the multi-dimensional convolution. Here we will use the same method with a 3d array that we have used in the above sub-section.
The syntax is given below.
scipy.ndimage.convolve(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)
Where parameters are:
- input(array): It is array data as input.
- weights(ndarray): The array of weights must have a dimension equal to the input array.
- output(dtype, array): Used to specify the data type of returned array as output.
- mode(): It is used to control the boundaries of the input array when we want to extend its boundary. The modes are wrap, constant, nearest, mirror, and reflect.
- cval(scalar): It is used to fill the past edges of the input array when the mode is set to constant.
- origin(int): It is used to put the filter on the given input array, if the value is zero then put the filter in the center. If the value is positive, put the filter on the left side, and if the value is negative, put the filter on the right side.
Let’s understand with an example by following the below steps:
Import the required libraries using the below python code.
from scipy import ndimage
import numpy as np
Create a three-dimensional array with several values and weights using the below code.
array_data = np.array([[[3,18], [46, 80]], [[0, 0], [62, 86]],[[86,34],[22,19]]])
weight = np.array([[[0,0,1],[1,1,1],[0,1,0]]])
Now pass the above-created three-dimensional array and weights to the method convolve
.
ndimage.convolve(array_data,weights=weight,cval=0.1,mode='constant')
Read: Scipy Constants
Scipy Convolve fft
The Scipy has a method fftconvolve()
in module scipy.signal
that convolved n-dimensional array using the method FFT (Fast Fourier Transform).
The syntax is given below.
scipy.signal.fftconvolve(in1, in2, mode='full', method='auto')
Where parameters are:
- in1(array_data): It is used to input the first signal in the form of an array.
- in2(array_data): It is used to input the second signal in the form of an array, the dimension must be the same as the first input array.
- mode: It is used to specify the string that determines output size. The mode can be
same
,full
andvalid
. - method: It is used to specify the method that computes the convolution. The method can be
auto
,direct
andfft
.
Let’s understand with an example by following the below steps:
Import the required library using the below python code.
from scipy.signal import fftconvolve
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Generate a random noise signal and apply the method fftconvolveI()
using the below code.
random_gen = np.random.default_rng()
s = random_gen.standard_normal(800)
autocorrelation = fftconvolve(s, s[::-1], mode='full')
Let’s plot the above-convolved signal using the below code.
fig, (orig_axes, mag_axes) = plt.subplots(2, 1)
orig_axes.plot(s)
orig_axes.set_title('It is noise')
mag_axes.plot(np.arange(-len(s)+1,len(s)), autocorrelation)
mag_axes.set_title('It is autocorrelation')
fig.tight_layout()
fig.show()
This is how to use the method fftconvolve()
using Scipy in Python.
Read: Scipy Stats
Scipy Convolve gaussian
The Scipy has a method gaussian_filter
within a module scipy.ndimage
that apply gaussian to the multi-dimensional array.
The syntax is given below.
scipy.ndimage.gaussian_filter(input, sigma, order=0, output=int, mode='nerest', cval=1.1, truncate=3.1)
Where parameters are:
- input(array_data): It is array data as input.
- sigma(sequence of scalar): Gaussian kernel standard deviation.
- order(int): It is used to specify the order of the filter.
- output(dtype, array): Used to specify the data type of returned array as output.
- mode(): It is used to control the boundaries of the given array as input when we want to extend its boundary. The modes are
wrap
,constant
,nearest
,mirror
andreflect
. - cval(scalar): It is used to fill the past edges of the given array as input when the mode is set to
constant
.
The method gaussian_filter
returns gaussian_filter
of type ndarray.
Let’s understand with an example by following the below steps:
Import the required libraries using the python below code.
from scipy.ndimage import gaussian_filter
import numpy as np
Create an array containing values using the below code.
array_data = np.arange(100, step=4).reshape((5,5))
array_data
Now, apply the Gaussian filter on the array data using the below code.
gaussian_filter(array_data, sigma=1)
This is how to apply a gaussian filter to an array of data using Scipy in the Python program.
Read: Scipy Misc + Examples
Scipy Convolve along axis
Here we will use the same method convolve1d()
of Scipy to convolve the given array on a specified axis.
To know about the method convolve()
please refer to the above sub-section.
Let’s take an example by following the below steps:
Import the required libraries using the below python code.
from scipy import ndimage
import numpy as np
Create a two-dimensional array with several values and weights using the below python code.
array_data = np.array([(4,5,8,6),(0,1,4,7)])
weight = [0,1,1]
Pass the above-created two-dimensional array and weights to the method convolve
without specifying axis parameters.
ndimage.convolve1d(array_data,weights=weight)
Again run the above code with an axis equal to 0 using the below code.
ndimage.convolve1d(array_data,weights=weight,axis=0)
Look at the result of both codes, one without an axis and the second with an axis.
Read: Scipy Signal – Helpful Tutorial
Scipy Convolve kernel
The weights are a group of values that is called kernel in convolution which slides over the given array as input to transform that data. Here we will use the same Scipy method convolve()
that we have learned in the above sub-section.
Let’s understand through an example by following the below steps:
Import the required libraries using the below python code.
from scipy import ndimage
import numpy as np
Create a two-dimensional array with several values and weights or kernels using the below code.
array_data = np.array([(1,4,8,6),(4,7,0,4)])
kernel = np.array([[1,0,1],[1,1,0]])
Pass the above-created two-dimensional array and kernel to the method convolve
and see the result.
ndimage.convolve(array_data,weights=kernel)
Create a new kernel using the below code.
kernel_2 = np.array([[0,0,1],[0,1,1]])
Again, pass the new kernel to a method and see the result using the code below.
ndimage.convolve(array_data,weights=kernel_2)
This is how to use the different kernels to achieve different transformations on the same data.
Also, check the following Python SciPy tutorials.
- Python Scipy Freqz
- Scipy Integrate + Examples
- Python Scipy Distance Matrix
- Scipy Linalg – Helpful Guide
- Scipy Ndimage Rotate
- Python Scipy Matrix + Examples
- Scipy Stats Zscore + Examples
So, in this tutorial, we have learned about the “Scipy Convolve” and covered the following topics.
- Scipy Convolve 3d
- Scipy Convolve 2d
- Scipy Convolve 1d
- Scipy Convolve fft
- Scipy Convolve gaussian
- Scipy Convolve stride
- Scipy Convolve along axis
- Scipy Convolve kernel
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.