# F1 Score in Machine Learning

When precision and recall are of paramount importance, and one cannot afford to prioritize one over the other, the F1 Score emerges as the go-to metric. This score represents the harmonic mean of precision and recall, a method of calculating an average that rightly penalizes extreme values.

Have you ever considered why, despite having high accuracy, a machine learning model might still fail to meet expectations in real-world applications? It's an intriguing quandary that many professionals encounter, underscoring the need for a more nuanced metric to evaluate model performance. This conundrum brings us to the F1 Score, a critical evaluation metric that delves deep into the precision and recall of predictive models.

In the realm of binary and multi-class classification problems, accuracy alone can paint a deceptively rosy picture, especially when the data is imbalanced. It's here that the F1 Score comes into play, offering a more balanced assessment through the harmonic mean of precision and recall. Unlike the arithmetic mean, the harmonic mean emphasizes the lowest numbers, ensuring that both false positives and false negatives receive due attention, thus providing a more holistic view of model performance.

## What is the F1 Score in Machine Learning?

When precision and recall are of paramount importance, and one cannot afford to prioritize one over the other, the F1 Score emerges as the go-to metric. This score represents the harmonic mean of precision and recall, a method of calculating an average that rightly penalizes extreme values.

See the image below for the mathematical formulas behind the Arithmetic Mean, Geometric Mean, and Harmonic Mean.

The essence of the F1 Score lies in its ability to capture the trade-off between the two critical components of a model's performance: the precision, which measures how many of the items identified as positive are actually positive, and recall, which measures how many of the actual positives the model identifies.

The importance of the F1 Score becomes particularly evident when dealing with datasets with class imbalance—a common scenario in domains like fraud detection where fraudulent transactions are rare compared to legitimate ones. In such cases, a model might have a high accuracy by simply predicting the majority class, but this would be misleading as it fails to catch the rare but crucial instances of fraud. Let’s walk through a concrete example:

Let’s say you’re a bank with 1,000,000 customers. Because it’s really hard to commit fraud, let’s say that only 100 of your customers are credit criminals, who are set out to steal money from the rest of the people who bank with you.

It would be very easy to create a computer program that “detects fraud” with over 99% accuracy. The program would follow this logic:

Look at each individual customer one-by-one. Without looking at any of their credit-card statements, their checking account history, or even their name, just classify that person as a non-fraudulent customer.

If we simply classify everyone as “not a fraud” then our computer program would correctly classify 999,900 of the customers. That’s 99.99% accuracy!

… But as we can see, this computer program isn’t artificially intelligent at all. In fact, it’s the opposite. It’s naturally dumb. It does absolutely no work, and it doesn’t catch any of the fraudsters. Yet with that 99.99% accuracy, we could deceptively claim that the computer program is “smart.”

The F1 Score mitigates this issue by equally weighing false positives and false negatives, ensuring that a high score can only be achieved by a model that performs well on both fronts.

However, the F1 Score is sensitive to changes in precision and recall, which means that a model must maintain a balance between these two metrics to achieve a high F1 Score. This sensitivity is a double-edged sword—it can provide nuanced insights into a model's performance, but it also means that a model can be penalized harshly for weaknesses in either metric, as noted in the Serokell Blog.

Moreover, the impact of false positives and false negatives on the F1 Score cannot be overstated. A false positive occurs when an instance is wrongly classified as positive, while a false negative is when a positive instance is missed. Both errors are equally detrimental to the F1 Score. A 'perfect' precision and recall, where the model correctly identifies all positives without any false positives or negatives, results in an F1 Score of 1, the ideal scenario. Conversely, a low F1 Score indicates a model with substantial room for improvement in either or both metrics.

Sometimes false negatives are more important to catch than false positives. For example, in the bank-fraud example above, it’s much easier to deal with accidentally accusing someone of being a fraud (a false positive) than it is to recover the money from a criminal who slipped through the cracks (a false negative.)

Likewise, if we’re dealing with medical technology, it’s much more appealing to get a false positive—telling someone who is cancer-free that they have cancer, for example —than it is to get a false negative—telling someone with cancer that they don’t have cancer and letting them go untreated.

On other occasions, false positives are more important to catch than false negatives. We’ll leave it as an exercise to you to think of such a scenario.

Despite its usefulness, the F1 Score does have limitations, such as its inability to capture the true negative rate or specificity. This means that in certain contexts, where true negatives are as significant as true positives, the F1 Score may not provide a complete picture of a model's performance. Therefore, while the F1 Score is a powerful tool for evaluating classifiers, it should be considered alongside other metrics to gain a full understanding of a model's capabilities.

## F1 Score Formula

