Overview
Machine learning plotting functions provide visualizations for model evaluation and diagnostics.
Classification Visualizations
plotConfusionMatrix
Plot a confusion matrix as a heatmap.
function plotConfusionMatrix(
cm: Tensor,
labels?: readonly string[],
options?: PlotOptions
): void
Confusion matrix tensor (2D)
Class labels for axis annotations
Displays a confusion matrix with proper axis labels and annotations. Rows represent true labels, columns represent predicted labels.
Example:
import { plotConfusionMatrix, confusionMatrix, tensor } from 'deepbox';
const yTrue = tensor([0, 1, 1, 0, 1, 2, 2, 0]);
const yPred = tensor([0, 1, 0, 0, 1, 2, 1, 0]);
const cm = confusionMatrix(yTrue, yPred);
plotConfusionMatrix(cm, ['Class 0', 'Class 1', 'Class 2']);
plotRocCurve
Plot a ROC curve with optional AUC annotation.
function plotRocCurve(
fpr: Tensor,
tpr: Tensor,
auc?: number,
options?: PlotOptions
): void
Area under curve (optional, for title annotation)
Plots the Receiver Operating Characteristic curve and includes a diagonal reference line representing random chance.
Example:
import { plotRocCurve, rocCurve, rocAucScore, tensor } from 'deepbox';
const yTrue = tensor([0, 0, 1, 1]);
const yScore = tensor([0.1, 0.4, 0.35, 0.8]);
const [fpr, tpr, _] = rocCurve(yTrue, yScore);
const auc = rocAucScore(yTrue, yScore);
plotRocCurve(fpr, tpr, auc);
plotPrecisionRecallCurve
Plot a precision-recall curve with optional AP annotation.
function plotPrecisionRecallCurve(
precision: Tensor,
recall: Tensor,
averagePrecision?: number,
options?: PlotOptions
): void
Average precision score (optional, for title annotation)
Plots the precision-recall curve, which is particularly useful for imbalanced datasets.
Example:
import { plotPrecisionRecallCurve, precisionRecallCurve, averagePrecisionScore, tensor } from 'deepbox';
const yTrue = tensor([0, 0, 1, 1]);
const yScore = tensor([0.1, 0.4, 0.35, 0.8]);
const [prec, rec, _] = precisionRecallCurve(yTrue, yScore);
const ap = averagePrecisionScore(yTrue, yScore);
plotPrecisionRecallCurve(prec, rec, ap);
plotDecisionBoundary
Plot a classifier decision boundary on a 2D feature space.
function plotDecisionBoundary(
X: Tensor,
y: Tensor,
model: { predict: (x: Tensor) => Tensor },
options?: PlotOptions
): void
Feature matrix of shape [n_samples, 2]
Class labels of shape [n_samples]
Model with a predict method that accepts a tensor and returns predictions
Plot styling options (colormap, colors, size, etc.)
Visualizes the decision boundary learned by a 2D classifier, showing how the model partitions the feature space.
Example:
import { plotDecisionBoundary, tensor } from 'deepbox';
// Assuming you have a trained model
const X = tensor([[1, 2], [2, 3], [3, 1], [5, 6]]);
const y = tensor([0, 0, 1, 1]);
plotDecisionBoundary(X, y, trainedModel);
Training Visualizations
plotLearningCurve
Plot training and validation learning curves.
function plotLearningCurve(
trainSizes: Tensor,
trainScores: Tensor,
valScores: Tensor,
options?: PlotOptions
): void
Training scores at each size
Validation scores at each size
Plot styling options (colors, etc.)
Visualizes how model performance changes with training set size, useful for diagnosing bias and variance.
Example:
import { plotLearningCurve, tensor } from 'deepbox';
const trainSizes = tensor([100, 200, 300, 400, 500]);
const trainScores = tensor([0.85, 0.87, 0.88, 0.89, 0.90]);
const valScores = tensor([0.75, 0.78, 0.80, 0.81, 0.82]);
plotLearningCurve(trainSizes, trainScores, valScores);
plotValidationCurve
Plot training and validation curves across hyperparameter values.
function plotValidationCurve(
paramRange: Tensor,
trainScores: Tensor,
valScores: Tensor,
options?: PlotOptions
): void
Training scores for each parameter value
Validation scores for each parameter value
Visualizes model performance across a range of hyperparameter values, useful for hyperparameter tuning.
Example:
import { plotValidationCurve, tensor } from 'deepbox';
const alphaValues = tensor([0.001, 0.01, 0.1, 1.0, 10.0]);
const trainScores = tensor([0.92, 0.90, 0.88, 0.85, 0.80]);
const valScores = tensor([0.78, 0.82, 0.85, 0.84, 0.79]);
plotValidationCurve(alphaValues, trainScores, valScores);