advanced patterns when using np.where with 3D arrays in NumPy 1.23.0
I tried several approaches but none seem to work. I'm trying to selectively modify elements in a 3D NumPy array using `np.where`, but I'm observing unexpected results. My original array has the shape `(2, 3, 4)` and contains random integers between 0 and 10. I want to replace all values greater than 5 with `-1`. Here's the code I've written: ```python import numpy as np # Create a 3D array array_3d = np.random.randint(0, 11, size=(2, 3, 4)) print('Original array:', array_3d) # Attempt to replace values greater than 5 modified_array = np.where(array_3d > 5, -1, array_3d) print('Modified array:', modified_array) ``` However, when I run the above code, I noticed that the `modified_array` correctly replaces values greater than 5 with `-1`, but the output seems to maintain the shape as `(2, 3, 4)`, which is expected. My confusion arises when I try to assign this modified array back to specific slices of the original array. For example: ```python array_3d[:1, :2, :2] = modified_array[:1, :2, :2] ``` This results in the following behavior: ``` ValueError: could not broadcast input array from shape (1,2,2) into shape (1,2,2) ``` I expected this to work seamlessly since both slices and the modified data have the same shape. What am I missing here? Is there an scenario with how I'm attempting to broadcast the assignment, or is there a better way to achieve my goal? Any insights would be greatly appreciated! Any ideas what could be causing this?