Delving into the mathematical heart of the F1 Score, we find a formula that stands as the beacon of balance in the classification world: F1 = 2 * (precision * recall) / (precision + recall). See the image above for the formulas of precision and recall. This equation is not just a mere string of variables and operators; it is an expression of equilibrium, ensuring that neither precision nor recall disproportionately affects the performance evaluation of a machine learning model.

The F1 Score formula's design reflects an elegant solution to a complex problem. The factor of 2 in the F1 Score formula, as elucidated by Deepchecks Glossary, is not arbitrary. It serves to adjust the scale of the metric so that it conveys the harmonic mean's properties. Notably, the harmonic mean differs from the arithmetic mean by penalizing the extremes more heavily. Hence, a model can only achieve a high F1 Score if both precision and recall are high, effectively preventing a skewed performance metric due to an imbalance between false positives and false negatives.

### The Significance of the Factor 2

Doubles the product of precision and recall: By multiplying the numerator by 2, we ensure the product of precision and recall is appropriately represented in the formula.

Maintains balance: The harmonic mean inherently requires this factor to ensure that neither precision nor recall dominates.

Reflects equal importance: It signifies that precision and recall contribute equally to the final F1 Score.

### Extreme Cases: When Precision or Recall is 0

The F1 Score demonstrates an unforgiving nature towards models that completely fail in either precision or recall. Consider a case where precision equals 0. This would imply an absence of true positives among the predicted positives, rendering the numerator of the F1 formula to zero. Similarly, if recall hits rock bottom at 0, not a single actual positive is captured by the model. In both scenarios, no matter the value of the other metric, the F1 Score plummets to 0, highlighting a complete failure in one aspect of classification.

### Asymmetry and Insensitivity

The relationship between precision and recall is asymmetric; one can be high while the other is low, and vice versa. However, the F1 Score exhibits insensitivity to this asymmetry. It does not differentiate whether it is precision or recall that is contributing more to the lower score, only that the harmonic balance is disrupted.

### The Trade-Off Between Precision and Recall

The inherent trade-off between precision and recall surfaces when we attempt to optimize one at the expense of the other. Raising the threshold for classification may increase precision but often reduces recall as fewer positives are identified. Conversely, lowering the threshold usually boosts recall at the cost of precision, as more instances are classified as positive, including those that aren't. The F1 Score encapsulates this trade-off, becoming a compass that guides models towards a more balanced classification performance.

In this finely tuned dance between precision and recall, the F1 Score remains a steadfast measure, impartial to which side falters, yet always rewarding the harmony of their union. It is this delicate equilibrium that makes the F1 Score a metric of choice for many practitioners in the field of machine learning, especially in situations where both false positives and false negatives carry significant consequences.

## How to Calculate F1 Score

Calculating the F1 score begins with understanding two fundamental components: precision and recall. These metrics emerge from the confusion matrix—a tableau of true positives, false positives, false negatives, and true negatives. The confusion matrix not only informs us about the errors a model is making but also illuminates the path to calculating these critical metrics.

### Precision and Recall from a Confusion Matrix

To illustrate, let's consider a confusion matrix with hypothetical values: 50 true positives, 10 false positives, and 5 false negatives. Precision measures the accuracy of positive predictions, calculated as the ratio of true positives to the sum of true positives and false positives (TP/(TP+FP)). In our example, precision would be 50/(50+10), equating to 0.833. Recall, on the other hand, gauges the model's ability to identify all actual positives, calculated as the ratio of true positives to the sum of true positives and false negatives (TP/(TP+FN)), resulting in 50/(50+5), or 0.909.

Precision: 50/(50+10) = 0.833

Recall: 50/(50+5) = 0.909

### F1 Score Calculation Example

The F1 Score then harmonizes these metrics using the formula: F1 = 2 * (precision * recall) / (precision + recall). Applying our precision and recall values yields an F1 Score of 2 * (0.833 * 0.909) / (0.833 + 0.909), which simplifies to an impressive 0.869. This numerical example, inspired by the guide from V7 Labs Blog, demonstrates the intermediate step of calculating precision and recall before arriving at the F1 Score.

F1 Score: 2 * (0.833 * 0.909) / (0.833 + 0.909) = 0.869

### Impact of Values on F1 Score

The values of true positives, false positives, and false negatives each have a weighty impact on the F1 Score. An increase in false positives would decrease precision, while an uptick in false negatives would drop recall, both leading to a lower F1 Score. Conversely, boosting the count of true positives enhances both precision and recall, raising the F1 Score.

### Interpretation of F1 Score Ranges

