0
0
NumPydata~10 mins

np.unique() for unique elements 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 find unique elements in the 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'
Aunique
Barray
Csort
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sort() instead of np.unique()
Trying to use list methods instead of numpy functions
2fill in blank
medium

Complete the code to find unique elements and their counts.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique_elements, counts = np.unique(arr, [1]=True)
print(unique_elements)
print(counts)
Drag options to blanks, or click blank then click option'
Areturn_inverse
Breturn_index
Creturn_counts
Dreturn_sorted
Attempts:
3 left
💡 Hint
Common Mistakes
Using return_index instead of return_counts
Forgetting to set the argument to True
3fill in blank
hard

Fix the error in the code to get unique elements from a 2D array.

NumPy
import numpy as np
arr = np.array([[1, 2], [2, 3]])
unique_elements = np.unique(arr, [1]=0)
print(unique_elements)
Drag options to blanks, or click blank then click option'
Areturn_counts
Baxis
Csort
Dreturn_index
Attempts:
3 left
💡 Hint
Common Mistakes
Using return_counts instead of axis
Not specifying axis when working with 2D arrays
4fill in blank
hard

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

NumPy
import numpy as np
arr = np.array([5, 6, 5, 7, 6, 8])
unique, counts = np.unique(arr, [1]=True)
result = [2](zip(unique, counts))
print(result)
Drag options to blanks, or click blank then click option'
Areturn_counts
Bdict
Clist
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using list() instead of dict() for the second blank
Forgetting to set return_counts to True
5fill in blank
hard

Fill both blanks to filter unique elements greater than 3 and create a list.

NumPy
import numpy as np
arr = np.array([1, 4, 4, 5, 2, 6, 3])
unique = np.unique(arr)
filtered = [x for x in unique if x [1] 3]
result = [2](filtered)
print(result)
Drag options to blanks, or click blank then click option'
A>
Blist
C<
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > for filtering
Using set() instead of list() for the result