i am trying delete a array from a array with numpy:
points = np.array([(25, 0), (0, 25), (0, 75), (25, 100), (75, 100), (100, 75), (100, 25), (75, 0)])
front_yard = np.array([(25, 0), (75, 0)])
when i try
new_coords = np.delete(points, front_yard)
it gaves:
IndexError: index 25 is out of bounds for axis 0 with size 16
or
new_coords = np.delete(coords, np.where(coords == front_yard))
it gaves (doesnt work:
[ 25 0 0 25 0 75 25 100 75 100 100 75 100 25 75 0]
what should i do?
CodePudding user response:
You can use the following code:
import numpy as np
points = np.array([(25, 0), (0, 25), (0, 75), (25, 100), (75, 100), (100, 75), (100, 25), (75, 0)])
front_yard = np.array([(25, 0), (75, 0)])
new_coords = np.setdiff1d(points, front_yard)
Your issue was that np.delete() function expects the second argument to be the indices of the elements to be deleted, rather than the elements themselves.
You can use np.setdiff1d() to get the difference of two arrays, it returns the unique values in array1 that are not in array2
new_coords = np.setdiff1d(points,front_yard)
CodePudding user response:
Piggybacking on this answer to a similar question, you can use broadcasting to generate a boolean array, and then use it as a filter:
points = np.array([(25, 0), (0, 25), (0, 75), (25, 100), (75, 100), (100, 75), (100, 25), (75, 0)])
front_yard = np.array([(25, 0), (75, 0)])
new_coords = points[~(points==front_yard[:,None]).all(2).any(0)]
new_coords
will be:
array([[ 0, 25],
[ 0, 75],
[ 25, 100],
[ 75, 100],
[100, 75],
[100, 25]])