0
0
NumPydata~10 mins

Convolution with np.convolve() in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform convolution of two arrays using numpy.

NumPy
import numpy as np

result = np.[1](a, b)
print(result)
Drag options to blanks, or click blank then click option'
Adot
Bconvolve
Cmultiply
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.dot instead of np.convolve
Using np.multiply which does element-wise multiplication
Using np.add which sums arrays element-wise
2fill in blank
medium

Complete the code to specify the convolution mode as 'valid'.

NumPy
import numpy as np

result = np.convolve(a, b, mode=[1])
print(result)
Drag options to blanks, or click blank then click option'
A'valid'
B'same'
C'full'
D'extend'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'full' mode which returns the complete convolution
Using 'same' mode which returns output the same size as first array
Using an invalid mode string
3fill in blank
hard

Fix the error in the code to correctly perform convolution.

NumPy
import numpy as np

a = [1, 2, 3]
b = [0, 1, 0.5]

result = np.convolve([1], b)
print(result)
Drag options to blanks, or click blank then click option'
Aa
Bb
Cresult
Dnp
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of arrays
Passing the result variable instead of input arrays
Passing the numpy module instead of arrays
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values for words longer than 3 characters.

NumPy
words = ['data', 'science', 'ai', 'ml', 'python']
lengths = { [1]: [2] for word in words if len(word) > 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.upper()
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length as key instead of value
Using word.upper() as key which changes the word
Using len function object instead of calling it
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys, their lengths as values, only for words longer than 3 characters.

NumPy
words = ['data', 'science', 'ai', 'ml', 'python']
lengths = { [1]: [2] for word in words if len(word) [3] 3 }
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase words as keys
Using '<' instead of '>' in condition
Using the function len without parentheses