Challenge - 5 Problems
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.correlate with mode='valid'
What is the output of the following code snippet?
NumPy
import numpy as np x = np.array([1, 2, 3]) y = np.array([0, 1, 0.5]) result = np.correlate(x, y, mode='valid') print(result)
Attempts:
2 left
💡 Hint
Remember that mode='valid' returns only positions where the signals fully overlap.
✗ Incorrect
np.correlate with mode='valid' computes the sum of products where the two arrays fully overlap. Here, it computes 1*0.5 + 2*1 + 3*0 = 0.5 + 2 + 0 = 2.5, because np.correlate reverses the second array before computing the sum of products.
❓ data_output
intermediate1:30remaining
Length of output array from np.correlate with mode='full'
Given two arrays of lengths 4 and 3, what is the length of the output array when using np.correlate with mode='full'?
NumPy
import numpy as np x = np.array([1, 2, 3, 4]) y = np.array([0, 1, 0.5]) result = np.correlate(x, y, mode='full') print(len(result))
Attempts:
2 left
💡 Hint
The length of the full correlation output is length(x) + length(y) - 1.
✗ Incorrect
For mode='full', the output length is the sum of input lengths minus 1. Here, 4 + 3 - 1 = 6. The correct length is 6, so option B is correct. Option B says 7, which is incorrect.
🔧 Debug
advanced2:00remaining
Identify the error in np.correlate usage
What error does the following code raise?
NumPy
import numpy as np x = np.array([1, 2, 3]) y = np.array([[0, 1, 0.5]]) result = np.correlate(x, y) print(result)
Attempts:
2 left
💡 Hint
Check the shape of the input arrays; np.correlate expects 1D arrays.
✗ Incorrect
np.correlate only works with 1D arrays. Here, y is a 2D array with shape (1,3), which causes a TypeError indicating np.correlate only supports 1D arrays.
🧠 Conceptual
advanced1:30remaining
Effect of reversing the second array in np.correlate
Which statement best describes how np.correlate computes correlation between two arrays x and y?
Attempts:
2 left
💡 Hint
Correlation involves flipping one of the signals before sliding.
✗ Incorrect
np.correlate computes the sum of products between x and the reversed y array, sliding y over x. This is the definition of cross-correlation.
🚀 Application
expert2:30remaining
Find the lag with maximum correlation
Given two 1D numpy arrays x and y, which code snippet correctly finds the lag (shift) where their correlation is maximum using np.correlate with mode='full'?
NumPy
import numpy as np x = np.array([1, 3, 2, 4]) y = np.array([0, 1, 0.5, 1])
Attempts:
2 left
💡 Hint
The lag index is offset by the length of the second array minus one in full mode.
✗ Incorrect
Using mode='full', the correlation array length is len(x)+len(y)-1. The zero lag corresponds to index len(y)-1. So subtracting (len(y)-1) from the argmax index gives the lag where correlation is maximum.