Unexpected results when using np.where with boolean conditions on multi-dimensional arrays
I just started working with I'm getting frustrated with I've looked through the documentation and I'm still confused about I've run into a perplexing scenario while trying to filter a 2D NumPy array using `np.where`..... I have a 2D array representing some data, and I want to extract the indices of all elements that are greater than a certain threshold. Here's the code I wrote: ```python import numpy as np # Sample data array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) threshold = 5 # Using np.where to find indices greater than the threshold indices = np.where(array > threshold) print(indices) ``` The output I get is a tuple of arrays representing the indices: ``` (array([1, 1, 2, 2, 2]), array([2, 1, 0, 1, 2])) ``` While this seems correct, what I'm struggling with is how to extract the actual values from the original array at these indices. I tried using something like `array[indices]`, but instead of getting just the values greater than 5, I receive `array([6, 5, 7, 8, 9])` which seems wrong since I'm not referencing the indices correctly. After some research, I attempted to convert the indices to a list and retrieve values accordingly: ```python value_indices = list(zip(*indices)) values = [array[idx] for idx in value_indices] print(values) ``` However, this returns the values in a nested list format which is not what I want. I expected to get a flat array of the values greater than 5, like `[6, 7, 8, 9]`. Is there a more efficient or appropriate way to retrieve only the values that meet the condition in a flat structure? I am using NumPy version 1.21.2, and I would appreciate any guidance on best practices for these kinds of operations. For context: I'm using Python on macOS. Has anyone else encountered this? Am I missing something obvious? The project is a web app built with Python. What am I doing wrong? The project is a service built with Python.