precision_recall_fscore_support()
provides a summary of precision, recall, F1 score, and support for each class in a classification problem.
- Precision: The ratio of true positive predictions to the total predicted positives.
- Recall: The ratio of true positive predictions to the total actual positives.
- F1 Score: The harmonic mean of precision and recall.
- Support: The number of actual occurrences of each class.
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
Generate a synthetic binary classification dataset using
make_classification()
.Split the dataset into training and test sets using
train_test_split()
.Train an
SVC
classifier on the training set.Predict on the test set using the trained classifier.
Calculate precision, recall, F1 score, and support using
precision_recall_fscore_support()
.Output the results, which include precision, recall, F1 score, and support values.