# How to Calculate the F1 Score in Python

The F1 Score is a commonly used performance metric for binary or multi-class classification problems. It represents a balance between precision and recall. In this comprehensive guide, we will dive deep into the process of calculating the F1 Score in Python.

## Part 1: Understanding the F1 Score

Before we delve into Python code, let’s familiarize ourselves with the theoretical underpinnings of the F1 Score.

The F1 Score is the harmonic mean of precision and recall, two metrics that quantify the quality of classification models. Precision measures the proportion of true positive predictions (out of all positive predictions), whereas recall (also known as sensitivity or true positive rate) measures the proportion of true positive predictions (out of all actual positives).

The F1 Score is calculated as follows:

F1 = 2 * (precision * recall) / (precision + recall)

By taking the harmonic mean of precision and recall, the F1 Score punishes extreme values. It gives a high score only when both recall and precision are high. Therefore, the F1 Score is a more suitable measure than accuracy when you have an uneven class distribution.

## Part 2: Computing F1 Score in Python

Now that we understand what the F1 Score is, we’ll discuss how to calculate it in Python. We’ll use the Breast Cancer Wisconsin dataset, a common dataset for classification problems.

### Step 1: Import Necessary Libraries

Firstly, we need to import the necessary Python libraries.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score

### Step 2: Load and Preprocess Data

Next, we’ll load the Breast Cancer Wisconsin dataset, which comes with Scikit-learn. This dataset has 30 features and a binary target variable indicating whether the breast cancer is malignant or benign.

# Load the dataset

# Extract features and target
X = data.data
y = data.target

Then, we split the data into training and testing sets. The model will be trained on the training data and evaluated on the test data.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

### Step 3: Train the Model

Let’s create a Logistic Regression model and train it on our training data.

# Create and train the model
model = LogisticRegression(max_iter=10000)
model.fit(X_train, y_train)

### Step 4: Predict and Calculate F1 Score

To calculate the F1 Score, we need to make predictions on the test data using the trained model. Then, we use the f1_score function from Scikit-learn’s metrics module.

# Predict
y_pred = model.predict(X_test)

# Calculate F1 Score
f1 = f1_score(y_test, y_pred)
print('F1 Score: ', f1)

## Conclusion

In this guide, we’ve learned how to calculate the F1 Score in Python. The F1 Score is a crucial performance metric for binary and multi-class classification problems, especially when dealing with imbalanced datasets. It provides a balance between precision (how many selected items are relevant?) and recall (how many relevant items are selected?).

As a harmonic mean of precision and recall, the F1 Score tries to find the balance between these two aspects and gives a higher score only if both values are high. This makes it a robust metric for overall model performance, especially in cases where either false positives or false negatives are more costly.