File:PR curve with optimal fscore.png

From testwiki
Jump to navigation Jump to search
    Original file (653 × 547 pixels, file size: 36 KB, MIME type: image/png)

    This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

    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

    37,001 byte

    547 pixel

    653 pixel

    dfcc4f955ab5a4ca93faf5d5e311341577206356

    File history

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

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

    There are no pages that use this file.