I have two numpy-arrays:
p_a_colors=np.array([[0,0,0],
[0,2,0],
[119,103,82],
[122,122,122],
[122,122,122],
[3,2,4]])
p_rem = np.array([[119,103,82],
[122,122,122]])
I want to delete all the columns from p_a_colors that are in p_rem, so I get:
p_r_colors=np.array([[0,0,0],
[0,2,0],
[3,2,4]])
I think, something should work like
p_r_colors= np.delete(p_a_colors, np.where(np.all(p_a_colors==p_rem, axis=0)),0)
but I just don't get the axis or [:] right.
I know, that
p_r_colors=copy.deepcopy(p_a_colors)
for i in range(len(p_rem)):
p_r_colors= np.delete(p_r_colors, np.where(np.all(p_r_colors==p_rem[i], axis=-1)),0)
would work, but I am trying to avoid (python)loops, because I also want the performance right.
You can use numpy. squeeze() to remove all dimensions of size 1 from the NumPy array ndarray . squeeze() is also provided as a method of ndarray .
np. delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on given index conditions and axis specified, the parameter ndarray is the array on which the manipulation will happen, the index is the particular rows based on conditions to be deleted, axis=0 for removing rows in our case.
The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])
This is how I would do it:
dtype = np.dtype((np.void, (p_a_colors.shape[1] *
p_a_colors.dtype.itemsize)))
mask = np.in1d(p_a_colors.view(dtype), p_rem.view(dtype))
p_r_colors = p_a_colors[~mask]
>>> p_r_colors
array([[0, 0, 0],
[0, 2, 0],
[3, 2, 4]])
You need to do the void dtype thing so that numpy compares rows as a whole. After that using the built-in set routines seems like the obvious way to go.
It's ugly, but
tmp = reduce(lambda x, y: x | np.all(p_a_colors == y, axis=-1), p_rem, np.zeros(p_a_colors.shape[:1], dtype=np.bool))
indices = np.where(tmp)[0]
np.delete(p_a_colors, indices, axis=0)
(edit: corrected)
>>> tmp = reduce(lambda x, y: x | np.all(p_a_colors == y, axis=-1), p_rem, np.zeros(p_a_colors.shape[:1], dtype=np.bool))
>>>
>>> indices = np.where(tmp)[0]
>>>
>>> np.delete(p_a_colors, indices, axis=0)
array([[0, 0, 0],
[0, 2, 0],
[3, 2, 4]])
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With