# Defining Performance Metrics

## 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

```python
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

```python
def accuracy_per_class(preds, labels):
    # e.g. index 1 -> happy ; index 2 -> sad
    label_dict_inverse = {v: k for k, v in label_dict.items()}
    preds_flat= np.argmax(preds, axis=1).flatten()
    labels_flat = labels.flatten()
    
    # iterate through each class a prediction has been made
    for label in np.unique(labels_flat):
        # select all positions, where the ”true label” should be ”label”, and see what’s the value preds_flat at this positions
        y_preds = preds_flat[labels_flat==label]
        y_true = labels_flat[labels_flat==label]
        
        print(f'class: {label_dict_inverse[label]}')
        print(f'Accuracy: {len(y_preds[y_preds==label])/len(y_true)}\n')

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://michael-mao.gitbook.io/sentiment-analysis-bert/pytorch-bert/defining-performance-metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
