简体   繁体   English

如何比较 2 arrays 并获得相互元素的数量?

[英]How to compare 2 arrays and get number of mutual elements?

I have 2 arrays like:我有 2 个 arrays 像:

array_A: [0 0 0 1 0 1 0 1 1 1] which is predicted and array_A: [0 0 0 1 0 1 0 1 1 1]预测和

array_B: [1 0 1 1 0 0 0 1 1 0] is the true labels, thus Array B has 2 classes, 0 and 1. array_B: [1 0 1 1 0 0 0 1 1 0]是真正的标签,因此 Array B 有 2 个类,0 和 1。

I would like to compare Array A to Array B but for only once class, let's say 0 for example where the prediction is correct.我想将数组 A 与数组 B 进行比较,但仅比较一次 class,例如预测正确的位置为 0。 Visually it gives us 3. I tried with like with np.where(array_A==array_B],1,0) but it doesn't work.从视觉上看,它给了我们 3。我尝试了与np.where(array_A==array_B],1,0)类似的方法,但它不起作用。 How should I do this using python?我应该如何使用 python 做到这一点?

You can build a list using a list comprehension over the zip of the two input lists.您可以使用对两个输入列表的zip的列表推导来构建列表。

Eg例如

a = [0, 0, 0, 1, 0, 1, 0, 1, 1, 1]
b = [1, 0, 1, 1, 0, 0, 0, 1, 1, 0]
[x for x, y in zip(a, b) if x == y == 0]

Elements from the two lists are only included in this new list if the corresponding elements in each input list are equal, and also are 0 .仅当每个输入列表中的相应元素相等且为0时,这两个列表中的元素才会包含在此新列表中。

The length of the list is the value you want.列表的长度是您想要的值。

len([x for x, y in zip(a, b) if x == y == 0]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM