How to get classification probabilities from PySpark MultilayerPerceptronClassifier?
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
PySpark’s MultilayerPerceptronClassifier can return class probabilities directly, but many users look only at the prediction column and miss the richer output. The classifier exposes both rawPrediction and probability, and those columns are what you want for confidence-based decisions, ranking, or threshold tuning. A clean workflow is to train the model, transform a DataFrame, and inspect the probability vector per row.
Understand the Output Columns
After calling transform, Spark ML classifiers usually produce these columns:
- '
prediction' - '
rawPrediction' - '
probability'
For most application use cases, probability is the one you want. It is a vector where each element corresponds to the estimated probability of one class.
For example, in a three-class problem a probability vector might look like:
That means the model is assigning the highest confidence to class index 1.
Train a Simple Multilayer Perceptron Model
Here is a small runnable example using a local Spark session.
Once the model is trained, run inference on new rows.
Read the probability Column
The probability column is included automatically by default.
This will display a dense vector of per-class probabilities for each input row.
If you only need prediction plus confidence, you can extract the maximum probability from the vector.
Convert Probability Vectors to Usable Columns
Spark stores probabilities as vector objects. For downstream SQL-like work, convert them into arrays and individual columns.
This is especially useful when writing predictions to tables or applying threshold rules in Spark SQL pipelines.
Map Probabilities Back to Class Labels
If your labels were encoded numerically, keep a mapping so the probability vector remains interpretable.
In multiclass problems, documenting that index-to-label mapping is essential. Otherwise, the probability vector is easy to misread.
Use Probabilities for Thresholding, Not Just Ranking
The default prediction picks the class with highest probability, but sometimes you want a custom rule. For binary classification, you might require a higher threshold before predicting the positive class.
This is common in fraud, moderation, and alerting systems where false positives are costly.
rawPrediction Is Not the Same as Probability
Users often confuse rawPrediction with normalized class probabilities. For MultilayerPerceptronClassifier, rawPrediction contains intermediate scores before probability normalization.
You should usually:
- use
probabilityfor reporting and thresholds - use
predictionfor final default class - inspect
rawPredictiononly if you are debugging model behavior
Do not build business thresholds on rawPrediction unless you have a specific reason and understand the model output semantics.
Common Pitfalls
One common mistake is selecting only prediction and ignoring probability, then later trying to reconstruct model confidence.
Another issue is treating the probability vector as if it were already labeled with class names. Spark keeps only numeric positions, so you need to maintain your own class mapping.
A third mistake is using rawPrediction for thresholding when the probability column is the correctly normalized output for most downstream tasks.
Summary
- '
MultilayerPerceptronClassifierexposes probabilities in theprobabilitycolumn aftertransform.' - Use
select("probability")or convert the vector into array columns for downstream processing. - Keep an explicit mapping from class index to business label.
- Use probabilities for thresholds and confidence-based decisions.
- Treat
rawPredictionandprobabilityas different outputs with different purposes.
Related reading
- How to get comparable and reproducible results from LogisticRegressionCV and GridSearchCV
- how to get covariance matrix in tensorflow?
- how to get covariance matrix in tensorflow?
- How to get current available GPUs in tensorflow?
- How to get current available GPUs in tensorflow?
- How to get current TensorFlow name scope
- how to get data type of a tensor in tensorflow?
- How to get decision function in randomforest in sklearn
.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.