Understanding the range in which the F1 Score falls is crucial, as it provides insights into the model's performance. Generally, a score closer to 1 indicates a robust model, while a score near 0 signals poor performance. The context-specific threshold for a 'good' F1 Score varies across domains, with some industries demanding higher benchmarks due to the higher stakes involved in misclassification. The insights from Spot Intelligence suggest that anything above 0.7 may be considered acceptable in certain contexts, yet for fields like medicine or fraud detection, this threshold might be significantly higher.

### Multi-Class Classification: Micro, Macro, and Weighted Averages

When we venture into the realm of multi-class classification, the computation of F1 Score becomes nuanced. We must choose between micro, macro, and weighted averages to aggregate the F1 Scores across multiple classes. Micro averaging tallies the total true positives, false positives, and false negatives across all classes before calculating precision, recall, and hence the F1 Score. Macro averaging computes the F1 Score for each class independently and then takes the average, not accounting for class imbalance. Weighted averaging, as explained in Sefidian, takes the class imbalance into account by weighting the F1 Score of each class by its support (the number of true instances for each class).

Each averaging method presents a different lens through which to examine the model's performance, and the choice of method should align with the specific objectives and concerns of the machine learning task at hand. The interpretation of the F1 Score in multi-class classification requires a thoughtful approach, one that considers the distribution of classes and the importance of each class's accurate prediction.

Through this meticulous process of calculation and interpretation, the F1 Score stands as a testament to a model's capacity to classify with a judicious balance of precision and recall. It is the harmony of these metrics that ultimately shapes the narrative of a model's performance in the ever-evolving landscape of machine learning.

## Implementing F1 Score in Python with scikit-learn

Successful machine learning models hinge not only on accurate predictions but also on the ability to quantify their performance. The F1 score serves as a critical measure in this regard, particularly when the cost of false positives and false negatives is high. Implementing the F1 score within a Python machine learning pipeline using scikit-learn, a powerful machine learning library, involves specific functions and careful consideration of parameters.

### Utilizing scikit-learn's Metrics

The sklearn.metrics module provides the f1_score function, an essential tool for model evaluation. To employ this function, one must first fit a model to the training data and make predictions. With these predictions and the true labels at hand, the F1 score calculation is straightforward:

```
from sklearn.metrics import f1_score
# Assuming y_true contains the true labels and y_pred the predicted labels
f1 = f1_score(y_true, y_pred, average='binary')
```

This snippet computes the F1 score for a binary classification problem. However, the average parameter demands attention when dealing with multi-class classification.

### Handling Multi-Class Cases

In multi-class scenarios, the average parameter becomes pivotal. It accepts values such as 'micro', 'macro', 'weighted', and 'samples' (for multi-label classification). Each offers a different approach to averaging F1 scores across classes:

'micro': Calculate metrics globally by counting the total true positives, false negatives, and false positives.

'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.

'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label).

'samples': Calculate metrics for each instance, and find their average (only meaningful for multi-label classification, where each label set is a binary vector).

Here is an example of using the f1_score function for multi-class classification:

```
f1_micro = f1_score(y_true, y_pred, average='micro')
f1_macro = f1_score(y_true, y_pred, average='macro')
f1_weighted = f1_score(y_true, y_pred, average='weighted')
```

### Practical Considerations and Pitfalls

When implementing the F1 score, one must preprocess data effectively to ensure reliable results. Normalization, handling missing values, and encoding categorical variables correctly are prerequisites before model training and evaluation.

Moreover, selecting the appropriate average type is crucial. The choice depends on the problem's nature and the dataset's characteristics. For instance, 'macro' averaging treats all classes equally, which may not be suitable for imbalanced datasets. 'Weighted' averaging, however, can compensate for class imbalance by considering the support for each class.

Potential pitfalls during implementation include ignoring class imbalance or using an inappropriate averaging method, leading to a misleading assessment of the model's performance. It is also vital to ensure that the data used for calculating the F1 score is separate from the data used for training to avoid overfitting and ensure an unbiased evaluation.

By integrating the F1 score into the evaluation process within a machine learning pipeline, practitioners gain a more nuanced understanding of their models' performance, especially in contexts where precision and recall are equally significant. The implementation in Python with scikit-learn, as outlined and exemplified, is a testament to the metric's versatility and the library's utility in machine learning endeavors.

## Use Cases of F1 Score

The F1 score emerges as a beacon of evaluation in fields where the consequences of misclassification are significant. It is not just a number but a narrative that guides critical decisions in various industries. Let's explore the practicality of F1 score through scenarios where its application is not just beneficial but paramount.

### Medical Diagnosis

In the realm of healthcare, particularly in medical diagnosis, the F1 score assumes a role of life-saving importance. Here, the cost of a false negative — failing to identify a condition — can lead to delayed treatment and potentially fatal outcomes. Conversely, false positives can result in unnecessary anxiety and invasive procedures for patients. A high F1 score in diagnostic models signifies a robust balance of precision and recall — an assurance that the model is reliable in detecting true cases of disease without an excessive number of false alarms.

