Combining fancy and slice indexing
📖 Scenario: Imagine you have a small dataset of daily temperatures recorded over a week for different cities. You want to select specific days and cities to analyze the temperature trends.
🎯 Goal: You will create a NumPy array with temperature data, set up an index list for cities, use fancy and slice indexing together to select data, and finally print the selected temperatures.
📋 What You'll Learn
Create a 2D NumPy array called
temps with shape (7, 4) representing 7 days and 4 cities.Create a list called
city_indices containing the city indices 0 and 2.Use fancy indexing with
city_indices and slice indexing for days 2 to 5 (inclusive start, exclusive end) on temps.Print the resulting selected temperatures.
💡 Why This Matters
🌍 Real World
Selecting specific rows and columns from data is common in data science when analyzing subsets of data, like temperatures for certain days and cities.
💼 Career
Data scientists often use fancy and slice indexing in NumPy to efficiently manipulate and analyze large datasets.
Progress0 / 4 steps