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

f1 score is a good way to balance: precision and recall. In the following function, we convert the model prediction result into a format suitable for interpreting

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

Accuracy is by telling the user how well the model is predicting

Last updated

Was this helpful?