Skip to main content
 首页 » 编程设计

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

2025年05月04日12shangdawei

我想用两个向量(预测的,实际的)对 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 ...总有一些转换要做。我会很感激一些非常简单的东西。

请您参考如下方法:

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