【问题标题】:How can I count the number of matching zero elements between two numpy arrays?如何计算两个 numpy 数组之间匹配的零元素的数量?
【发布时间】:2020-07-06 04:13:10
【问题描述】:

我有两个大小相等的 numpy 数组。它们包含值10-1。我可以计算匹配和负数的数量,但我不确定如何计算具有相同索引和值为零的匹配元素。

我有点困惑如何在这里继续。

这里有一些代码:

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')

这是打印输出:

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

所以我在这里所做的只是从数组的总长度中减去一个和负数的总和。我不能假设差异(零:5745)包含所有包含零的匹配元素吗?

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    你可以试试这个:

    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)
    

    输出:

    3
    

    【讨论】:

    • 你把(predicted_direction==0) & (actual_direction==0)放在np.sum()里面是吗?我的意思是,np.sum((predicted_direction==0) & (actual_direction==0))...print((predicted_direction==0) & (actual_direction==0)) 得到什么输出?
    • 嘿@brohjoe 我不确定你为什么要得到一个数组...你能评论print((predicted_direction==0) & (actual_direction==0))的输出吗?
    • 不要打印总和,只打印列表,我的意思是print((predicted_direction==0) & (actual_direction==0)) 看看输出是什么...
    • 不客气。是的,我在聊天中写了那个可能性,我猜这可能正在发生。如果您也接受答案,我将不胜感激。 :)
    【解决方案2】:

    您可以使用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)
    

    输出:

    6
    

    您也可以这样做来列出找到的所有匹配项,并打印出金额:

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

    输出:

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

    【讨论】:

      【解决方案3】:

      你有两个数组,想计算这两个数组都为0的位置,对吧?

      您可以检查数组在哪里满足您要求的条件(a == 0),然后使用“与”运算符& 检查两个数组在哪里满足您的要求:

      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
      

      在您更新的问题中,您似乎对实际值和预测之间的异同感兴趣。为此,confusion matrix 非常适合。

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

      会给你一个混淆矩阵作为输出,告诉你有多少个 -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
      

      【讨论】:

      • 嘿,你能解释一下这和https://stackoverflow.com/a/62745467/13676202有什么不同吗?
      • @MrNobody33 不是,看起来你的速度更快,请支持我!
      • @brohjoe 我假设您的数组是 predict_direction 和 predict_direction,是吗?您尝试过 np.sum((predicted_direction==0) & (actual_direction==0)) 吗?
      • 让我试试
      • @brohjoe 是的,有可能,在代码片段中,您评论说您忘记了一对括号。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      相关资源
      最近更新 更多