How to interpret the values returned by numpy.correlate()?

Learn, how to interpret the values returned by numpy.correlate() in Python?
By Pranit Sharma Last updated : December 25, 2023

Problem statement

Suppose that we are given two 1D arrays and we want to see their inter-relationships.

Interpret the values returned by numpy.correlate()

To interpret the values returned by numpy.correlate(), there are different procedures we can use in numpy. If we use numpy.corrcoef(arrayA, arrayB), it will give some numerical results which we need to understand. The numpy.coorelate() simply returns the cross-correlation of two vectors which can be understood with the help of an example,

Python code to interpret the values returned by numpy.correlate()

# Import numpy
import numpy as np

# Creating a array
arr = np.random.normal(0,1,size=50)

# Display Original array
print("Original array:\n",arr,"\n")

# Inserting a signal
arr[::10]+=5

# cross correlation
res = np.correlate(arr,arr,mode='full')

# Display result
print("Result:\n",res)

This will return a comb/shah function with a maximum when both data sets are overlapping. As this is an autocorrelation there will be no "lag" between the two input signals. The maximum of the correlation is therefore vector.size-1.

If we only want the value of the correlation for overlapping data, we can use mode='valid'.

Output

Example: How to interpret the values returned by numpy.correlate()?

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.