kdiagram.plot.evaluation.plot_polar_confusion_matrix¶
- kdiagram.plot.evaluation.plot_polar_confusion_matrix(y_true, *y_preds, names=None, normalize=True, title='Polar Confusion Matrix', figsize=(8, 8), cmap='viridis', colors=None, show_grid=True, grid_props=None, mask_radius=False, savefig=None, dpi=300, acov='default', zero_at='N', clockwise=True, categories=None, kind='polar', ax=None)[source]¶
Plots a Polar Confusion Matrix for binary classification.
This function creates a polar bar chart to visualize the four key components of a binary confusion matrix: True Positives (TP), False Positives (FP), True Negatives (TN), and False Negatives (FN).
- Parameters:
- y_true
np.ndarray 1D array of true binary labels (0 or 1). Shape
(n_samples,).- *y_preds
np.ndarray One or more 1D arrays of predicted probabilities or scores for the positive class. A fixed threshold of
0.5is applied to derive class labels.- names
listofstr,optional Display names for the models. If not provided, generic names like
'Model 1'are generated.- normalizebool, default=True
If
True, values are converted to proportions that sum to1.0within each model. IfFalse, raw counts are shown.- title
str, default=”PolarConfusionMatrix” Title to place above the figure.
- figsize
tupleof(float,float), default=(8, 8) Figure size in inches.
- cmap
str, default=’viridis’ Colormap used to assign distinct colors to the bars of each model.
- show_gridbool, default=True
Toggle the polar grid. Set to
Falsefor a minimal look.- grid_props
dict,optional Keyword arguments forwarded to the grid styling helper, e.g.
{'linestyle': '--', 'alpha': 0.5}.- mask_radiusbool, default=False
If
True, hide the radial tick labels to declutter the plot.- savefig
str,optional File path where the figure is saved. If
None, the plot is shown interactively.- dpi
int, default=300 Resolution in dots per inch used when saving.
- acov{‘default’, ‘half_circle’, ‘quarter_circle’,
‘eighth_circle’}, default=’default’ Angular coverage (span) of the polar plot.
'default'covers 360°,'half_circle'180°,'quarter_circle'90°, and'eighth_circle'45°. Fewer degrees compress the four sectors into a smaller sweep.- zero_at{‘N’, ‘E’, ‘S’, ‘W’}, default=’N’
Cardinal direction where
θ = 0is placed. For example,'E'puts zero at the right-hand side.- clockwisebool, default=True
Direction of increasing angle.
Truedraws clockwise;Falsedraws counter-clockwise.- categories
listofstr,optional Custom labels for the four sectors. Must contain exactly four items. If
None, uses:['True Positive', 'False Positive', 'True Negative', 'False Negative']
The sectors are laid out starting at
θ = 0and follow the chosen direction.- kind{‘polar’, ‘cartesian’}, default=’polar’
Rendering mode selector. When set to
'polar'(default), the plot uses a Matplotlib polar projection and applies polar-specific options (acov,zero_at,clockwise) via internal helpers. When set to'cartesian', the function delegates to a Cartesian renderer (throughmaybe_delegate_cartesian), keeping names/colors/figsize/grid behavior consistent while ignoring polar- only arguments (e.g.,acov,zero_at,clockwise). The return value is always theAxesactually used. The value is validated withvalidate_kind(case-insensitive); invalid values raiseValueError("kind must be 'polar' or 'cartesian'.").- ax
matplotlib.axes.Axes,optional Existing polar axes to draw on. If
None, a new figure and polar axes are created.
- y_true
- Returns:
- ax
matplotlib.axes.Axes The Matplotlib Axes object containing the plot.
- ax
- Parameters:
- Return type:
See also
plot_polar_confusion_multiclassThe companion plot for multiclass problems.
sklearn.metrics.confusion_matrixThe underlying scikit-learn function.
Notes
The confusion matrix is a fundamental tool for evaluating a classifier’s performance [1]. This function maps its four components to a polar bar chart for intuitive comparison.
True Positives (TP): Correctly predicted positive cases.
False Positives (FP): Negative cases incorrectly predicted as positive.
True Negatives (TN): Correctly predicted negative cases.
False Negatives (FN): Positive cases incorrectly predicted as negative.
Each of these four categories is assigned its own angular sector, and the height (radius) of the bar in that sector represents the count or proportion of samples in that category.
References
Examples
>>> import numpy as np >>> from sklearn.datasets import make_classification >>> from kdiagram.plot.evaluation import plot_polar_confusion_matrix >>> >>> # Generate synthetic binary classification data >>> X, y_true = make_classification( ... n_samples=500, n_classes=2, flip_y=0.2, random_state=42 ... ) >>> >>> # Simulate predictions from two models >>> y_pred1 = y_true * 0.8 + np.random.rand(500) * 0.4 # Good model >>> y_pred2 = np.random.rand(500) # Random model >>> >>> # Generate the plot >>> ax = plot_polar_confusion_matrix( ... y_true, ... y_pred1, ... y_pred2, ... names=["Good Model", "Random Model"], ... normalize=True ... )