Bird
Raised Fist0
NumPydata~20 mins

Convolution with np.convolve() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Convolution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of simple 1D convolution
What is the output of this code using np.convolve() with default mode?
NumPy
import numpy as np
x = np.array([1, 2, 3])
h = np.array([0, 1, 0.5])
result = np.convolve(x, h)
print(result)
A[0. 1. 2.5 4. 1.5]
B[1. 2. 3. 0. 0.5]
C[0. 1. 2. 3. 1.5]
D[0. 1. 2.5 3. 1.5]
Attempts:
2 left
💡 Hint
Remember that default mode is 'full' which gives the complete convolution result.
❓ data_output
intermediate
1:30remaining
Length of convolution output with 'valid' mode
Given two arrays a = np.array([1, 2, 3, 4]) and b = np.array([1, 0, -1]), what is the length of the output when convolving with mode='valid'?
NumPy
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([1, 0, -1])
result = np.convolve(a, b, mode='valid')
print(len(result))
A4
B2
C3
D6
Attempts:
2 left
💡 Hint
Length in 'valid' mode is max(len(a), len(b)) - min(len(a), len(b)) + 1.
🔧 Debug
advanced
2:00remaining
Identify the error in convolution code
What error does this code raise when run?
NumPy
import numpy as np
x = np.array([1, 2, 3])
h = np.array([[0, 1, 0.5]])
result = np.convolve(x, h)
print(result)
ATypeError: object of type 'numpy.ndarray' has no len()
BValueError: object too deep for desired array
CTypeError: cannot perform reduce with flexible type
DTypeError: 1D arrays expected
Attempts:
2 left
💡 Hint
Check the shape of the arrays passed to np.convolve.
🧠 Conceptual
advanced
1:30remaining
Effect of 'same' mode in np.convolve()
Which statement best describes the output length when using np.convolve(a, b, mode='same')?
AOutput length matches the length of the first input array.
BOutput length is the maximum of the two input lengths.
COutput length matches the length of the second input array.
DOutput length is always the sum of input lengths minus one.
Attempts:
2 left
💡 Hint
The 'same' mode centers the full convolution output to match the first input's length.
🚀 Application
expert
2:30remaining
Using convolution to compute moving average
You want to compute a simple moving average of window size 3 on array data = np.array([1, 2, 3, 4, 5]). Which np.convolve() call produces the correct moving average values aligned with the original data length?
Anp.convolve(data, np.ones(3)/3, mode='full')
Bnp.convolve(data, np.ones(3)/3, mode='valid')
Cnp.convolve(data, np.ones(3)/3, mode='same')
Dnp.convolve(data, np.ones(3), mode='same') / 3
Attempts:
2 left
💡 Hint
Moving average should have the same length as data and average over 3 points centered.

Practice

(1/5)
1. What does the np.convolve() function do in numpy?
easy
A. It combines two arrays to produce a third array showing how one affects the other.
B. It sorts an array in ascending order.
C. It finds the maximum value in an array.
D. It reshapes an array into a different dimension.

Solution

  1. Step 1: Understand the purpose of convolution

    Convolution combines two arrays to analyze how one array modifies or overlaps with the other.
  2. Step 2: Match this with np.convolve() function

    In numpy, np.convolve() performs this combination and returns the resulting array.
  3. Final Answer:

    It combines two arrays to produce a third array showing how one affects the other. -> Option A
  4. Quick Check:

    np.convolve() = combine arrays [OK]
Hint: Convolution means combining arrays to see overlap effects [OK]
Common Mistakes:
  • Confusing convolution with sorting or reshaping
  • Thinking it finds max or min values
  • Assuming it changes array shape without combining
2. Which of the following is the correct syntax to convolve two arrays a and b using numpy?
easy
A. np.convolve(a, b, mode='sum')
B. np.convolve(a + b)
C. np.convolve(a * b)
D. np.convolve(a, b)

