使用python中的二进制向量评估预测

时间:2014-03-03 17:51:23

标签: python numpy

我想采用两个向量(预测,实际)并对Python中的预测进行简单评估:(两个向量都是ndarrays)

prediction = [ 1 1 1 0 0 1 ]
actual     = [ 1 0 1 0 1 0 ]

score = 1 + 0 + 1 + 1 + 0 + 0 / 6 = 3/6 = 50% 

我尝试了&&个运算符和numpy.mul ...总会有一些变换要做。我很欣赏一些非常简单的事情。

5 个答案:

答案 0 :(得分:7)

>>> import numpy as np
>>> prediction = np.array([1,1,1,0,0,1])
>>> actual     = np.array([1,0,1,0,1,0])
>>> np.sum(prediction == actual, dtype=float) / len(prediction)
0.5
>>> np.mean(prediction == actual)
0.5

答案 1 :(得分:1)

import itertools
score = 100.0 * sum(1 for x,y in itertools.izip(prediction, actual) if x == y) / len(actual)

输出:

50.0

答案 2 :(得分:0)

sum([prediction[i]==actual[i] for i in range(len(prediction))])/len(prediction)

答案 3 :(得分:0)

您也可以使用标准的python运算符实现此目的:

>>> prediction = [ 1, 1, 1, 0, 0, 1 ];
>>> actual     = [ 1, 0, 1, 0, 1, 0 ];
>>> 100.0 * sum([not p ^ a for p, a in zip(prediction, actual)])/len(prediction)

答案 4 :(得分:0)

predicted = [1, 0, 1, 1, 0, 0, 1]
actual    = [1, 1, 0, 1, 0, 0, 0]
assert len(predicted) == len(actual)
score = sum(map(lambda x: 1. if x[0] == x[1] else 0., zip(predicted, actual))) / len(predicted)
print(score) # <0,1> where 0=complete mismatch and 1=complete match