matlab
PNN
class probabilities
ROC plot
machine learning

Find class probabilities in matlab PNN and make ROC plot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

With a MATLAB probabilistic neural network, the main challenge is not drawing the ROC curve itself. The challenge is getting a meaningful score per class that can be used as the decision value for ROC analysis. Once you have per-sample class scores, perfcurve does the rest.

Train a PNN and Inspect the Outputs

In MATLAB's neural network workflow, inputs are usually arranged as columns and class targets are often one-hot encoded. A PNN built with newpnn produces output values per class for each sample. Those outputs can be used as class scores, and in many workflows they are treated as probability-like scores for ranking.

matlab
1P = [1 2 1 2 8 9 8 9;
2     1 1 2 2 8 8 9 9];
3
4T = [1 1 1 1 0 0 0 0;
5     0 0 0 0 1 1 1 1];
6
7spread = 0.5;
8net = newpnn(P, T, spread);
9
10Y = net(P);
11disp(Y)

Y is a matrix where each column corresponds to one sample and each row corresponds to one class. For binary classification, Y(2, :) can be used as the score for the positive class if class 2 is your positive label.

Build an ROC Curve with perfcurve

For ROC analysis, you need true class labels and one continuous score per sample. The labels should be a vector, not one-hot encoded.

matlab
1labels = [1 1 1 1 2 2 2 2];
2positiveClass = 2;
3scores = Y(2, :);
4
5[fpr, tpr, threshold, auc] = perfcurve(labels, scores, positiveClass);
6
7plot(fpr, tpr, 'LineWidth', 2)
8xlabel('False positive rate')
9ylabel('True positive rate')
10title(['ROC curve, AUC = ' num2str(auc)])
11grid on

This works because perfcurve only needs a ranking score. The score does not have to be a hard class prediction.

Multi-Class ROC Usually Means One-vs-Rest

If the PNN predicts more than two classes, build ROC curves one class at a time. For each class, treat that class as positive and the rest as negative. Then use the corresponding row of the network output matrix as the score vector.

matlab
1labels = [1 1 2 2 3 3];
2scores = rand(3, 6);  % example score matrix shaped like net output
3
4for cls = 1:3
5    [fpr, tpr, ~, auc] = perfcurve(labels, scores(cls, :), cls);
6    plot(fpr, tpr, 'DisplayName', ['Class ' num2str(cls) ', AUC=' num2str(auc)])
7    hold on
8end
9
10xlabel('False positive rate')
11ylabel('True positive rate')
12legend show
13grid on

This one-vs-rest framing is the standard way to visualize class-specific ROC behavior in multi-class problems.

Check Whether Your Outputs Need Calibration

The network outputs are useful as ranking scores for ROC, but you should be cautious before calling them perfectly calibrated probabilities. ROC only depends on ranking quality, not calibration quality. If you need trustworthy probability estimates for thresholding or downstream decision analysis, validate that behavior separately on held-out data.

That distinction matters: a model can produce good ROC curves even if the raw scores are not perfectly calibrated probabilities.

Common Pitfalls

  • Feeding hard predicted labels into perfcurve instead of continuous class scores.
  • Mixing one-hot targets and label vectors without checking which format the function expects.
  • Using the wrong output row as the positive-class score.
  • Expecting a single ROC curve to summarize a multi-class problem without one-vs-rest handling.
  • Treating all network scores as calibrated probabilities without verification.

Summary

  • MATLAB PNN output gives one score per class and sample.
  • For binary ROC, use the score row of the positive class with perfcurve.
  • For multi-class ROC, compute one-vs-rest curves class by class.
  • ROC needs continuous scores, not only predicted class labels.
  • If you need calibrated probabilities rather than ranking scores, validate calibration separately.

Course illustration
Course illustration

All Rights Reserved.