Challenge - 5 Problems
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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)
Attempts:
2 left
💡 Hint
Remember that correlation flips the second array before sliding it over the first.
✗ Incorrect
The correlation flips y and slides it over x, computing sums of products at each shift. The output length is len(x)+len(y)-1 = 5. The values correspond to the sums at each shift.
❓ data_output
intermediateLength 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))
Attempts:
2 left
💡 Hint
For 'valid' mode, output length is max(len(a), len(b)) - min(len(a), len(b)) + 1.
✗ Incorrect
The 'valid' mode returns only points where the arrays fully overlap. So length is 4 - 3 + 1 = 2.
🔧 Debug
advancedIdentify 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)
Attempts:
2 left
💡 Hint
Check the data types in the input arrays.
✗ Incorrect
The list y contains a string 'a', which cannot be multiplied by integers during correlation, causing a TypeError.
🚀 Application
advancedUsing 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)
Attempts:
2 left
💡 Hint
Look for where the pattern aligns best with the signal by highest correlation value.
✗ Incorrect
The highest correlation value occurs at index 5, where the pattern matches the last three elements of the signal exactly.
🧠 Conceptual
expertEffect 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?Attempts:
2 left
💡 Hint
The 'same' mode returns output length equal to the first input array.
✗ Incorrect
The 'same' mode returns an output with the same length as the first input array (7). 'valid' returns length 3, 'full' returns length 11. None produce length 5 except 'same' if the first array length is 5.
