我有这样的多个类:
predicted = [1 0 2 1 1 0 1 2 1 2 2 0 0 0 0 2 2 1 1 1 0 1 0 1 2 1 1 2 0 0]
actual = [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]
我想找到每个类(0,1,2(的精度
这是我的代码:
TP_0 = 0
TP_1 = 0
TP_2 = 0
FP_0 = 0
FP_1 = 0
FP_2 = 0
for i in range(len(y_pred)):
if y_pred[i] == y_test[i] :
if y_pred[i] == 0:
TP_0 += 1
elif y_pred[i] == 1:
TP_1 += 1
else:
TP_2 += 1
else:
if y_pred[i] == 0:
FP_0 += 1
elif y_pred[i] == 1:
FP_1 += 1
else:
FP_2 += 1
precision_0 = TP_0/(TP_0+FP_0)
precision_1 = TP_1/(TP_1+FP_1)
precision_2 = TP_2/(TP_2+FP_2)
如果我之前知道类和数据的数量,它就会起作用。但现在,无论我是否认识他们,我都想让它发挥作用,就像我有更多的课程一样。
如何减少代码或使其动态?
注意:我不喜欢用图书馆来完成它。
你可以试试这个:
def precision(y_test, y_pred):
# to count false-pos and true-pos
classes = sorted(list(set(y_test + y_pred)))
tp = {cls: 0 for cls in classes}
fp = {cls: 0 for cls in classes}
# count tp and fp
for i in range(len(y_pred)):
if y_pred[i] == y_test[i]:
tp[y_test[i]] += 1
else:
fp[y_test[i]] += 1
# calculate prec for every class
precision = dict()
for cls in classes:
try:
precision[cls] = tp[cls] / (tp[cls] + fp[cls])
except ZeroDivisionError:
precision[cls] = 0.0
return precision
predicted = [0, 1, 2, 3, 0, 1, 4]
actual = [0, 1, 2, 0, 1, 2, 3]
print(precision(actual, predicted))
输出:
{0: 0.5, 1: 0.5, 2: 0.5, 3: 0.0, 4: 0.0}
您可以得到具有键类和值精度的字典。
import numpy as np
# Convert to arrays
y_pred = np.array(y_pred)
y_test = np.array(y_test)
def get_precision(pred, truth, num_classes):
precision_by_class = []
match = (pred == truth) # Binary array indicating whether each prediction is true
for i_class in range(num_classes): # Iterate over classes
# match[pred == i_class].sum() -> number of correct predictions of specific class
# (pred == i_class).sum() -> number of times specific class was predicted
out.append(match[pred == i_class].sum() / (pred == i_class).sum())
accuracy = match.mean() # Total accuracy
return precision_by_class, accuracy