[0, 1, 1, ...]我正在为某些给定的真值标签(例如)和概率(例如)编写自己的准确性函数(正确预测数/总预测数)[[0.8, 0.2], [0.3, 0.7], [0.1, 0.9] ...]。我不想使用诸如 sklearn 之类的库函数accuracy_score()。我使用 for 循环创建了这个版本:def compute_accuracy(truth_labels, probs):    total = 0    total_correct = 0    for index, prob in enumerate(probs):        predicted_label = 0 if prob[0] > 0.5 else 1        if predicted_label == truth_labels[index]:            total_correct += 1        total += 1    if total:        return total_correct / total    else:        return -1我现在希望通过矢量化来提高效率。我的目标是检查概率 > 0.5 是否与真值标签匹配:import numpy as npdef compute_accuracy(truth_labels, probs):    return ((np.array(probs[:][value_of_truth_labels_at_same_index]) > 0.5).astype(int) == np.array(truth_labels)).mean()此时我不知道如何退出value_of_truth_labels_at_same_index而不返回 for 循环。
                    
                    
                1 回答
 
                    
                    
                            胡说叔叔
                            
                                
                            
                        
                        
                                                
                    TA贡献1804条经验 获得超8个赞
import numpy as np
N = 10
X = np.random.randint(0,2,(N,))
p = np.random.random((N,2))
acc = np.mean(np.argmax(p, axis=1) == X)*100
print(f'Accuracy: {acc}%')
添加回答
举报
0/150
	提交
		取消
	