Bird
Raised Fist0
NumPydata~20 mins

Correlation with np.correlate() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What does the np.correlate() function compute between two arrays?

easy
A. The similarity by sliding one array over another and summing products
B. The sum of all elements in both arrays
C. The difference between the maximum values of the arrays
D. The element-wise multiplication without any sliding

Solution

  1. Step 1: Understand correlation concept

    Correlation measures how similar two sequences are by sliding one over the other and summing the products of overlapping elements.
  2. Step 2: Relate to np.correlate()

    The np.correlate() function implements this sliding and summing operation to compute similarity.
  3. Final Answer:

    The similarity by sliding one array over another and summing products -> Option A
  4. Quick Check:

    Correlation = similarity by sliding and summing [OK]
Hint: Correlation slides and sums products to measure similarity [OK]
Common Mistakes:
  • Confusing correlation with element-wise multiplication only
  • Thinking it sums all elements instead of sliding sums
  • Mixing correlation with difference or subtraction
2.

Which of the following is the correct syntax to compute the full correlation of two arrays a and b using NumPy?

import numpy as np
a = np.array([1, 2, 3])
b = np.array([0, 1, 0])
# What is the correct call?
easy
A. np.correlate(a, b)
B. np.correlate(a, b, method='slide')
C. np.corrcoef(a, b)
D. np.correlate(a, b, mode='full')

Solution

  1. Step 1: Recall np.correlate syntax

    The function np.correlate() takes two arrays and an optional mode parameter to control output size.
  2. Step 2: Identify correct usage

    Using mode='full' returns the complete correlation result. The default mode is 'valid'. np.correlate(a, b, mode='full') uses correct syntax and mode.
  3. Final Answer:

    np.correlate(a, b, mode='full') -> Option D
  4. Quick Check:

    np.correlate with mode='full' is correct syntax [OK]
Hint: Use mode='full' for complete correlation output [OK]
Common Mistakes:
  • Using np.corrcoef instead of np.correlate
  • Passing invalid parameter 'method'
  • Omitting mode when full output is needed
3.

What is the output of the following code?

import numpy as np
x = np.array([1, 2, 3])
y = np.array([0, 1, 0])
result = np.correlate(x, y, mode='full')
print(result)
medium
A. [3 2 1 0 0]
B. [0 1 2]
C. [0 1 2 3 0]
D. [0 1 2 0 0]

Solution

  1. Step 1: Understand mode='full' output length

    For arrays of length 3 and 3, full mode output length is 3+3-1=5.
  2. Step 2: Calculate correlation values

    Sliding y over x:
    Positions and sums:
    Index 0: x[0]*y[2]=1*0=0
    Index 1: x[0]*y[1]+x[1]*y[2]=1*1+2*0=1
    Index 2: x[0]*y[0]+x[1]*y[1]+x[2]*y[2]=1*0+2*1+3*0=2
    Index 3: x[1]*y[0]+x[2]*y[1]=2*0+3*1=3
    Index 4: x[2]*y[0]=3*0=0
    So result is [0 1 2 3 0]
  3. Final Answer:

    [0 1 2 3 0] -> Option C
  4. Quick Check:

    np.correlate full mode length = 5, values match [OK]
Hint: Remember full mode length = len(a)+len(b)-1 [OK]
Common Mistakes:
  • Not flipping second array in correlation
  • Confusing correlation with convolution
  • Wrong output length for full mode
4.

Identify the error in the following code snippet:

import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
result = np.correlate(x, y, mode='middle')
print(result)
medium
A. 'middle' is not a valid mode for np.correlate()
B. Arrays x and y must be the same length
C. np.correlate requires 3 arrays as input
D. The function np.correlate does not exist in numpy

Solution

  1. Step 1: Check valid modes for np.correlate()

    Valid modes are 'valid', 'same', and 'full'. 'middle' is not a valid mode.
  2. Step 2: Confirm function usage

    Arrays can be different lengths; np.correlate supports that. The function exists and takes two arrays.
  3. Final Answer:

    'middle' is not a valid mode for np.correlate() -> Option A
  4. Quick Check:

    Valid modes = 'valid', 'same', 'full' only [OK]
Hint: Use only 'valid', 'same', or 'full' for mode [OK]
Common Mistakes:
  • Using unsupported mode strings
  • Assuming arrays must be same length
  • Thinking np.correlate needs more than two arrays
5.

You have two time series arrays representing daily sales: sales_a = np.array([10, 20, 30, 40]) and sales_b = np.array([0, 1, 0]). You want to find the day offset where sales_b best matches sales_a. Which np.correlate() call and mode should you use to find this offset?

hard
A. np.correlate(sales_a, sales_b, mode='valid')
B. np.correlate(sales_a, sales_b, mode='full')
C. np.correlate(sales_b, sales_a, mode='same')
D. np.correlate(sales_a, sales_b, mode='same')

Solution

  1. Step 1: Understand the goal

    To find the best matching offset, we need the full correlation result to see similarity at all shifts.
  2. Step 2: Choose correct mode and order

    Using mode='full' gives all possible overlaps. The order np.correlate(sales_a, sales_b) is correct to slide sales_b over sales_a.
  3. Final Answer:

    np.correlate(sales_a, sales_b, mode='full') -> Option B
  4. Quick Check:

    Full mode shows all offsets for best match [OK]
Hint: Use mode='full' to find best offset in correlation [OK]
Common Mistakes:
  • Using 'valid' mode which limits output length
  • Swapping array order incorrectly
  • Using 'same' mode which centers output, hiding edges
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard