0
0
SciPydata~5 mins

Correlation (correlate) in SciPy

Choose your learning style9 modes available
Introduction

Correlation helps us find how two sets of numbers relate to each other. It shows if they move together or not.

Comparing daily temperatures in two cities to see if they change similarly.
Checking if sales of two products increase or decrease together over time.
Analyzing if two sensors in a machine give related readings.
Finding patterns between stock prices of two companies.
Syntax
SciPy
scipy.signal.correlate(a, v, mode='full', method='auto')

a and v are the two data arrays to compare.

mode controls the size of the output: 'full' gives the complete correlation.

Examples
Basic correlation of two small lists with default 'full' mode.
SciPy
from scipy.signal import correlate

result = correlate([1, 2, 3], [0, 1, 0.5])
Correlation only where the arrays fully overlap.
SciPy
correlate([1, 2, 3], [0, 1, 0.5], mode='valid')
Correlation output is the same size as the first array.
SciPy
correlate([1, 2, 3], [0, 1, 0.5], mode='same')
Sample Program

This program calculates the correlation between two lists of numbers. It shows how similar they are at different shifts.

SciPy
from scipy.signal import correlate

# Two simple data sequences
data1 = [1, 2, 3, 4]
data2 = [0, 1, 0.5]

# Calculate full correlation
corr = correlate(data1, data2)

print('Correlation result:', corr)
OutputSuccess
Important Notes

The correlation result length is len(a) + len(v) - 1 when mode='full'.

Correlation can help find delays or shifts between signals.

Summary

Correlation measures similarity between two data sets at different shifts.

Use scipy.signal.correlate to compute it easily in Python.

Different modes control the size of the output array.