0
0
NumPydata~10 mins

Vectorization over loops 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 create a NumPy array from the list.

NumPy
import numpy as np
numbers = [1, 2, 3, 4, 5]
arr = np.[1](numbers)
Drag options to blanks, or click blank then click option'
Aarray
Brange
Cmatrix
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' which is not a NumPy function.
Using 'matrix' which is a different NumPy type.
Using 'range' which creates a range object, not an array.
2fill in blank
medium

Complete the code to add 5 to every element in the array using vectorization.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
result = arr [1] 5
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which multiplies instead of adds.
Using '-' which subtracts instead of adds.
Using '/' which divides instead of adds.
3fill in blank
hard

Fix the error in the code to multiply each element by 2 using vectorization.

NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = arr [1] 2
Drag options to blanks, or click blank then click option'
Adot
Bmul
Cmultiply
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'multiply' or 'mul' as methods which do not exist on arrays.
Using 'dot' which performs matrix multiplication, not element-wise.
4fill in blank
hard

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

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<=' which selects words with length 3 or less.
5fill in blank
hard

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

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = { [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase words as keys instead of uppercase.
Using '<=' which includes short words.
Using the word itself as value instead of length.