kdiagram.plot.evaluation.plot_polar_pr_curve¶
- kdiagram.plot.evaluation.plot_polar_pr_curve(y_true, *y_preds, names=None, title='Polar Precision-Recall Curve', figsize=(8, 8), cmap='viridis', show_grid=True, grid_props=None, savefig=None, dpi=300)[source]¶
Plots a Polar Precision-Recall (PR) Curve.
This function visualizes the performance of binary classification models by mapping the standard PR curve onto a polar plot. It is particularly useful for evaluating models on imbalanced datasets where ROC curves can be misleading.
- Parameters:
- y_true
np.ndarray 1D array of true binary labels (0 or 1).
- *y_preds
np.ndarray One or more 1D arrays of predicted probabilities or scores for the positive class.
- names
listofstr,optional Display names for each of the models. If not provided, generic names like
'Model 1'will be generated.- title
str, default=”Polar Precision-Recall Curve” The title for the plot.
- figsize
tupleof(float,float), default=(8, 8) The figure size in inches.
- cmap
str, default=’viridis’ The colormap used to assign a unique color to each model’s curve.
- show_gridbool, default=True
Toggle the visibility of the polar grid lines.
- grid_props
dict,optional Custom keyword arguments passed to the grid for styling.
- savefig
str,optional The file path to save the plot. If
None, the plot is displayed interactively.- dpi
int, default=300 The resolution (dots per inch) for the saved figure.
- y_true
- Returns:
- ax
matplotlib.axes.Axes The Matplotlib Axes object containing the plot.
- ax
- Parameters:
See also
plot_polar_rocA companion plot for ROC analysis.
sklearn.metrics.precision_recall_curveThe underlying scikit-learn function.
Notes
A Precision-Recall (PR) curve is a standard tool for evaluating binary classifiers, especially on imbalanced data [1]. It plots Precision against Recall at various threshold settings.
(1)¶\[\text{Precision} = \frac{TP}{TP + FP} \quad , \quad \text{Recall} = \frac{TP}{TP + FN}\]This function adapts the concept to a polar plot:
The angle (θ) is mapped to Recall, spanning from 0 at 0° to 1 at 90°.
The radius (r) is mapped to Precision, spanning from 0 at the center to 1 at the edge.
A “no-skill” classifier, which predicts randomly based on the class distribution, is represented by a horizontal line (a circle in polar coordinates) at a radius equal to the proportion of positive samples. A good model will have a curve that bows outwards towards the top-right corner of the plot, maximizing the area under the curve (Average Precision).
References
Examples
>>> import numpy as np >>> from sklearn.datasets import make_classification >>> from kdiagram.plot.evaluation import plot_polar_pr_curve >>> >>> # Generate imbalanced binary classification data >>> X, y_true = make_classification( ... n_samples=1000, ... n_classes=2, ... weights=[0.9, 0.1], # 10% positive class ... flip_y=0.1, ... random_state=42 ... ) >>> >>> # Simulate predictions from two models >>> y_pred_good = y_true * 0.6 + np.random.rand(1000) * 0.4 >>> y_pred_bad = np.random.rand(1000) >>> >>> # Generate the plot >>> ax = plot_polar_pr_curve( ... y_true, ... y_pred_good, ... y_pred_bad, ... names=["Good Model", "Random Model"] ... )