np.where behaving unexpectedly with multi-dimensional arrays in NumPy 1.23
Could someone explain I'm trying to configure I'm stuck on something that should probably be simple. I've been trying to use `np.where` to conditionally select elements from a multi-dimensional NumPy array, but I'm seeing unexpected results. I'm working with a 2D array and I want to replace values that meet a certain condition with a specified value. Here's what my code looks like: ```python import numpy as np # Create a 2D array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Attempt to replace values less than 5 with -1 result = np.where(arr < 5, -1, arr) print(result) ``` When I run this, I expect the output to replace all values less than 5 with -1, yielding: ``` [[-1 -1 -1] [ 4 5 6] [ 7 8 9]] ``` However, I'm getting: ``` [[-1 -1 -1] [ 4 5 6] [ 7 8 9]] ``` which looks correct, but the issue arises when I try to apply this logic to an array of a different shape. When I use the same logic on a (3, 3) array but provide a single value for the `np.where` function: ```python result = np.where(arr < 5, -1) ``` I get a warning and an unexpected output: ``` Warning: `where` only works with boolean conditions. Returning an array of shape (n,) instead of (m, n). ``` This makes me think that `np.where` is not handling the shape correctly when I provide only two arguments. I want to keep my original array as the third argument and still replace the values correctly. What am I missing here? Is there a better way to handle selection and replacement for multi-dimensional arrays while avoiding these shape issues? Any help would be appreciated! Thanks in advance! My development environment is macOS. Any ideas what could be causing this? My development environment is Windows. What am I doing wrong? Has anyone else encountered this? I'm working on a web app that needs to handle this. Thanks for any help you can provide!