删除NumPy 2D数组中的一个对象

huangapple go评论56阅读模式
英文:

delete a object in numpy 2d array

问题

I want only the desired [1,2] to be deleted, but this is not done

import numpy as np

arr = np.array([[1,2],[3,4],[5,6],[1,3]])
arr = np.delete(arr, np.where(np.isin(arr, [[1,2]])))

print(arr)

output: array([3, 5, 6, 1, 3])

I also used the setdiff1d method, but it removed all 1 and 2

英文:

I want only the desired [1,2] to be deleted, but this is not done

import numpy as np

arr = np.array([[1,2],[3,4],[5,6],[1,3]])
arr = np.delete(arr, np.where(np.isin(arr, [[1,2]])))

print(arr)

output: array([3, 5, 6, 1, 3])

I also used the setdiff1d method, but it removed all 1 and 2

答案1

得分: 1

这可能有所帮助?

mask = (arr[:,0] == 1) & (arr[:,1] == 2)
# 当第一列 == 1 且第二列 == 2 时
# 反转掩码并选择所有不满足条件的行
print(arr[~mask])

以下是您示例的输出:

>>> import numpy as np
>>> arr = np.array([[1,2],[3,4],[5,6],[1,3]])
>>> mask = (arr[:,0] == 1) & (arr[:,1] == 2)
>>> mask
array([ True, False, False, False])
>>> arr[~mask]
array([[3, 4],
       [5, 6],
       [1, 3]])
>>>
英文:

maybe this helps?

mask = (arr[:,0] == 1) & (arr[:,1] == 2)
# where first column == 1 and 2nd column == 2
# invert the mask and select all the rows where this is not true
print(arr[~mask])

here is the output from your example

>>> import numpy as np
>>> arr = np.array([[1,2],[3,4],[5,6],[1,3]])
>>> mask = (arr[:,0] == 1) & (arr[:,1] == 2)
>>> mask
array([ True, False, False, False])
>>> arr[~mask]
array([[3, 4],
       [5, 6],
       [1, 3]])
>>>

</details>



# 答案2
**得分**: 0

我怀疑你没有花太多时间阅读`np.delete`文档。你没有指定轴。

移除第一行很容易:

