0
0
NumPydata~15 mins

np.choose() for conditional selection in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Using np.choose() for Conditional Selection
📖 Scenario: Imagine you are analyzing sales data for a small store. You want to categorize daily sales numbers into different labels based on their values.
🎯 Goal: You will create a NumPy array of sales numbers, define conditions to categorize these sales, use np.choose() to assign category labels, and finally display the categorized results.
📋 What You'll Learn
Create a NumPy array called sales with exact values: [150, 85, 230, 45, 120]
Create a NumPy array called conditions with three conditions for sales: less than 50, between 50 and 150 (inclusive), and greater than 150
Use np.choose() with the conditions array to assign categories: 'Low', 'Medium', and 'High'
Print the final array of category labels
💡 Why This Matters
🌍 Real World
Categorizing data based on conditions is common in sales analysis, customer segmentation, and many other fields.
💼 Career
Data scientists often use conditional selection to prepare data for analysis and reporting.
Progress0 / 4 steps
1
Create the sales data array
Create a NumPy array called sales with these exact values: [150, 85, 230, 45, 120].
NumPy
Need a hint?

Use np.array() to create the array with the exact numbers.

2
Define conditions for sales categories
Create a NumPy array called conditions with three conditions for the sales array: sales less than 50, sales between 50 and 150 inclusive, and sales greater than 150. Use NumPy comparison operators to create boolean arrays and combine them into a single array of integers representing the condition index for each sale.
NumPy
Need a hint?

Use np.select() with a list of conditions and corresponding integer choices.

3
Use np.choose() to assign category labels
Create a list called categories with the strings 'Low', 'Medium', and 'High'. Then use np.choose() with the conditions array to create a new array called sales_categories that assigns the correct category label to each sale.
NumPy
Need a hint?

Use np.choose() with the conditions array and the categories list.

4
Print the sales categories
Print the sales_categories array to display the category labels for each sale.
NumPy
Need a hint?

Use print() to display the sales_categories array.