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

**Table of Contents**show

## Scipy Convolve 1d

The Scipy has a method

within module *convolve1d()*

that computes the one-dimensional convolution on a specified axis with the provided weights.*scipy.ndimage*

The syntax is given below.

`scipy.ndimage.convolve1d(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)`

Where parameters are:

It is array data as input.*input(array):*It is the one-dimensional sequence of numbers.*weights(ndarray):*This parameter represents the axis of the given array on which to calculate the convolution.*axis(int):*Used to specify the data type of returned array as output.*output(dtype, array):*It is used to control the boundaries of the input array when we want to extend its boundary. The modes are*mode():*

,*wrap*

,*constant*

,*nearest*

and*mirror*

.*reflect*It is used to fill the past edges of the input array when the mode is set to*cval(scalar):*

.**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

withing module *convolve()*

that computes the multi-dimensional convolution on a specified axis with the provided weights.*scipy.ndimage*

The syntax is given below.

`scipy.ndimage.convolve(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)`

Where parameters are:

It is array data as input.*input(array):*The array of weights must have a dimension equal to the input array.*weights(ndarray):*Used to specify the data type of returned array as output.*output(dtype, array):*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.*mode():*- 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

withing module *convolve()*

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.*scipy.ndimage*

The syntax is given below.

`scipy.ndimage.convolve(input, weights, axis=- 1, output=None, mode='reflect', cval=0.1, origin=1)`

Where parameters are:

It is array data as input.*input(array):*The array of weights must have a dimension equal to the input array.*weights(ndarray):*Used to specify the data type of returned array as output.*output(dtype, array):*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.*mode():*It is used to fill the past edges of the input array when the mode is set to constant.*cval(scalar):*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.*origin(int):*

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

in module *fftconvolve()*

that convolved n-dimensional array using the method *scipy.signal***FFT **(Fast Fourier Transform).

The syntax is given below.

`scipy.signal.fftconvolve(in1, in2, mode='full', method='auto')`

Where parameters are:

It is used to input the first signal in the form of an array.*in1(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.*in2(array_data):*It is used to specify the string that determines output size. The mode can be*mode:*

,*same*

and*full*

.*valid*It is used to specify the method that computes the convolution. The method can be*method:*

,*auto*

and*direct*

.*fft*

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

using Scipy in Python.*fftconvolve()*

Read: Scipy Stats

## Scipy Convolve gaussian

The Scipy has a method

within a module *gaussian_filter*

that apply gaussian to the multi-dimensional array.*scipy.ndimage*

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:

It is array data as input.*input(array_data):*Gaussian kernel standard deviation.*sigma(sequence of scalar):*It is used to specify the order of the filter.*order(int):*Used to specify the data type of returned array as output.*output(dtype, array):*It is used to control the boundaries of the given array as input when we want to extend its boundary. The modes are*mode():*

,*wrap*

,*constant*

,*nearest*

and*mirror*

.*reflect*It is used to fill the past edges of the given array as input when the mode is set to*cval(scalar):*

.**constant**

The method

returns*gaussian_filter*** ** of type ndarray.

`gaussian_filter`

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

of Scipy to convolve the given array on a specified axis.*convolve1d()*

To know about the method

please refer to the above sub-section.*convolve()*

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

without specifying axis parameters.*convolve*

`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

that we have learned in the above sub-section.*convolve()*

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

and see the result.*convolve*

`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

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.