### Fraud Detection

The financial sector relies on machine learning models to detect fraudulent activities. In this domain, a false negative translates into financial loss, whereas a false positive might result in blocking legitimate transactions, damaging customer trust. The F1 score becomes the metric of choice to ensure a fraud detection system efficiently minimizes both errors. It is crucial in fine-tuning the model to detect as many fraudulent transactions as possible without inconveniencing genuine customers.

### Imbalanced Datasets

The F1 score shines in scenarios with imbalanced datasets, where one class significantly outnumbers the others. In such cases, accuracy can be misleading, as a model might predict only the majority class and still achieve a high accuracy score. The F1 score, however, will reflect the true performance of the model across all classes by balancing the precision and recall, making it indispensable for model selection and comparison.

### Precision-Recall Balance

Adjusting the threshold of classification algorithms is an art that requires careful evaluation. The F1 score plays a critical role in finding the sweet spot where both precision and recall are optimized. It guides in selecting a threshold that maintains a balance, ensuring that the model does not skew too far towards either precision or recall, which is paramount in cases where both false positives and false negatives carry significant costs.

### Feature Selection and Engineering

Feature selection and engineering are foundational to the construction of a potent machine learning model. The F1 score aids in identifying features that contribute to a balanced prediction model. By assessing the impact of individual features or combinations on the F1 score, data scientists can engineer their models to enhance precision and recall, ultimately leading to more accurate and reliable predictions.

By embracing the F1 score as a critical metric, industries ensure that they are not just chasing numbers but are genuinely improving the outcomes and experiences of the end-users. Whether it's a patient awaiting a diagnosis, a bank combating fraud, or a data scientist wrestling with an imbalanced dataset, the F1 score serves as a guiding light for precision and reliability in an often uncertain world of predictions. Through the lens of the F1 score, we capture a more complete picture of model performance, one that fosters trust and dependability in machine learning applications.

## F1 Score vs. Accuracy

When venturing into the realm of machine learning model evaluation, one quickly encounters the delicate interplay between various metrics. Among these, the F1 score and accuracy often emerge as central figures, yet their messages can sometimes diverge, leading to a conundrum known as the 'Accuracy Paradox'.

### The Misleading Nature of Accuracy

Accuracy, while intuitively appealing due to its straightforward calculation, falls short in the face of class imbalance—a common scenario in real-world data.

A model could naively predict the majority class for all instances and still achieve high accuracy, masking its inability to correctly predict minority class instances.

This overestimation of performance is particularly deceptive in critical applications like disease screening, where failing to identify positive cases can have dire consequences.

### The 'Accuracy Paradox'

The 'Accuracy Paradox' underscores the phenomenon where models with a high accuracy might possess poor predictive powers. This paradox arises when the measure of accuracy becomes disconnected from the reality of the model's effectiveness.

The F1 score, by considering both precision (the quality of the positive predictions) and recall (the ability to find all positive instances), offers a safeguard against this paradox.

An F1 score that is significantly lower than the accuracy signals that one should scrutinize the model further, especially for its performance on the minority class.

### Comparative Analysis: F1 Score's Superiority

Consider a dataset where 95% of instances belong to one class. A model that always predicts this class will be 95% accurate but utterly fail at identifying the 5% minority class.

A comparative analysis would reveal that the F1 score for this model would be much lower, reflecting the poor recall for the minority class.

Thus, in datasets with skewed class distributions, the F1 score provides a more nuanced and truthful representation of a model's performance.

### Expert Opinions on Metric Prioritization

Experts often deliberate on the context-specific prioritization of evaluation metrics.

They advocate for the F1 score in cases where false positives and false negatives carry different costs or when classes are imbalanced.

The consensus is clear: while accuracy is easy to understand and communicate, the F1 score often better aligns with the true objectives of a machine learning task.

### Balancing Simplicity and Effectiveness

The choice between F1 score and accuracy is not merely a technical decision but a strategic one. It hinges on the balance between simplicity for stakeholders' understanding and effectiveness in reflecting model performance.

In practice, this balance requires transparent communication about the limitations of accuracy and the advantages of the F1 score in capturing a model's true capabilities.

Ultimately, the decision to prioritize the F1 score hinges on the specific requirements of the application and the stakes involved in the predictive task at hand.

By delving into the intricacies of these metrics, we equip ourselves with a more informed perspective on model performance and ensure that we select the right tool for the right job. It is this informed choice that paves the way for machine learning models to generate impactful, reliable, and truly beneficial outcomes in their application.