0
0
NumPydata~20 mins

Correlation with np.correlate() in NumPy - 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 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)
A[2.0 3.5 3.0]
B[3.5]
C[2.5]
D[0.5 2.0 3.5]
Attempts:
2 left
💡 Hint
Remember that mode='valid' returns only positions where the signals fully overlap.
data_output
intermediate
1: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))
A3
B6
C7
D4
Attempts:
2 left
💡 Hint
The length of the full correlation output is length(x) + length(y) - 1.
🔧 Debug
advanced
2: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)
ATypeError: np.correlate() only supports 1D arrays
BValueError: object too deep for desired array
CNo error, prints the correlation result
DTypeError: unsupported operand type(s) for *: 'int' and 'list'
Attempts:
2 left
💡 Hint
Check the shape of the input arrays; np.correlate expects 1D arrays.
🧠 Conceptual
advanced
1: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?
Anp.correlate computes the sum of element-wise products of x and the reversed y.
Bnp.correlate computes the convolution of x and y without reversing any array.
Cnp.correlate computes the sum of element-wise products of x and y without reversing y.
Dnp.correlate computes the sum of element-wise products of reversed x and y.
Attempts:
2 left
💡 Hint
Correlation involves flipping one of the signals before sliding.
🚀 Application
expert
2: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])
A
corr = np.correlate(x[::-1], y, mode='full')
lag = np.argmax(corr) - (len(y) - 1)
print(lag)
B
corr = np.correlate(x, y, mode='valid')
lag = np.argmax(corr)
print(lag)
C
corr = np.correlate(x, y[::-1], mode='full')
lag = np.argmax(corr) - (len(x) - 1)
print(lag)
D
corr = np.correlate(x, y, mode='full')
lag = np.argmax(corr) - (len(y) - 1)
print(lag)
Attempts:
2 left
💡 Hint
The lag index is offset by the length of the second array minus one in full mode.