I’m learning a data preprocessing course. I got a task in which I have to invert array elements in place. The teacher told me it has many approaches to do. I’m able to write one approach. Let me show you below:
#Loading library
import numpy as np
# Create an example 1D array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Using indices and replacing it with invert values
arr[2:5] = -1 * arr[2:5]
print(arr)
Output: [ 1 2 -3 -4 -5 6 7 8 9]
In this approach, I used the concept of Indexing
and broadcasting
.
Can someone provide me with more techniques for achieving the same goal?