如何从形状不均匀的数组中删除一个元素?
How to remove an element from an unevenly shaped array?
labels_1 = np.array([[-100,32,34,25,2,35,2,5,-100,-100],[-100,35,2,5,-100,-100]])
pred_1 = np.array([[8,32,3,25,2,3,2,5,8],[8,3,2,5,8]])
我想去掉labels_1中的-100s,从pred_1中得到相应匹配的索引元素。
例如输出应该是
labels_1 = np.array([[32,34,25,2,35,2,5],[35,2,5]])
pred_1 = np.array([[32,3,25,2,3,2,5],[3,2,5]])
我尝试使用 np.where(labels_1!=-100)
但它仅适用于具有相同长度列表的数组,但正如您所看到的 labels_1 中的数组具有不同的长度,这是一个问题。
在你的 numpy 数组中有不均匀大小的列表,你违背了 numpy 数组的目的,所以你可以为此使用 numpy 解决方案。
不过,您可以使用列表理解来完成此任务:
labels_1 = np.array([[x for x in y if x != -100] for y in labels_1])
pred_1 = np.array([[x for x in y if x != -100] for y in pred_1])
输出:
>>> labels_1
array([list([32, 34, 25, 2, 35, 2, 5]), list([35, 2, 5])], dtype=object)
>>> pred_1
array([list([8, 32, 3, 25, 2, 3, 2, 5, 8]), list([8, 3, 2, 5, 8])], dtype=object)
我相信你要找的是:
pred_1 = [[b for a, b in zip(la, lb) if a != -100] for la, lb in zip(labels_1, pred_1)]
labels_1 = [[a for a in la if a != -100] for la in labels_1]
结果:
>>> labels_1
[[32, 34, 25, 2, 35, 2, 5], [35, 2, 5]]
>>> pred_1
[[32, 3, 25, 2, 3, 2, 5], [3, 2, 5]]
如评论中所述,您不能在 numpy
中表示参差不齐的数组。如果你按照你写的那样尝试,你应该看到一个响亮的 VisibleDeprecationWarning
,表明你的结果将是一个列表列表,如果你真的打算这样做,你应该指定 'dtype=object'.. . 你可以研究掩码数组,如果它更符合你的需要,否则你最好使用简单的列表(列表,在这种情况下)。
编辑
虽然有点复杂,但下面的内容可能更具有普遍性(任何涉及 a
和 b
之一或两者的条件,其中 a
和 b
是一个数组和另一个数组的元素):
labels_1, pred_1 = map(list, zip(*[
list(map(list, zip(*[
(a, b) for a, b in zip(la, lb)
if a != -100 # you can adapt this condition at will
])))
for la, lb in zip(labels_1, pred_1)
]))
这样比较灵活。例如,当一个比另一个大时,它会让你在两个数组中都有 select 个元素,或者这对元素的任何条件。
labels_1 = np.array([[-100,32,34,25,2,35,2,5,-100,-100],[-100,35,2,5,-100,-100]])
pred_1 = np.array([[8,32,3,25,2,3,2,5,8],[8,3,2,5,8]])
我想去掉labels_1中的-100s,从pred_1中得到相应匹配的索引元素。
例如输出应该是
labels_1 = np.array([[32,34,25,2,35,2,5],[35,2,5]])
pred_1 = np.array([[32,3,25,2,3,2,5],[3,2,5]])
我尝试使用 np.where(labels_1!=-100)
但它仅适用于具有相同长度列表的数组,但正如您所看到的 labels_1 中的数组具有不同的长度,这是一个问题。
在你的 numpy 数组中有不均匀大小的列表,你违背了 numpy 数组的目的,所以你可以为此使用 numpy 解决方案。
不过,您可以使用列表理解来完成此任务:
labels_1 = np.array([[x for x in y if x != -100] for y in labels_1])
pred_1 = np.array([[x for x in y if x != -100] for y in pred_1])
输出:
>>> labels_1
array([list([32, 34, 25, 2, 35, 2, 5]), list([35, 2, 5])], dtype=object)
>>> pred_1
array([list([8, 32, 3, 25, 2, 3, 2, 5, 8]), list([8, 3, 2, 5, 8])], dtype=object)
我相信你要找的是:
pred_1 = [[b for a, b in zip(la, lb) if a != -100] for la, lb in zip(labels_1, pred_1)]
labels_1 = [[a for a in la if a != -100] for la in labels_1]
结果:
>>> labels_1
[[32, 34, 25, 2, 35, 2, 5], [35, 2, 5]]
>>> pred_1
[[32, 3, 25, 2, 3, 2, 5], [3, 2, 5]]
如评论中所述,您不能在 numpy
中表示参差不齐的数组。如果你按照你写的那样尝试,你应该看到一个响亮的 VisibleDeprecationWarning
,表明你的结果将是一个列表列表,如果你真的打算这样做,你应该指定 'dtype=object'.. . 你可以研究掩码数组,如果它更符合你的需要,否则你最好使用简单的列表(列表,在这种情况下)。
编辑
虽然有点复杂,但下面的内容可能更具有普遍性(任何涉及 a
和 b
之一或两者的条件,其中 a
和 b
是一个数组和另一个数组的元素):
labels_1, pred_1 = map(list, zip(*[
list(map(list, zip(*[
(a, b) for a, b in zip(la, lb)
if a != -100 # you can adapt this condition at will
])))
for la, lb in zip(labels_1, pred_1)
]))
这样比较灵活。例如,当一个比另一个大时,它会让你在两个数组中都有 select 个元素,或者这对元素的任何条件。