0
0
NumPydata~10 mins

Why vectorized operations matter in NumPy - Test Your Understanding

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 a Python list.

NumPy
import numpy as np
arr = np.[1]([1, 2, 3, 4, 5])
Drag options to blanks, or click blank then click option'
Aarray
Blist
Cmatrix
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' will cause an error because NumPy has no 'list' function.
Using 'matrix' creates a matrix object, not a general array.
Using 'range' is a Python built-in, not a NumPy function.
2fill in blank
medium

Complete the code to add 5 to every element in the NumPy array using vectorized operations.

NumPy
import numpy as np
arr = np.array([1, 2, 3])
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 '*' multiplies instead of adding.
Using '-' subtracts instead of adding.
Using '/' divides instead of adding.
3fill in blank
hard

Fix the error in the code to multiply two NumPy arrays element-wise.

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

Fill both blanks to create a vectorized operation that squares each element and then filters elements greater than 10.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
squared = arr [1] 2
filtered = squared[squared [2] 10]
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' will multiply by 2, not square.
Using '<' filters elements less than 10, not greater.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

NumPy
words = ['data', 'is', 'fun', 'and', 'cool']
lengths = { [1] : [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'item' instead of 'word' causes a NameError.
Using the wrong variable name in the loop breaks the comprehension.