NumPy correlate()

The numpy.correlate() method computes the cross-correlation of two 1-dimensional sequences.

Example


correlate() Syntax

The syntax of the numpy.correlate() method is:

numpy.correlate(a, v, mode = 'valid')

correlate() Arguments

The numpy.correlate() method takes the following arguments:

  • a, v - arrays whose correlation we need to compute (can be array_like)
  • mode (optional) - specifies the size of the output ('valid', 'full', or 'same')

correlate() Return Value

The numpy.correlate() method returns the discrete cross-correlation of a and v.


Example 1: Find the Correlation Between Two ndArrays

While computing the correlation between two ndarrays, the correlation can have three modes.

  • 'valid' (default): The output contains only valid cross-correlation values. It returns an array with length max(M, N) - min(M, N) + 1, where M and N are the lengths of the input arrays a and v, respectively.
  • 'full': The output is the full discrete linear cross-correlation of the inputs. It returns an array with length M + N - 1, where M and N are the lengths of the input arrays a and v, respectively.
  • 'same': The output has the same size as a, centered with respect to the 'full' output.

Let's look at an example.

Output

Correlation in Valid mode : [11]
Correlation in Full mode : [ 0  3  7 11  5  4  6]
Correlation in Same mode : [ 3  7 11  5]

Example 2: Correlation Between Complex ndArrays

The numpy.correlate() function can also be used to find the correlation between complex data types.

Output

[12.-2.j]