GnuplotEx.ML.Confusion (gnuplot_ex v0.2.4)
Confusion matrix heatmap visualization.
Provides functions to visualize classification confusion matrices as heatmaps with proper labels, normalization options, and customizable color schemes.
Examples
# Basic confusion matrix
matrix = [
[50, 2, 1],
[3, 45, 2],
[1, 1, 48]
]
classes = ["Cat", "Dog", "Bird"]
plot = GnuplotEx.ML.Confusion.plot(matrix, classes)
GnuplotEx.render(plot, :svg)
# Normalized confusion matrix
plot = GnuplotEx.ML.Confusion.plot(matrix, classes,
normalize: true,
title: "Normalized Confusion Matrix"
)
Summary
Functions
Calculate accuracy from confusion matrix.
Calculate F1 score from confusion matrix.
Normalize confusion matrix by row (true class).
Plot a confusion matrix as a heatmap.
Calculate per-class precision from confusion matrix.
Calculate per-class recall from confusion matrix.
Functions
Calculate accuracy from confusion matrix.
Returns the overall accuracy (sum of diagonal / sum of all elements).
Example
iex> matrix = [[50, 10], [5, 35]]
iex> GnuplotEx.ML.Confusion.accuracy(matrix)
0.85
Calculate F1 score from confusion matrix.
Returns a list of F1 scores for each class.
F1 = 2 (precision recall) / (precision + recall)
Example
iex> matrix = [[50, 10], [5, 35]]
iex> GnuplotEx.ML.Confusion.f1_score(matrix)
[0.8771929824561403, 0.823529411764706]
Normalize confusion matrix by row (true class).
Each row sums to 1.0, showing the proportion of predictions for each true class.
Example
iex> matrix = [[50, 10], [5, 35]]
iex> GnuplotEx.ML.Confusion.normalize_matrix(matrix)
[[0.8333333333333334, 0.16666666666666666], [0.125, 0.875]]
Plot a confusion matrix as a heatmap.
Creates a heatmap visualization of a confusion matrix with class labels on both axes and optional normalization.
Parameters
matrix- 2D list representing the confusion matrix (rows = true labels, cols = predicted labels)class_names- List of class names for labels
Options
:normalize- Normalize by row (true class) whentrue(default:false):title- Plot title (default: "Confusion Matrix"):palette- Color palette (default::viridis):show_values- Annotate cells with values (default:false, gnuplot limitation):colorbar- Show colorbar (default:true):value_format- Format string for normalized values (default: "%.2f")
Examples
# Binary classification
matrix = [[95, 5], [10, 90]]
classes = ["Negative", "Positive"]
plot = GnuplotEx.ML.Confusion.plot(matrix, classes)
# Multi-class with normalization
matrix = [[50, 2, 1], [3, 45, 2], [1, 1, 48]]
classes = ["Cat", "Dog", "Bird"]
plot = GnuplotEx.ML.Confusion.plot(matrix, classes,
normalize: true,
palette: :plasma
)
# Custom color scheme
plot = GnuplotEx.ML.Confusion.plot(matrix, classes,
palette: {:custom, ["#ffffff", "#ff0000"]}
)
Calculate per-class precision from confusion matrix.
Precision = True Positives / (True Positives + False Positives)
Returns a list of precision values for each class.
Example
iex> matrix = [[50, 10], [5, 35]]
iex> GnuplotEx.ML.Confusion.precision(matrix)
[0.9090909090909091, 0.7777777777777778]
Calculate per-class recall from confusion matrix.
Recall = True Positives / (True Positives + False Negatives)
Returns a list of recall values for each class.
Example
iex> matrix = [[50, 10], [5, 35]]
iex> GnuplotEx.ML.Confusion.recall(matrix)
[0.8333333333333334, 0.875]