0
0
NumPydata~20 mins

np.unique() for unique values in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.unique Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.unique() with return_counts
What is the output of this code snippet?
NumPy
import numpy as np
arr = np.array([3, 1, 2, 3, 2, 1, 4])
unique_vals, counts = np.unique(arr, return_counts=True)
print(unique_vals)
print(counts)
A
[1 2 3 4]
[2 2 2 1]
B
[3 1 2 4]
[2 2 2 1]
C
[1 2 3 4]
[1 2 2 2]
D
[1 2 3 4]
[2 1 2 2]
Attempts:
2 left
💡 Hint
np.unique() sorts the unique values by default.
data_output
intermediate
1:30remaining
Number of unique elements in a 2D array
How many unique elements are in this 2D numpy array?
NumPy
import numpy as np
arr = np.array([[5, 2, 5], [3, 2, 1]])
unique_elements = np.unique(arr)
print(len(unique_elements))
A3
B5
C6
D4
Attempts:
2 left
💡 Hint
Count all distinct numbers in the array.
Predict Output
advanced
2:30remaining
Output of np.unique() with return_index and return_inverse
What will be printed by this code?
NumPy
import numpy as np
arr = np.array([7, 2, 7, 3, 2])
unique_vals, indices, inverse = np.unique(arr, return_index=True, return_inverse=True)
print(unique_vals)
print(indices)
print(inverse)
A
[2 3 7]
[1 3 0]
[2 0 2 1 0]
B
[2 3 7]
[1 3 0]
[0 2 0 1 2]
C
[2 3 7]
[1 3 0]
[0 1 0 2 1]
D
[7 2 3]
[0 1 3]
[0 1 0 2 1]
Attempts:
2 left
💡 Hint
return_index gives the first index of each unique value in the original array. return_inverse maps original elements to unique indices.
visualization
advanced
3:00remaining
Visualizing unique values and their counts
Which option shows the correct bar chart for unique values and their counts from this array?
NumPy
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique_vals, counts = np.unique(arr, return_counts=True)
plt.bar(unique_vals, counts)
plt.xlabel('Unique Values')
plt.ylabel('Counts')
plt.title('Counts of Unique Values')
plt.show()
ABar chart with bars at 1,2,3,4 with heights 2,2,2,1
BBar chart with bars at 1,2,3,4 with heights 1,2,3,1
CBar chart with bars at 1,2,3,4 with heights 1,3,2,1
DBar chart with bars at 1,2,3,4 with heights 1,1,3,2
Attempts:
2 left
💡 Hint
Count how many times each number appears in the array.
🧠 Conceptual
expert
3:00remaining
Effect of np.unique() on structured arrays
Given a structured numpy array with fields 'name' and 'age', what does np.unique() return by default?
NumPy
import numpy as np
arr = np.array([('Alice', 25), ('Bob', 30), ('Alice', 25)], dtype=[('name', 'U10'), ('age', 'i4')])
unique_arr = np.unique(arr)
print(unique_arr)
AArray with unique names only: ['Alice', 'Bob']
BArray with unique ages only: [25, 30]
CArray with unique rows: [('Alice', 25), ('Bob', 30)]
DRaises a TypeError because structured arrays are not supported
Attempts:
2 left
💡 Hint
np.unique() compares entire rows for structured arrays.