🎯 Advanced Indexing

Advanced indexing lets you select arbitrary elements, reorder data, and create complex patterns using lists and arrays of indices.

import numpy as np

# Advanced indexing overview
data = np.array([[10, 20, 30, 40],
                 [50, 60, 70, 80],
                 [90, 100, 110, 120]])

# Select specific rows
selected_rows = data[[0, 2]]  # Rows 0 and 2
print(f"Selected rows: \n{selected_rows}")

# Select specific elements
elements = data[[1, 2], [0, 1]]  # (1,0) and (2,1)
print(f"Selected elements: {elements}")

🎪 Fancy Row Selection

Use lists to select specific rows in any order.

import numpy as np

grades = np.array([[85, 92, 78, 88],   # Alice
                   [79, 85, 91, 82],   # Bob
                   [94, 89, 96, 93],   # Carol
                   [72, 78, 74, 76]])  # David

# Select specific students
top_students = grades[[0, 2]]  # Alice and Carol
print(f"Top students: \n{top_students}")

# Reorder rows
new_order = grades[[2, 0, 1]]  # Carol, Alice, Bob
print(f"New order: \n{new_order}")

🎪 Fancy Column Selection

Select specific columns using lists.

import numpy as np

sales = np.array([[100, 120, 80, 150],   # Week 1
                  [110, 130, 90, 160],   # Week 2
                  [105, 125, 85, 155]])  # Week 3

# Select specific days
key_days = sales[:, [1, 3]]  # Tuesday and Thursday
print(f"Tue & Thu sales: \n{key_days}")

# Rearrange columns
priority = sales[:, [3, 0, 1, 2]]  # Thursday first
print(f"Thu priority: \n{priority}")

🎭 Element Selection

Select specific elements by providing row and column indices.

Diagonal Patterns

import numpy as np

matrix = np.array([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12],
                   [13, 14, 15, 16]])

# Main diagonal
main_diag = matrix[[0, 1, 2, 3], [0, 1, 2, 3]]
print(f"Main diagonal: {main_diag}")

# Anti-diagonal
anti_diag = matrix[[0, 1, 2, 3], [3, 2, 1, 0]]
print(f"Anti-diagonal: {anti_diag}")

Custom Patterns

import numpy as np

matrix = np.array([[1, 2, 3, 4],
                   [5, 6, 7, 8],
                   [9, 10, 11, 12],
                   [13, 14, 15, 16]])

# Corner elements
corners = matrix[[0, 0, 3, 3], [0, 3, 0, 3]]
print(f"Four corners: {corners}")

# Custom pattern
pattern = matrix[[0, 1, 2], [1, 2, 3]]
print(f"Custom pattern: {pattern}")

🔢 Maximum/Minimum Selection

Find and select maximum or minimum elements from each row or column.

Row-wise Max/Min

import numpy as np

scores = np.array([[85, 92, 78, 88],   # Alice
                   [79, 85, 91, 82],   # Bob
                   [94, 89, 96, 93]])  # Carol

# Find max position for each student
max_pos = scores.argmax(axis=1)
students = np.arange(len(scores))

# Select max scores
max_scores = scores[students, max_pos]
print(f"Best scores: {max_scores}")
print(f"Best subject indices: {max_pos}")

Column-wise Max/Min

import numpy as np

scores = np.array([[85, 92, 78, 88],
                   [79, 85, 91, 82],
                   [94, 89, 96, 93]])

# Find min position for each subject
min_pos = scores.argmin(axis=0)
subjects = np.arange(scores.shape[1])

# Select min scores
min_scores = scores[min_pos, subjects]
print(f"Lowest in each subject: {min_scores}")
print(f"Struggling student indices: {min_pos}")

🎨 Combining Techniques

Mix fancy indexing with other methods for complex selections.

import numpy as np

data = np.array([[10, 20, 30, 40, 50],
                 [60, 70, 80, 90, 100],
                 [110, 120, 130, 140, 150]])

# Fancy rows + column slicing
subset = data[[0, 2], 1:4]  # Rows 0,2 and columns 1-3
print(f"Combined selection: \n{subset}")

# Use np.ix_ for clean combinations
rows = [0, 2]
cols = [1, 3, 4]
clean_subset = data[np.ix_(rows, cols)]
print(f"Clean selection: \n{clean_subset}")

🧠 Practice Exercise

import numpy as np

# Monthly sales data: 6 stores x 4 months
sales = np.array([[120, 135, 145, 160],  # Store A
                  [98, 112, 125, 140],   # Store B  
                  [156, 167, 175, 185],  # Store C
                  [134, 145, 155, 170],  # Store D
                  [178, 189, 195, 205],  # Store E
                  [145, 156, 165, 180]]) # Store F

# Select top 3 performing stores (indices 2, 4, 5)
top_stores = sales[[2, 4, 5]]
print(f"Top 3 stores: \n{top_stores}")

# Get Q4 performance for all stores
q4_sales = sales[:, -1]
print(f"Q4 sales: {q4_sales}")

# Find best month for each store
best_months = sales.argmax(axis=1)
store_indices = np.arange(len(sales))
peak_sales = sales[store_indices, best_months]
print(f"Peak sales per store: {peak_sales}")

🎯 Key Takeaways

🚀 What's Next?

Master advanced indexing! Now learn boolean indexing for conditional data selection.

Continue to: Boolean Indexing

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent