# NumPy correlate()

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

### Example

``````import numpy as np

# create two arrays
array1 = np.array([0, 1, 2, 3])
array2 = np.array([2, 0, 1, 3])

# calculate the correlation of two arrays
corr = np.correlate(array1, array2)

print(corr)

# Output: [11]``````

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

``````import numpy as np

# create two arrays
array1 = np.array([0, 1, 2, 3])
array2 = np.array([2, 0, 1, 3])

# calculate the valid correlation of two arrays
valid_corr = np.correlate(array1, array2, mode = 'valid')

# calculate the full correlation of two arrays
full_corr = np.correlate(array1, array2, mode = 'full')

# calculate the correlation of two arrays
same_corr = np.correlate(array1, array2, mode = 'same')

print('Correlation in Valid mode :', valid_corr)
print('Correlation in Full mode :', full_corr)
print('Correlation in Same mode :', same_corr)``````

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.

``````import numpy as np

# create two arrays of complex numbers
array1 = np.array([1+1j, 2, 3-1j])
array2 = np.array([2, 1-1j, 3+1j])

# calculate the correlation of the complex arrays
corr = np.correlate(array1, array2)

print(corr)``````

Output

`[12.-2.j]`