GnuplotEx.ML.ROC (gnuplot_ex v0.5.1)
ROC curve visualization for binary and multi-class classification.
Provides functions to plot Receiver Operating Characteristic (ROC) curves, which show the trade-off between true positive rate and false positive rate at various classification thresholds.
Examples
# Binary classification ROC curve
fpr = [0.0, 0.1, 0.2, 0.5, 1.0]
tpr = [0.0, 0.6, 0.8, 0.95, 1.0]
plot = GnuplotEx.ML.ROC.plot(fpr, tpr, auc: 0.85)
GnuplotEx.render(plot, :svg)
# Multi-class ROC curves
roc_data = %{
"Class A" => {fpr_a, tpr_a, 0.92},
"Class B" => {fpr_b, tpr_b, 0.88},
"Class C" => {fpr_c, tpr_c, 0.85}
}
plot = GnuplotEx.ML.ROC.plot_multiclass(roc_data)
Summary
Functions
Calculate the area under the ROC curve (AUC) using the trapezoidal rule.
Calculate ROC curve points from scores and labels.
Plot a binary classification ROC curve.
Plot ROC curves for multi-class classification.
Functions
Calculate the area under the ROC curve (AUC) using the trapezoidal rule.
Parameters
fpr- List of false positive rates (x values, must be sorted)tpr- List of true positive rates (y values)
Returns
The AUC value as a float between 0.0 and 1.0.
Examples
iex> fpr = [0.0, 0.25, 0.5, 0.75, 1.0]
iex> tpr = [0.0, 0.5, 0.75, 0.9, 1.0]
iex> GnuplotEx.ML.ROC.calculate_auc(fpr, tpr)
0.8125
Calculate ROC curve points from scores and labels.
Given prediction scores and true binary labels, computes the FPR and TPR at various thresholds.
Parameters
scores- List of prediction scores (higher = more likely positive)labels- List of true binary labels (1 for positive, 0 for negative)
Options
:n_thresholds- Number of thresholds to evaluate (default: 100)
Returns
A tuple {fpr, tpr, thresholds} where each is a list.
Examples
iex> scores = [0.9, 0.8, 0.7, 0.4, 0.3, 0.1]
iex> labels = [1, 1, 0, 1, 0, 0]
iex> {fpr, tpr, _thresholds} = GnuplotEx.ML.ROC.calculate_curve(scores, labels)
Plot a binary classification ROC curve.
Creates a plot showing the True Positive Rate vs False Positive Rate, with a diagonal line representing random guessing (AUC = 0.5).
Parameters
fpr- List of false positive rates (x-axis values)tpr- List of true positive rates (y-axis values)
Options
:auc- Area Under Curve value to display in label (default:nil):title- Plot title (default: "ROC Curve"):label- Legend label (default: auto-generated with AUC if provided):color- Line color (default: "#E95420" - Ubuntu orange):line_width- Line width (default: 2):show_random- Show random classifier baseline (default:true):legend- Legend position (default: :bottom_right)
Examples
iex> fpr = [0.0, 0.1, 0.2, 0.5, 1.0]
iex> tpr = [0.0, 0.6, 0.8, 0.95, 1.0]
iex> plot = GnuplotEx.ML.ROC.plot(fpr, tpr, auc: 0.85)
# Without AUC label
iex> plot = GnuplotEx.ML.ROC.plot(fpr, tpr,
...> label: "My Classifier",
...> color: "#0066CC"
...> )
# Hide random baseline
iex> plot = GnuplotEx.ML.ROC.plot(fpr, tpr,
...> auc: 0.85,
...> show_random: false
...> )
Plot ROC curves for multi-class classification.
Creates a single plot with ROC curves for multiple classes, useful for visualizing one-vs-rest or one-vs-one classification performance.
Parameters
roc_data- Map of class names to{fpr, tpr, auc}tuples
Options
:title- Plot title (default: "Multi-class ROC Curves"):colors- Map of class names to colors (auto-assigned if not provided):line_width- Line width (default: 2):show_random- Show random classifier baseline (default:true):legend- Legend position (default: :bottom_right)
Examples
iex> roc_data = %{
...> "Cat" => {[0.0, 0.1, 0.3, 1.0], [0.0, 0.7, 0.95, 1.0], 0.92},
...> "Dog" => {[0.0, 0.15, 0.4, 1.0], [0.0, 0.65, 0.9, 1.0], 0.88},
...> "Bird" => {[0.0, 0.2, 0.5, 1.0], [0.0, 0.6, 0.85, 1.0], 0.85}
...> }
iex> plot = GnuplotEx.ML.ROC.plot_multiclass(roc_data)
# Custom colors
iex> plot = GnuplotEx.ML.ROC.plot_multiclass(roc_data,
...> colors: %{
...> "Cat" => "#E95420",
...> "Dog" => "#0066CC",
...> "Bird" => "#77DD77"
...> }
...> )