```python
np.delete(arr, 0, axis=0)

没有指定轴时,delete首先会将数组扁平化:

np.delete(arr, 0)

通过索引或布尔掩码也很容易移除/选择行:

arr[[1, 2, 3], :]

这基本上就是delete的工作原理;它将“删除”列表转换为“保留”列表。

问题的另一部分是确定要移除的行。也就是将值转换为行索引。将这两个任务分开处理是个好主意。

尝试使用isin

np.isin(arr, [[1, 2]])

这是一个二维布尔数组。应用all

np.isin(arr, [[1, 2]]).all(axis=0)

哎呀,轴错了:

np.isin(arr, [[1, 2]]).all(axis=1)

我们可以使用这个布尔数组来索引行:

arr[np.isin(arr, [[1, 2]]).all(axis=1), :]

要移除一行:

arr[~np.isin(arr, [[1, 2]]).all(axis=1), :]

是的,我们可以使用where来将其转换为索引,但这并不是必需的。

我鼓励你仔细研究并理解每个步骤,包括识别值和移除它们。不要将任何一个步骤视为你不需要理解的“黑盒子”。

英文:

I suspect you didn't spend much time reading the np.delete docs. You didn't specify an axis.

In [96]: arr = np.array([[1,2],[3,4],[5,6],[1,3]])

Removing the first row is easy:

In [97]: np.delete(arr, 0, axis=0)
Out[97]: 
array([[3, 4],
       [5, 6],
       [1, 3]])

Without axis,delete flattens the array first:

In [98]: np.delete(arr, 0, )
Out[98]: array([2, 3, 4, 5, 6, 1, 3])

It's also easy to remove/select rows by indexing, or boolean masking

In [99]: arr[[1,2,3],:]
Out[99]: 
array([[3, 4],
       [5, 6],
       [1, 3]])

That's ultimately what delete does; it translates the "remove" list into a "keep" list.

The other part of your problem is identifing which rows you want to remove. That is, converting values into row indices. It's a good idea to keep the two tasks separate.

Exploring the use of isin

In [100]: np.isin(arr, [[1,2]])
Out[100]: 
array([[ True,  True],
       [False, False],
       [False, False],
       [ True, False]])

This is a 2d boolean array. Applying all:

In [101]: np.isin(arr, [[1,2]]).all(axis=0)
Out[101]: array([False, False])

oops, wrong axis:

In [102]: np.isin(arr, [[1,2]]).all(axis=1)
Out[102]: array([ True, False, False, False])

we can use that boolean array index the rows:

In [103]: arr[np.isin(arr, [[1,2]]).all(axis=1),:]
Out[103]: array([[1, 2]])

and for removing a row:

In [104]: arr[~np.isin(arr, [[1,2]]).all(axis=1),:]
Out[104]: 
array([[3, 4],
       [5, 6],
       [1, 3]])

Yes, we could use where to convert that to an index, but it isn't needed.

I encourage you to examine and understand each piece in the process, both identifying the values, and removing them. Don't treat any as a 'block box' that you don't need to understand.

答案3

得分: 0

以下是翻译好的部分:

所以,对于这个特定的情况,我建议使用一个结构化的视图来处理你的数组,这样可以将它视为一个“对象数组”(结构体):

import numpy as np
arr = np.array([[1,2],[3,4],[5,6],[1,3]])
structured = arr.ravel().view('i8, i8')
structured
array([(1, 2), (3, 4), (5, 6), (1, 3)],
      dtype=[('f0', '<i8'), ('f1', '<i8')])

然后,你可以简单地执行以下操作:

np.isin(structured, np.array([(1,2)], dtype='i8, i8'))
array([ True, False, False, False])
arr[np.isin(structured, np.array([(1,2)], dtype='i8, i8'))]
array([[1, 2]])
arr[~np.isin(structured, np.array([(1,2)], dtype='i8, i8'))]
array([[3, 4],
       [5, 6],
       [1, 3]])

或者等效地使用 np.delete 并指定正确的轴:

np.delete(arr, np.isin(structured, np.array([(1,2)], dtype='i8, i8')), axis=0)
array([[3, 4],
       [5, 6],
       [1, 3]])

值得注意的是,你可以轻松地添加更多要删除的“项目”:

np.delete(arr, np.isin(structured, np.array([(1,2), (5,6)], dtype='i8, i8')), axis=0)
array([[3, 4],
       [1, 3]])

只需确保在使用 np.array 构造函数创建结构化数组时,使用包含 tuplelist,以便它正确理解你希望如何解释它们。

英文:

So, for this particular case, I suggest using a structured view of your array, this allows you to treat it like an "array of objects" (of structs):

&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; arr = np.array([[1,2],[3,4],[5,6],[1,3]])
&gt;&gt;&gt; structured = arr.ravel().view(&quot;i8, i8&quot;)
&gt;&gt;&gt; structured
array([(1, 2), (3, 4), (5, 6), (1, 3)],
      dtype=[(&#39;f0&#39;, &#39;&lt;i8&#39;), (&#39;f1&#39;, &#39;&lt;i8&#39;)])

Then, you can simply do:

&gt;&gt;&gt; np.isin(structured, np.array([(1,2)], dtype=&quot;i8, i8&quot;))
array([ True, False, False, False])
&gt;&gt;&gt; arr[np.isin(structured, np.array([(1,2)], dtype=&quot;i8, i8&quot;))]
array([[1, 2]])
&gt;&gt;&gt; arr[~np.isin(structured, np.array([(1,2)], dtype=&quot;i8, i8&quot;))]
array([[3, 4],
       [5, 6],
       [1, 3]])

Or equivalently using np.delete specifying the correct axis:

&gt;&gt;&gt; np.delete(arr, np.isin(structured, np.array([(1,2)], dtype=&quot;i8, i8&quot;)), axis=0)
array([[3, 4],
       [5, 6],
       [1, 3]])

Important to note, you can add more "items" to delete trivially:

&gt;&gt;&gt; np.delete(arr, np.isin(structured, np.array([(1,2), (5,6)], dtype=&quot;i8, i8&quot;)), axis=0)
array([[3, 4],
       [1, 3]])

Just make sure you use a list with tuple's when you create the structured array using the np.array constructor, so that it understands how you want them interpreted correctly.

huangapple
  • 本文由 发表于 2023年6月6日 05:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定