Reference · Statistics

stats.normalize

signature
stats.normalize(list | null) -> list

What it does

Each number of a list scaled to 0..1: the smallest value becomes 0.0, the largest 1.0, and the rest keep their proportions between them. The answer is a list of floats in the same order. It is min-max scaling, not in Python's statistics module; for scaling by the spread use stats.zscore.

Examples

print(stats.normalize([1, 2, 3, 4, 5]))print(stats.normalize([72, 85, 90, 66, 85, 78]) | map((v) => round(v, 2)))
output
[0.0, 0.25, 0.5, 0.75, 1.0][0.25, 0.79, 1.0, 0.0, 0.79, 0.5]

Edge cases

print(stats.normalize([5, 5, 5]), stats.normalize([3]), stats.normalize([]))print(stats.normalize([-2, 0, 2]), stats.normalize([10, 0]))
output
[0.0, 0.0, 0.0] [0.0] [][0.0, 0.5, 1.0] [1.0, 0.0]

Errors

An item that is not a number is a runtime error, not an error value. So is an argument that is not a list or null.

this one fails
print(stats.normalize([1, null]))
output
line 1: `stats.normalize` expects a list of numbers, but item 1 is null

Chaining

print([1, 2, 3] | stats.normalize | map((v) => v * 100))print([4, 1, 3] | stats.normalize | map((v) => round(v, 2)))
output
[0.0, 50.0, 100.0][1.0, 0.0, 0.67]

See also

stats.zscore, stats.rank, min, max, vec.clip

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On stats.normalize(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo