Standardization
Standardization to z-scores
Below is a minimal example: the standardize function converts a value into a z-score using the mean and standard deviation.
Example of use
<?php
function standardize(float $value, float $mean, float $std): float {
if ($std == 0.0) {
return 0.0;
}
return ($value - $mean) / $std;
}
// Suppose this feature is "user response time" (in seconds)
$value = 8.5;
// Statistics from the training set
$mean = 5.0; // mean value
$std = 2.0; // standard deviation
$zScore = standardize($value, $mean, $std);
echo 'Z-score for ' . $value . ' is ' . round($zScore, 2) . PHP_EOL;
// Interpretation
if ($zScore > 2) {
echo 'The value is much higher than average (anomaly)';
} elseif ($zScore < -2) {
echo 'The value is much lower than average (anomaly)';
} elseif ($zScore > 1) {
echo 'Above average';
} elseif ($zScore < -1) {
echo 'Below average';
} else {
echo 'Within normal range';
}
Result:
Memory: 0.002 Mb
Time running: < 0.001 sec.
Z-score for 8.5 is 1.75
Above average
The key idea is simple: standardization centers the data and expresses it in comparable statistical units.