SKLearner Home | About | Contact | Examples

Scikit-Learn recall_score() Metric

Recall is a metric used to evaluate the performance of classification models. It measures the ability of a classifier to find all the positive instances in a dataset.

The recall_score() function in scikit-learn calculates recall by dividing the number of true positive predictions by the sum of true positives and false negatives. It takes the true labels and predicted labels as input and returns a float value between 0 and 1, with 1 being perfect recall.

Recall is particularly important in problems where missing positive instances is costly, such as in medical diagnosis or fraud detection. However, recall should not be used alone when dealing with imbalanced datasets; consider other metrics like precision and F1-score for a more balanced evaluation.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import recall_score

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_classes=2, weights=[0.1, 0.9], random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a logistic regression classifier
clf = LogisticRegression(random_state=42)
clf.fit(X_train, y_train)

# Predict on test set
y_pred = clf.predict(X_test)

# Calculate recall
recall = recall_score(y_test, y_pred)
print(f"Recall: {recall:.2f}")

Running the example gives an output like:

Recall: 0.97

The steps are as follows:

  1. Generate a synthetic binary classification dataset using make_classification() with imbalanced classes.
  2. Split the dataset into training and test sets using train_test_split().
  3. Train a LogisticRegression classifier on the training set.
  4. Use the trained classifier to make predictions on the test set with predict().
  5. Calculate the recall of the predictions using recall_score() by comparing the predicted labels to the true labels.

First, we generate a synthetic binary classification dataset using the make_classification() function from scikit-learn. This function creates a dataset with 1000 samples and imbalanced classes, allowing us to simulate a classification problem where the positive class is less frequent.

Next, we split the dataset into training and test sets using the train_test_split() function. This step is crucial for evaluating the performance of our classifier on unseen data. We use 80% of the data for training and reserve 20% for testing.

With our data prepared, we train a logistic regression classifier using the LogisticRegression class from scikit-learn. The fit() method is called on the classifier object, passing in the training features (X_train) and labels (y_train) to learn the underlying patterns in the data.

After training, we use the trained classifier to make predictions on the test set by calling the predict() method with X_test. This generates predicted labels for each sample in the test set.

Finally, we evaluate the recall of our classifier using the recall_score() function. This function takes the true labels (y_test) and the predicted labels (y_pred) as input and calculates the ratio of true positive predictions to the sum of true positives and false negatives. The resulting recall score is printed, giving us a quantitative measure of our classifier’s ability to identify positive instances.



See Also