🔍 Finding & Counting Elements in NumPy
The Treasure Hunt in Your Data Chest
Imagine you have a giant toy box filled with thousands of marbles. Some are red, some are blue, some are your favorites. How do you:
- Find which colors you actually have? (unique values)
- See which marbles you share with your friend? (set operations)
- Count how many marbles aren’t clear/empty? (nonzero elements)
- Find exactly WHERE your favorite marble is hiding? (element positions)
NumPy is like having a super-smart helper who can answer all these questions in a blink! 🚀
🎨 Finding Unique Values
The “What Colors Do I Have?” Question
When you pour out your marble collection, you want to know: “What different colors do I actually have?”
import numpy as np
marbles = np.array([3, 1, 2, 3, 1, 3, 2, 1])
unique_marbles = np.unique(marbles)
print(unique_marbles) # [1 2 3]
What happened? NumPy looked at ALL your marbles and said: “You have types 1, 2, and 3!” It removed all the duplicates and sorted them nicely.
Counting Each Type
Want to know HOW MANY of each color?
marbles = np.array([3, 1, 2, 3, 1, 3, 2, 1])
unique, counts = np.unique(marbles, return_counts=True)
print(unique) # [1 2 3]
print(counts) # [3 2 3]
# 3 ones, 2 twos, 3 threes!
Finding Where Each First Appears
Where did I FIRST see each color?
marbles = np.array([3, 1, 2, 3, 1, 3])
unique, indices = np.unique(marbles, return_index=True)
print(unique) # [1 2 3]
print(indices) # [1 2 0]
# First "1" at position 1
# First "2" at position 2
# First "3" at position 0
🤝 Set Operations on Arrays
Playing with Two Toy Boxes
Imagine you AND your friend both have marble collections. NumPy can help you compare them like magic!
graph TD A["Your Marbles: 1,2,3,4"] --> C{Set Operations} B[Friend's Marbles: 3,4,5,6] --> C C --> D["Union: 1,2,3,4,5,6"] C --> E["Intersection: 3,4"] C --> F["Difference: 1,2"]
Union: ALL Marbles Combined
“What if we combined both collections (no duplicates)?”
yours = np.array([1, 2, 3, 4])
friends = np.array([3, 4, 5, 6])
all_marbles = np.union1d(yours, friends)
print(all_marbles) # [1 2 3 4 5 6]
Intersection: Marbles You BOTH Have
“Which marbles do we BOTH own?”
yours = np.array([1, 2, 3, 4])
friends = np.array([3, 4, 5, 6])
shared = np.intersect1d(yours, friends)
print(shared) # [3 4]
Difference: Marbles ONLY You Have
“Which marbles do I have that my friend doesn’t?”
yours = np.array([1, 2, 3, 4])
friends = np.array([3, 4, 5, 6])
only_yours = np.setdiff1d(yours, friends)
print(only_yours) # [1 2]
XOR: Marbles Only ONE of Us Has
“Which marbles are NOT shared between us?”
yours = np.array([1, 2, 3, 4])
friends = np.array([3, 4, 5, 6])
not_shared = np.setxor1d(yours, friends)
print(not_shared) # [1 2 5 6]
Membership Check: Is This Marble in the Collection?
“Is marble 3 in my friend’s collection?”
yours = np.array([1, 2, 3, 4])
friends = np.array([3, 4, 5, 6])
check = np.in1d(yours, friends)
print(check) # [False False True True]
# Your 1? No. Your 2? No. Your 3? Yes! Your 4? Yes!
🔢 Counting Nonzero Elements
The “How Many Real Marbles?” Question
Sometimes your box has empty slots (zeros). You want to count only the REAL marbles!
marbles = np.array([0, 5, 0, 3, 0, 8, 1])
real_count = np.count_nonzero(marbles)
print(real_count) # 4 (the 5, 3, 8, and 1)
Counting in 2D: Rows and Columns
Imagine a shelf with multiple rows of marbles:
shelf = np.array([
[0, 1, 2],
[3, 0, 0],
[0, 4, 5]
])
# Count ALL nonzero marbles
total = np.count_nonzero(shelf)
print(total) # 5
# Count per ROW (how many real marbles in each row?)
per_row = np.count_nonzero(shelf, axis=1)
print(per_row) # [2 1 2]
# Count per COLUMN
per_col = np.count_nonzero(shelf, axis=0)
print(per_col) # [1 2 2]
graph TD A["2D Array"] --> B["axis=None: Count ALL"] A --> C["axis=0: Count per Column"] A --> D["axis=1: Count per Row"]
Boolean Arrays: Counting True Values
True acts like 1, False acts like 0!
scores = np.array([85, 92, 78, 95, 88])
passed = scores >= 80
print(passed) # [True True False True True]
print(np.count_nonzero(passed)) # 4 students passed!
📍 Finding Element Positions
The “WHERE Is My Favorite Marble?” Question
This is like asking: “At which spots in the box can I find red marbles?”
np.where(): The Location Finder
marbles = np.array([10, 25, 30, 25, 40])
# Where are marbles worth 25?
positions = np.where(marbles == 25)
print(positions) # (array([1, 3]),)
# Found at positions 1 and 3!
np.where() with Replacement
“Replace all 25s with 100s, keep others as 0”
marbles = np.array([10, 25, 30, 25, 40])
result = np.where(marbles == 25, 100, 0)
print(result) # [0 100 0 100 0]
np.argmax() and np.argmin(): Find the Champion!
“Which position has the BIGGEST marble?”
marbles = np.array([10, 45, 30, 45, 20])
biggest_pos = np.argmax(marbles)
print(biggest_pos) # 1 (first 45 found)
smallest_pos = np.argmin(marbles)
print(smallest_pos) # 0 (the 10)
np.searchsorted(): Where Would This Fit?
Imagine a sorted line of marbles. Where would a NEW marble go?
sorted_marbles = np.array([10, 20, 30, 40, 50])
new_marble = 25
position = np.searchsorted(sorted_marbles, new_marble)
print(position) # 2 (between 20 and 30)
np.argwhere(): Get Full Coordinates
For 2D arrays, where exactly is each treasure?
grid = np.array([
[0, 1, 0],
[2, 0, 3],
[0, 4, 0]
])
# Where are all nonzero elements?
coords = np.argwhere(grid > 0)
print(coords)
# [[0 1] <- row 0, col 1 (value: 1)
# [1 0] <- row 1, col 0 (value: 2)
# [1 2] <- row 1, col 2 (value: 3)
# [2 1]] <- row 2, col 1 (value: 4)
🎯 Quick Summary
| Task | Function | Example |
|---|---|---|
| Find unique values | np.unique() |
Remove duplicates |
| Union of sets | np.union1d() |
Combine two arrays |
| Intersection | np.intersect1d() |
Find common elements |
| Difference | np.setdiff1d() |
Elements in A not in B |
| Count non-zeros | np.count_nonzero() |
Skip zeros, count rest |
| Find positions | np.where() |
Get indices matching condition |
| Find max position | np.argmax() |
Index of largest value |
| Find insert point | np.searchsorted() |
Where to insert in sorted array |
🌟 You Did It!
You now have super powers to:
- 🎨 Find all unique items in your data
- 🤝 Compare collections like a pro
- 🔢 Count the real stuff (ignore the zeros)
- 📍 Locate any element instantly
These tools turn you into a data detective — finding exactly what you need in mountains of numbers! 🔍✨
