0
0
NumPydata~10 mins

np.unique() for unique values 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 values in the array.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 4, 4, 5])
unique_values = np.[1](arr)
print(unique_values)
Drag options to blanks, or click blank then click option'
Aunique
Bsort
Carray
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 values and return their counts.

NumPy
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique_vals, counts = np.unique(arr, [1]=True)
print(unique_vals, counts)
Drag options to blanks, or click blank then click option'
Areturn_index
Breturn_sorted
Creturn_inverse
Dreturn_counts
Attempts:
3 left
💡 Hint
Common Mistakes
Using return_index instead of return_counts
Not setting any argument and expecting counts
3fill in blank
hard

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

NumPy
import numpy as np
arr = np.array([[1, 2], [2, 3]])
unique_vals = np.unique(arr, [1]=1)
print(unique_vals)
Drag options to blanks, or click blank then click option'
Aaxis
Breturn_index
Caxis=0
Dreturn_counts
Attempts:
3 left
💡 Hint
Common Mistakes
Using return_counts instead of axis
Passing axis as a keyword argument value instead of parameter name
4fill in blank
hard

Fill both blanks to create a dictionary of unique values and their counts from a list.

NumPy
import numpy as np
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']
unique_words, counts = np.unique(words, [1]=True)
word_count = { [2]: counts[i] for i, [2] in enumerate(unique_words) }
print(word_count)
Drag options to blanks, or click blank then click option'
Areturn_counts
Breturn_index
Cword
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using return_index instead of return_counts
Using count as dictionary key instead of word
5fill in blank
hard

Fill all three blanks to filter unique numbers greater than 3 and create a list of them.

NumPy
import numpy as np
numbers = np.array([1, 2, 3, 4, 5, 5, 6])
unique_nums = np.unique(numbers)
filtered = [num for num in unique_nums if num [1] [2]]
result = [3](filtered)
print(result)
Drag options to blanks, or click blank then click option'
A>
B3
Clist
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>'
Forgetting to convert filtered result to list