使用python在数组中输出小数“不正确”值

使用python在数组中输出小数“不正确”值,python,arrays,numpy,Python,Arrays,Numpy,我有一个方法可以预测一些数据并将其输出到一个numpy数组,称为Y_predict。然后我有一个名为Y_real的numpy数组,它存储了应该预测的Y的实际值 例如: Y_predict = [1, 0, 2, 1] Y_real = [1, 0, 1, 1] 然后我需要一个名为errRate[]的数组,它将检查Y_predict[I]==Y_real[I]。应注意与Y_real不匹配的任何值。最后,输出应该是正确预测的数量。在上述情况下,这将是0.75,因为Y_predict[2]=2

我有一个方法可以预测一些数据并将其输出到一个numpy数组,称为Y_predict。然后我有一个名为Y_real的numpy数组,它存储了应该预测的Y的实际值

例如:

Y_predict = [1, 0, 2, 1]
Y_real    = [1, 0, 1, 1]
然后我需要一个名为errRate[]的数组,它将检查Y_predict[I]==Y_real[I]。应注意与Y_real不匹配的任何值。最后,输出应该是正确预测的数量。在上述情况下,这将是0.75,因为Y_predict[2]=2和Y_real[2]=1

在numpy或python中是否有快速计算该速率的方法?

给出以下列表:

Y_predict = [1, 0, 2, 1]
Y_real    = [1, 0, 1, 1]
我能想到的最简单的方法是在列表comp中使用zip:

Y_rate         = [int(x == y) for x, y in zip(Y_predict, Y_real)] # 1 if correct, 0 if incorrect
Y_rate_correct = sum(Y_rate) / len(Y_rate)

print( Y_rate_correct ) # this will print 0.75
鉴于这些清单:

Y_predict = [1, 0, 2, 1]
Y_real    = [1, 0, 1, 1]
我能想到的最简单的方法是在列表comp中使用zip:

Y_rate         = [int(x == y) for x, y in zip(Y_predict, Y_real)] # 1 if correct, 0 if incorrect
Y_rate_correct = sum(Y_rate) / len(Y_rate)

print( Y_rate_correct ) # this will print 0.75

因为它们是numpy阵列,所以这相对简单:

>>> p
array([1, 0, 2, 1])
>>> r
array([1, 0, 1, 1])
>>> p == r
array([ True,  True, False,  True], dtype=bool)
>>> (p == r).mean()
0.75

因为它们是numpy阵列,所以这相对简单:

>>> p
array([1, 0, 2, 1])
>>> r
array([1, 0, 1, 1])
>>> p == r
array([ True,  True, False,  True], dtype=bool)
>>> (p == r).mean()
0.75