Defining Performance Metrics
One of the most important things in machine learning is to define a proper performance metrics. For better machine learning performance, and also for the sake of easier interpretation on the result.
F1 score
import numpy as np
from sklearn.metrics import f1_score
def f1_score_func(preds, labels):
# prediction: the prediction generated by model: [0.7, 0.2, 0.04, 0.06, 0.0]
# labels: the true label
# we want to convert prediction into [1, 0, 0, 0, 0]
# the purpose of flatten is to convert [[]] into []
preds_flat = np.argmax(preds, axis=1).flatten()
labels_flat = labels.flatten()
return f1_score(labels_flat, preds_flat, average='weighted')
Accuracy: the most intuitive metrics
Last updated