What is the numpy.convolve() method in Python?
Overview
In Python, we use the numpy.convolve() method to calculate the combination of two one-dimensional vectors discretely and linearly.
Syntax
numpy.convolve(v1, v2, mode)
Parameters
v1: This is the 1st input array.v2: This is the 2nd input array.mode(optional): There are three different modes:full: This is the default mode, which returns the convolution at each overlap point with an output shape of (v1+v2-1,). The signals don’t wholly overlap at the convolution’s endpoints, and boundary effects can be noticed.same: This returns the output with the maximum length (v2, v1). The effects of boundaries can still be seen.valid: This produces a length of max(v2, v1) - min(v2, v1) + 1. Only points where the signals overlap are provided with the convolution product. Outside of the signal boundary, values have no effect.
Return value
The numpy.convolve() method returns discrete, linear convolution of two one-dimensional vectors
Example
The following code shows how to use Python’s numpy.convolve() method.
# import numpyimport numpy as np# create arrays using np.arrayv1 = np.array(range(1,10,2))v2 = np.array(range(5,15,3))# compute discrete, linear convolution# and store the result in result# By default mode='full'result = np.convolve(v1, v2)print(result)# using mode='same' argumentresult = np.convolve(v1, v2, mode='same')print(f"using mode='same' argument: {result}")# using mode='valid' argumentresult = np.convolve(v1, v2, mode='valid')print(f"using mode='valid' argument: {result}")
Explanation
- Line 2: We import the
numpylibrary. - Lines 4–5: We create two 1D arrays,
v1andv2usingrange()method. - Lines 10, 13, and 17: The
np.convolve()method is used to calculate the discrete, linear convolution of two one-dimensional vectors (v1 & v2) The result is stored in a new variable calledresult - Lines 11, 14, and 18: The result is displayed.
Note: By default, the mode argument is set to
full.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved