Unexpected behavior of np.where with multi-dimensional arrays in NumPy 1.25
I've hit a wall trying to I'm encountering an issue with the `np.where` function when attempting to conditionally select elements from a multi-dimensional NumPy array... I'm using NumPy version 1.25 and I have a 3D array that I'm trying to filter based on a condition. Hereโs a simplified version of my code: ```python import numpy as np # Create a 3D array array = np.random.rand(4, 4, 4) # Define a condition: select elements > 0.5 condition = array > 0.5 # Use np.where to get the indices of elements satisfying the condition indices = np.where(condition) # Attempt to retrieve the elements selected_elements = array[indices] print(selected_elements) ``` The `np.where` function returns a tuple of arrays (one for each dimension), and when I use these indices to retrieve the elements from the original array, I expect to get a 1D array of values. However, I noticed that the output is not what I anticipated. Instead of a flat array, I'm getting an array that retains the multi-dimensional structure: ```python array([[0.862, 0.734], [0.563, ...], ...]) ``` Iโve also tried flattening the indices using `np.ravel`, but it didnโt yield the correct result either. My expectation is to have all selected elements in a single flattened array. Is there a straightforward way to achieve this? Am I misusing `np.where`? Any insights would be helpful. Additionally, I verified that the condition is indeed correct by printing the boolean mask, and I also confirmed that elements exist that satisfy the condition. Still, the output remains unexpected. Thanks in advance for your assistance! I'm on Windows 10 using the latest version of Python. Has anyone dealt with something similar? What's the correct way to implement this?