Probability as degree of confidence
Implementation in pure PHP
In many machine learning models, the outputs are not probabilities, but so‑called scores (logits). These are just numbers that reflect the model's relative confidence in each option. They can be anything — positive, negative, large or small — and by themselves they are not interpreted as probabilities. To turn such scores into proper probabilities, we use the softmax function.
Example of use
<?php
require_once __DIR__ . '/code.php';
// Example raw scores ("logits") for each email category before converting to probabilities
$scores = [
'spam' => 2.1,
'promo' => 1.3,
'normal' => 0.2,
];
$probabilities = softmax($scores);
foreach ($probabilities as $class => $probability) {
echo $class . ': ' . round($probability, 3) . PHP_EOL;
}
Result:
Memory: 0.003 Mb
Time running: < 0.001 sec.
spam: 0.625
promo: 0.281
normal: 0.094
Now we have a correct probability distribution: each value lies in the range from 0 to 1; the sum of all values equals 1; and the numbers can be interpreted as the model's degree of confidence.