0
0
SciPydata~20 mins

Correlation (correlate) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of 1D correlation using scipy.correlate
What is the output of the following code snippet using scipy.signal.correlate on two 1D arrays?
SciPy
import numpy as np
from scipy.signal import correlate

x = np.array([1, 2, 3])
y = np.array([0, 1, 0.5])
result = correlate(x, y, mode='full')
print(result)
A[0. 1. 2.5 4. 1.5]
B[0. 1. 2.5 3. 1.5]
C[0. 1. 2. 3. 1.5]
D[0. 1. 2.5 4. 3. ]
Attempts:
2 left
💡 Hint
Remember that correlation flips the second array before sliding it over the first.
data_output
intermediate
1:30remaining
Length of correlation output with different modes
Given two arrays a and b of lengths 4 and 3 respectively, what is the length of the output from scipy.signal.correlate(a, b, mode='valid')?
SciPy
import numpy as np
from scipy.signal import correlate

a = np.array([1, 2, 3, 4])
b = np.array([0, 1, 0.5])
result = correlate(a, b, mode='valid')
print(len(result))
A3
B2
C4
D6
Attempts:
2 left
💡 Hint
For 'valid' mode, output length is max(len(a), len(b)) - min(len(a), len(b)) + 1.
🔧 Debug
advanced
1:30remaining
Identify the error in correlation code
What error will this code raise when run, and why?
SciPy
from scipy.signal import correlate

x = [1, 2, 3]
y = [0, 1, 'a']
result = correlate(x, y)
print(result)
ASyntaxError: invalid syntax
BValueError: operands could not be broadcast together
CNo error, outputs a numeric array
DTypeError: unsupported operand type(s) for *: 'int' and 'str'
Attempts:
2 left
💡 Hint
Check the data types in the input arrays.
🚀 Application
advanced
2:00remaining
Using correlation to find a pattern in a signal
You have a signal array signal = [1, 3, 2, 1, 0, 1, 3, 2] and a pattern pattern = [1, 3, 2]. Using scipy.signal.correlate with mode='valid', which index in the correlation output corresponds to the best match of the pattern in the signal?
SciPy
import numpy as np
from scipy.signal import correlate

signal = np.array([1, 3, 2, 1, 0, 1, 3, 2])
pattern = np.array([1, 3, 2])
correlation = correlate(signal, pattern, mode='valid')
best_match_index = np.argmax(correlation)
print(best_match_index)
A5
B0
C1
D6
Attempts:
2 left
💡 Hint
Look for where the pattern aligns best with the signal by highest correlation value.
🧠 Conceptual
expert
1:30remaining
Effect of correlation mode on output length
Given two arrays of lengths 7 and 5, which mode of scipy.signal.correlate will produce an output array of length 5?
A'full'
B'valid'
CNone of the above
D'same'
Attempts:
2 left
💡 Hint
The 'same' mode returns output length equal to the first input array.