0
0
NumPydata~10 mins

Why set 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])
print(arr)
Drag options to blanks, or click blank then click option'
Aset
Blist
Carray
Dmatrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' causes an error.
Using 'set' creates a Python set, not a NumPy array.
2fill in blank
medium

Complete the code to find unique elements in a NumPy array.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 4, 4, 5])
unique_elements = np.[1](arr)
print(unique_elements)
Drag options to blanks, or click blank then click option'
Aset
Bunique
Cdistinct
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' returns a Python set, not a NumPy array.
Using 'sort' only sorts but does not remove duplicates.
3fill in blank
hard

Fix the error in the code to find the intersection of two NumPy arrays.

NumPy
import numpy as np
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
common = np.[1](arr1, arr2)
print(common)
Drag options to blanks, or click blank then click option'
Aintersect1d
Bintersect
Cintersection1d
Dintersect_1d
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'intersect' causes an AttributeError.
Using 'intersection1d' is a common misspelling and causes an error.
4fill in blank
hard

Fill both blanks to create a dictionary of unique elements and their counts from a NumPy array.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3])
unique, counts = np.[1](arr, return_counts=True)
result = dict(zip(unique, [2]))
print(result)
Drag options to blanks, or click blank then click option'
Aunique
Bcounts
Ccount
Dfrequency
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' instead of 'counts' causes a NameError.
Not using return_counts=True in np.unique causes unpacking error.
5fill in blank
hard

Fill all three blanks to filter unique elements greater than 2 and create a dictionary with their counts.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique, counts = np.unique(arr, return_counts=True)
filtered = unique[unique [1] 2]
filtered_counts = counts[unique [2] 2]
result = dict(zip(filtered, [3]))
print(result)
Drag options to blanks, or click blank then click option'
A>
Bcounts
C<
Dfiltered_counts
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters wrong elements.
Using 'counts' instead of 'filtered_counts' causes mismatch.