Find class probabilities in matlab PNN and make ROC plot
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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.
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.
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.
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
perfcurveinstead 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.
Related reading
- Find input that maximises output of a neural network using Keras and TensorFlow
- Find Unique values in a 2D Tensor using Tensorflow
- Finding a corresponding leaf node for each data point in a decision tree scikit-learn
- Finding daily patterns with machine learning
- Finding good heuristic for A search
- Finding groups of similar strings in a large set of strings
- Finding K-nearest neighbors and its implementation
- Finding the best cosine similarity in a set of vectors
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.