File:PR curve with optimal fscore.png

PR_curve_with_optimal_fscore.png (653 × 547 pixels, file size: 36 KB, MIME type: image/png)

import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_recall_curve, f1_score

# Generate synthetic data with make_classification
X, y_true = make_classification(n_samples=1000, n_features=20, random_state=42)

# Create a Logistic Regression model
model = LogisticRegression()

# Fit the model on the data
model.fit(X, y_true)

# Predict probabilities for the positive class
y_scores = model.predict_proba(X)[:, 1]

# Compute precision, recall, and F-score
precision, recall, thresholds = precision_recall_curve(y_true, y_scores)
f_scores = 2 * (precision * recall) / (precision + recall)

# Find the threshold with the maximal F-score
max_f_score_idx = np.argmax(f_scores)
max_f_score_threshold = thresholds[max_f_score_idx]

# Create the PR curve plot
plt.figure(figsize=(8, 6))
plt.scatter(recall[:-1], precision[:-1], c=thresholds)
plt.scatter(recall[max_f_score_idx], precision[max_f_score_idx], c='red', marker='o', label=f'Max F-score ({max_f_score_threshold:.2f})', s=100)
plt.colorbar()
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve')
plt.legend()
plt.grid(True)
plt.show()

Summary

Description
English: Precision Recall Curve, points from different thresholds are color coded, the point with optimal fscore is highlighted in red
Date
Source Own work
Author Biggerj1

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Captions

Precision Recall Curve, points from different thresholds are color coded, the point with optimal fscore is highlighted in red

Items portrayed in this file

depicts

9 September 2023

image/png

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current11:38, 9 September 2023Thumbnail for version as of 11:38, 9 September 2023653 × 547 (36 KB)Biggerj1Uploaded own work with UploadWizard

The following page uses this file:

Global file usage

The following other wikis use this file:

Metadata