Solution

  1. Step 1: Recall the np.convolve() function signature

    The function requires two arrays as separate arguments: np.convolve(a, b).
  2. Step 2: Check each option

    np.convolve(a, b) correctly passes two arrays. Options B and D use invalid expressions inside convolve. np.convolve(a, b, mode='sum') uses an invalid mode value.
  3. Final Answer:

    np.convolve(a, b) -> Option D
  4. Quick Check:

    Correct syntax = np.convolve(a, b) [OK]
Hint: np.convolve takes two arrays as separate arguments [OK]
Common Mistakes:
  • Passing combined arrays instead of two separate arguments
  • Using invalid mode values
  • Using operators inside np.convolve() argument
3. What is the output of the following code?
import numpy as np
x = np.array([1, 2, 3])
k = np.array([0, 1])
result = np.convolve(x, k)
print(result)
medium
A. [0 1 2]
B. [1 2 3 0]
C. [0 1 2 3]
D. [1 2 3]

Solution

  1. Step 1: Understand convolution of x and k

    Convolution slides kernel k over x, multiplying and summing overlapping elements. Here, x=[1,2,3], k=[0,1].
  2. Step 2: Calculate convolution manually

    Output length = len(x)+len(k)-1 = 3+2-1=4.
    Values:
    Index 0: 1*0=0
    Index 1: 1*1 + 2*0=1
    Index 2: 2*1 + 3*0=2
    Index 3: 3*1=3
    Result: [0, 1, 2, 3]
  3. Final Answer:

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

    Convolution length = 4, values match [OK]
Hint: Output length = len(a)+len(b)-1 for default mode [OK]
Common Mistakes:
  • Forgetting output length formula
  • Mixing up order of multiplication
  • Assuming output length equals input length
4. The following code throws an error. What is the mistake?
import numpy as np
x = np.array([1, 2, 3])
k = np.array([1, 0])
result = np.convolve(x, k, mode='middle')
print(result)
medium
A. Missing import statement for np.convolve()
B. 'middle' is not a valid mode for np.convolve()
C. np.convolve() requires integer arrays only
D. Arrays x and k must be the same length

Solution

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

    Valid modes are 'full', 'valid', and 'same'. 'middle' is not valid.
  2. Step 2: Identify error cause

    Using mode='middle' causes a ValueError because it's unsupported.
  3. Final Answer:

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

    Valid modes = full, valid, same [OK]
Hint: Use only 'full', 'valid', or 'same' modes in np.convolve() [OK]
Common Mistakes:
  • Using unsupported mode strings
  • Assuming arrays must be same length
  • Thinking np.convolve needs special imports
5. You want to smooth a noisy signal stored in array signal using a simple moving average kernel of length 3. Which code correctly applies convolution to achieve this smoothing?
hard
A. kernel = np.array([1, 1, 1]) smoothed = np.convolve(signal, kernel, mode='valid') / 3
B. kernel = np.array([1, 1, 1]) smoothed = np.convolve(signal, kernel, mode='full') * 3
C. kernel = np.array([1, 0, 1]) smoothed = np.convolve(signal, kernel, mode='same') / 2
D. kernel = np.array([1, 1, 1]) smoothed = np.convolve(signal, kernel, mode='valid') * 3

Solution

  1. Step 1: Define the moving average kernel

    A simple moving average kernel of length 3 is [1, 1, 1].
  2. Step 2: Apply convolution and normalize

    Convolve signal with kernel using mode='valid' to avoid edge effects, then divide by 3 to average.
  3. Final Answer:

    kernel = np.array([1, 1, 1]) smoothed = np.convolve(signal, kernel, mode='valid') / 3 -> Option A
  4. Quick Check:

    Moving average = sum/3 with valid mode [OK]
Hint: Use kernel of ones and divide by length for moving average [OK]
Common Mistakes:
  • Forgetting to divide by kernel length
  • Using wrong kernel values
  • Using mode='full' causing longer output
  • Multiplying instead of dividing for average
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