Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Finding Unique Values with np.unique()
📖 Scenario: You work in a store that tracks daily sales of different fruits. Sometimes the same fruit is sold multiple times in a day. You want to find out which unique fruits were sold today.
🎯 Goal: Build a small program that uses np.unique() to find all unique fruits sold from a list of sales.
📋 What You'll Learn
Create a numpy array called sales with the exact fruit names given.
Create a variable called unique_fruits that stores the unique fruit names using np.unique().
Print the unique_fruits array to see the result.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to find unique items sold or listed to analyze inventory or sales trends.
💼 Career
Data analysts and scientists use <code>np.unique()</code> to quickly find unique values in datasets for reporting and decision making.
Progress0 / 4 steps
1
Create the sales data array
Create a numpy array called sales with these exact fruit names in this order: 'apple', 'banana', 'apple', 'orange', 'banana', 'kiwi'.
NumPy
Hint
Use np.array([...]) to create the array with the exact fruit names.
2
Set up the variable for unique fruits
Create a variable called unique_fruits and set it to None for now as a placeholder.
NumPy
Hint
This step just prepares the variable. We will update it next.
3
Find unique fruits using np.unique()
Use np.unique() on the sales array and assign the result to the variable unique_fruits.
NumPy
Hint
Call np.unique() with sales inside the parentheses.
4
Print the unique fruits
Print the variable unique_fruits to display the unique fruit names.
NumPy
Hint
Use print(unique_fruits) to show the unique fruits.
Practice
(1/5)
1. What does the np.unique() function do in NumPy?
easy
A. Calculates the sum of all elements
B. Sorts the array in descending order
C. Reverses the order of elements
D. Finds all unique values in an array
Solution
Step 1: Understand the purpose of np.unique()
This function is designed to find all different (unique) values in a NumPy array or list.
Step 2: Compare with other options
Sorting, summing, or reversing are different operations and not what np.unique() does.
Final Answer:
Finds all unique values in an array -> Option D
Quick Check:
np.unique() = unique values [OK]
Hint: Remember: unique means different values only [OK]
Common Mistakes:
Confusing unique with sorting
Thinking it sums values
Assuming it reverses array
2. Which of the following is the correct syntax to get unique values from a NumPy array arr?
easy
A. np.arr.unique()
B. np.unique(arr)
C. unique(arr)
D. arr.unique()
Solution
Step 1: Recall the correct function call
The function unique is part of the NumPy module and is called as np.unique().
Step 2: Check other options for errors
arr.unique() is not a NumPy array method, unique(arr) misses the module prefix, and np.arr.unique() is invalid syntax.