简体   繁体   English

如何计算两个 numpy arrays 之间匹配零元素的数量?

[英]How can I count the number of matching zero elements between two numpy arrays?

I have two numpy arrays of equal size.我有两个大小相同的numpy arrays。 They contain the values 1 , 0 , and -1 .它们包含值10-1 I can count the number of matching ones and negative ones, but I'm not sure how to count the matching elements that have the same index and value of zero.我可以计算匹配元素和负元素的数量,但我不确定如何计算具有相同索引和零值的匹配元素。

I'm a little confused on how to proceed here.我对如何在这里进行有点困惑。

Here is some code:这是一些代码:

print(actual_direction.shape)
print(predicted_direction.shape)
act = actual_direction
pre = predicted_direction
part1 = act[pre == 1]
part2 = part1[part1 == 1]
result1 = part2.sum()
part3 = act[pre == -1]
part4 = part3[part3 == -1]
result2 = part4.sum() * -1
non_zeros = result1 + result2
zeros = len(act) - non_zeros
print(f'zeros : {zeros}\n')
print(f'non_zeros : {non_zeros}\n')
final_result = non_zeros + zeros
print(f'result1 : {result1}\n')
print(f'result2 : {result2}\n')
print(f'final_result : {final_result}\n')

Here is the printout:这是打印输出:

(11279,)
(11279,)
zeros : 5745.0
non_zeros : 5534.0
result1 : 2217.0
result2 : 3317.0
final_result : 11279.0

So what I've done here is simply subtract the summation of the ones and negative ones from the total length of the array.所以我在这里所做的只是从数组的总长度中减去一个和负数的总和。 I can't assume that the difference (zeros: 5745) contains ALL matching elements that contain zeros can I?我不能假设差异(零:5745)包含所有包含零的匹配元素,我可以吗?

You could try this:你可以试试这个:

import numpy as np

a=np.array([1,0,0,1,-1,-1,0,0])
b=np.array([1,0,0,1,-1,-1,0,1])
summ = np.sum((a==0) & (b==0))
print(summ)

Output: Output:

3

You can use numpy.ravel() to flatten out the array, then use zip() to compare each element side by side:您可以使用numpy.ravel()来展平数组,然后使用zip()并排比较每个元素:

import numpy as np

ar1 = np.array([[1, 0, 0],
                [0, 1, 1],
                [0, 1, 0]])

ar2 = np.array([[0, 0, 0],
                [1, 0, 1],
                [0, 1, 0]])

count = 0
for e1, e2 in zip(ar1.ravel(), ar2.ravel()):
    if e1 == e2:
        count += 1

print(count)

Output: Output:

6

You can also do this to list all the matches found, as well as print out the amount:您也可以这样做来列出找到的所有匹配项,并打印出金额:

dup = [e1 for e1, e2 in zip(ar1.ravel(), ar2.ravel()) if e1 == e2]
print(dup)
print(len(dup))

Output: Output:

[0, 0, 1, 0, 1, 0]
6

You have two arrays and want to count the positions where both of these are 0, right?你有两个 arrays 并且想计算这两个都是 0 的位置,对吧?

You can check where the array meets your required condition (a == 0) , and then use the 'and' operator & to check where both arrays meet your requirement:您可以检查数组满足您所需条件的位置(a == 0) ,然后使用“和”运算符&检查 arrays 满足您的要求的位置:

import numpy as np
a = np.array([1, 0, -1, 0, -1, 1, 1, 1, 1])
b = np.array([1, 0, -1, 1, 0, -1, 1, 0, 1])

both_zero = (a == 0) & (b == 0)  # [False,  True, False, False, False, False]

both_zero.sum()  # 1

In your updated question you appear to be interested in the similarities and differences between actual values and predictions.在您更新的问题中,您似乎对实际值和预测之间的异同感兴趣。 For this, a confusion matrix is ideally suited.为此, 混淆矩阵非常适合。

from sklearn.metrics import confusion_matrix
confusion_matrix(a, b, labels=[-1, 0, 1])

will give you a confusion matrix as output telling you how many -1s were predicted as -1, 0 and 1, and the same for 0 and +1:会给你一个混淆矩阵,如 output 告诉你有多少 -1 被预测为 -1、0 和 1,0 和 +1 也一样:

[[1 1 0]   # -1s predicted as -1, 0 and 1
 [0 1 1]   # 0s predicted as -1, 0 and 1
 [1 1 3]]  # 1s predicted as -1, 0 and 1

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

相关问题 如何计算两个 arrays 之间的匹配行数? - How do I count the number of matching rows between two arrays? 如何从两个 arrays 中找到匹配的元素和索引? - How can I find matching elements and indices from two arrays? 从两个 numpy arrays 中移除匹配元素 - Removing matching elements from two numpy arrays 如何在numpy中获取两个索引数组之间的矩阵元素? - How in numpy get elements of matrix between two indices arrays? 在 Tensorflow 中,如何更改两个非零元素之间的元素值? - In Tensorflow, how can I change value of elements between two non-zero elements? 如何提取两个元素之间的数字? (网页抓取) - How can i extract a number between two elements? (webscraping) 计算两个数据帧之间的匹配元素 - count the matching elements between two dataframes 计算两个元组列表之间匹配的第二个元素的数量的更快方法? -Python - Faster way to count number of matching 2nd elements between two list of tuples? - Python 如何计算python中一定范围内不为零的行数? - How can I count the number of rows that are not zero in a certain range in python? 如何在numpy和R之间传递大型数组? - How can I pass large arrays between numpy and R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM