SKLearner Home | About | Contact | Examples

Scikit-Learn precision_recall_fscore_support() Metric

precision_recall_fscore_support() provides a summary of precision, recall, F1 score, and support for each class in a classification problem.

Good values for precision, recall, and F1 score range from 0 to 1, with 1 being perfect. These metrics are used in both binary and multiclass classification problems. However, they are not suitable for regression problems. Additionally, precision and recall can be misleading on imbalanced datasets.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import precision_recall_fscore_support

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_classes=2, 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 an SVM classifier
clf = SVC(kernel='linear', C=1, random_state=42)
clf.fit(X_train, y_train)

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

# Calculate precision, recall, F1 score, and support
precision, recall, fscore, support = precision_recall_fscore_support(y_test, y_pred, average='binary')
print(f"Precision: {precision:.2f}, Recall: {recall:.2f}, F1 Score: {fscore:.2f}, Support: {support}")

Running the example gives an output like:

Precision: 0.92, Recall: 0.83, F1 Score: 0.87, Support: None
  1. Generate a synthetic binary classification dataset using make_classification().

  2. Split the dataset into training and test sets using train_test_split().

  3. Train an SVC classifier on the training set.

  4. Predict on the test set using the trained classifier.

  5. Calculate precision, recall, F1 score, and support using precision_recall_fscore_support().

  6. Output the results, which include precision, recall, F1 score, and support values.



See Also