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
Find Unique Elements Using np.unique()
📖 Scenario: You work in a store and have a list of product IDs sold today. Some products were sold multiple times.You want to find out which unique products were sold to understand your sales better.
🎯 Goal: Build a small program that uses np.unique() to find unique product IDs from a list.
📋 What You'll Learn
Create a numpy array called product_ids with the exact values: [101, 203, 101, 405, 203, 507]
Create a variable called unique_products to store unique product IDs using np.unique()
Print the unique_products array to see the unique product IDs
💡 Why This Matters
🌍 Real World
Finding unique items in sales data helps stores understand what products are popular and manage inventory better.
💼 Career
Data scientists often need to clean and analyze data by identifying unique values to prepare datasets for further analysis.
Progress0 / 4 steps
1
Create the product IDs array
Create a numpy array called product_ids with these exact values: [101, 203, 101, 405, 203, 507]
NumPy
Hint
Use np.array() to create the array with the exact list of numbers.
2
Use np.unique() to find unique product IDs
Create a variable called unique_products and assign it the result of np.unique(product_ids) to find unique product IDs.
NumPy
Hint
Call np.unique() with product_ids inside the parentheses.
3
Print the unique product IDs
Write a print statement to display the unique_products array.
NumPy
Hint
Use print(unique_products) to show the unique product IDs.
4
Complete program to find and print unique product IDs
Combine all previous steps to create a complete program that imports numpy, creates the product_ids array, finds unique products with np.unique(), and prints the result.
NumPy
Hint
Run the full program to see the unique product IDs printed.
Practice
(1/5)
1. What does the np.unique() function do when applied to a NumPy array?
easy
A. Returns the shape of the array
B. Returns the sum of all elements in the array
C. Returns the maximum value in the array
D. Returns all unique elements sorted from the array
Solution
Step 1: Understand the purpose of np.unique()
The function np.unique() extracts all different values from the input array and sorts them.
Step 2: Compare with other options
Options A, B, and D describe different functions like shape, sum, and max, which are not what np.unique() does.
Final Answer:
Returns all unique elements sorted from the array -> Option D
2. Which of the following is the correct syntax to get unique elements from a NumPy array arr?
easy
A. np.unique(arr)
B. arr.unique()
C. unique(arr)
D. np.arr.unique()
Solution
Step 1: Recall the correct function call
The correct way to call the unique function in NumPy is np.unique(arr).
Step 2: Identify incorrect syntax
Options A, B, and C are invalid because either the method does not exist on the array object or the function is not called from the NumPy module correctly.
Final Answer:
np.unique(arr) -> Option A
Quick Check:
Use np.unique(array) syntax [OK]
Hint: Always call unique as np.unique(array) [OK]
Common Mistakes:
Trying to call unique as a method on array
Forgetting the np. prefix
Using a non-existent unique() function without np
3. What is the output of the following code?
import numpy as np
arr = np.array([3, 1, 2, 3, 2, 1, 4])
result = np.unique(arr)
print(result)
medium
A. [4 3 2 1]
B. [3 1 2 4]
C. [1 2 3 4]
D. [1 1 2 2 3 3 4]
Solution
Step 1: Apply np.unique() to the array
The function extracts unique values from the array: 1, 2, 3, and 4.
Step 2: Note the sorting behavior
np.unique() returns these unique values sorted in ascending order: [1 2 3 4].