For train per-class accuracy: implement below on training dataset - after (and/or before) training on the dataset.
For raw per-class
validation accuracy:
def per_class_accuracy(y_preds,y_true,class_labels):
return [np.mean([
(y_true[pred_idx] == np.round(y_pred)) for pred_idx, y_pred in enumerate(y_preds)
if y_true[pred_idx] == int(class_label)
]) for class_label in class_labels]
def update_val_history():
[val_history[class_label].append(np.mean( np.asarray(temp_history).T[class_idx] )
) for class_idx, class_label in enumerate(class_labels)]
Example:
class_labels = ['0','1','2','3']
val_history = {class_label:[] for class_label in class_labels}
y_true = np.asarray([0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3])
y_preds1 = np.asarray([0,3,3,3, 1,1,0,0, 2,2,2,0, 3,3,3,3])
y_preds2 = np.asarray([0,0,3,3, 0,1,0,0, 2,2,2,2, 0,0,0,0])
y_preds1 = model.predict(x1)
temp_hist.append(per_class_accuracy(y_preds1,y_true,class_labels))
update_val_history()
y_preds2 = model.predict(x2)
temp_hist.append(per_class_accuracy(y_preds2,y_true,class_labels))
update_val_history()
print(val_history)
>>{
'0': [0.25, 0.50],
'1': [0.50, 0.25],
'2': [0.75, 1.00],
'3': [1.00, 0.00]
}