亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何矢量化準確度度量計算?

如何矢量化準確度度量計算?

月關寶盒 2024-01-16 15:25:02
[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}%')


查看完整回答
反對 回復 2024-01-16
  • 1 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號