Normalization
Normalization to the range 0..1
Below is a minimal example: the normalize function maps a value into the range from 0 to 1 using the minimum and maximum values.
Example of use
<?php
function normalize(float $value, float $min, float $max): float {
$range = $max - $min;
if ($range === 0.0) {
return 0.0;
}
return ($value - $min) / $range;
}
try {
$features = [1.12, 0.85, 2.33, 4.67, 2.91, 0.44, 0.58, 0.76, 3.29, 0.50];
$min = min($features);
$max = max($features);
$result = [];
foreach ($features as $feature) {
$normalizedFeature = normalize(value: $feature, min: $min, max: $max);
$result[] = round($normalizedFeature, 2);
}
echo 'Features: [1.12, 0.85, 2.33, 4.67, 2.91, 0.44, 0.58, 0.76, 3.29, 0.50]' . PHP_EOL . PHP_EOL;
echo 'Result (normalized): ' . PHP_EOL;
echo print_r($result, true) . PHP_EOL . PHP_EOL;
echo 'Explanation: (1.12 - 0.44) / (4.67 - 0.44) = 0.16';
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Result:
Memory: 0.003 Mb
Time running: < 0.001 sec.
Features: [1.12, 0.85, 2.33, 4.67, 2.91, 0.44, 0.58, 0.76, 3.29, 0.50]
Result (normalized):
Array
(
[0] => 0.16
[1] => 0.1
[2] => 0.45
[3] => 1
[4] => 0.58
[5] => 0
[6] => 0.03
[7] => 0.08
[8] => 0.67
[9] => 0.01
)
Explanation: (1.12 - 0.44) / (4.67 - 0.44) = 0.16
The key idea is simple: normalization makes features comparable and helps many models work more stably.