Lesson 17
Unique Values and Sets
Find unique values with counts and first positions, unique rows and columns, and combine arrays with union, intersection, and difference.
np.unique returns each distinct value once, sorted. It can also count how often each value appears and where it first appears. NumPy's set functions combine two 1-D arrays the way Python sets do.
Unique values
The result is sorted and has no repeats.
import numpy as np
arr = np.array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18])
print(np.unique(arr))Output:
[11 12 13 14 15 16 17 18]Counts and first positions
return_counts=True adds how many times each value appears. return_index=True adds the position of its first appearance. With both, you get three arrays back, in the order values, positions, counts.
import numpy as np
arr = np.array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18])
values, first, counts = np.unique(arr, return_index=True, return_counts=True)
print(values)
print(first)
print(counts)
for m, p in zip(values, counts):
if p > 1:
print(m, "appears", p, "times")Output:
[11 12 13 14 15 16 17 18]
[ 0 2 3 4 5 6 7 12]
[3 2 2 2 1 1 1 1]
11 appears 3 times
12 appears 2 times
13 appears 2 times
14 appears 2 timesUnique rows and columns
Without an axis, a 2-D array is flattened first. axis=0 keeps the unique rows and axis=1 the unique columns.
import numpy as np
m = np.array([[11, 11, 12, 11],
[13, 11, 12, 11],
[16, 11, 12, 11],
[11, 11, 12, 11]])
print(np.unique(m))
print(np.unique(m, axis=0))
print(np.unique(m, axis=1))Output:
[11 12 13 16]
[[11 11 12 11]
[13 11 12 11]
[16 11 12 11]]
[[11 11 12]
[11 13 12]
[11 16 12]
[11 11 12]]Set operations
union1d keeps values in either array. intersect1d keeps values in both. setdiff1d(a, b) keeps values of a that are not in b. setxor1d keeps values in exactly one. np.isin gives a mask of which values of a appear in b. Pass assume_unique=True only when neither array has repeats. It is faster, and wrong if repeats are present.
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([3, 4, 5, 6])
print(np.union1d(a, b))
print(np.intersect1d(a, b, assume_unique=True))
print(np.setdiff1d(a, b))
print(np.setxor1d(a, b))
print(np.isin(a, b))Output:
[1 2 3 4 5 6]
[3 4]
[1 2]
[1 2 5 6]
[False False True True]Key takeaways
-
np.uniquereturns sorted values with no repeats -
return_countsandreturn_indexadd counts and first positions -
axis=0finds unique rows,axis=1unique columns -
union1d,intersect1d,setdiff1d, andsetxor1dwork on 1-D arrays -
Next: Linear Algebra
-
Previous: Sums, Products, and Rounding
Loading playground…
Check your understanding
Unique Values and Sets Quiz
What is np.unique([3, 1, 3, 2])?
1 / 10