function zscore
zscore(
x: array,
flag?: 0 | 1,
dim?: 0 | 1,
): array

Standardized Z score.

Calculates standardized z-scores by subtracting the mean and dividing by the standard deviation. Results have mean 0 and standard deviation 1.

Examples

Simple z-scores

import { assertEquals } from "jsr:@std/assert";

assertEquals(zscore([1, 2, 3, 4, 5]), [-1.2649110640673518, -0.6324555320336759, 0, 0.6324555320336759, 1.2649110640673518]);

Z-scores preserve relative distances

import { assertEquals } from "jsr:@std/assert";

assertEquals(zscore([10, 20, 30]), [-1, 0, 1]);

Matrix z-scores

import { assertEquals } from "jsr:@std/assert";

assertEquals(zscore([[1, 2], [3, 4]]), [[-0.7071067811865475, 0.7071067811865475], [-0.7071067811865475, 0.7071067811865475]]);

Parameters

Input array or matrix

optional
flag: 0 | 1

Normalization value (0: population, 1: sample). Default is 1

optional
dim: 0 | 1

Dimension along which to compute z-scores. Default is 0

Return Type

Standardized z-score values

Throws

When input is invalid

zscore(
x: matrix,
flag?: 0 | 1,
dim?: 0 | 1,
): matrix

Standardized Z score.

Calculates standardized z-scores by subtracting the mean and dividing by the standard deviation. Results have mean 0 and standard deviation 1.

Examples

Simple z-scores

import { assertEquals } from "jsr:@std/assert";

assertEquals(zscore([1, 2, 3, 4, 5]), [-1.2649110640673518, -0.6324555320336759, 0, 0.6324555320336759, 1.2649110640673518]);

Z-scores preserve relative distances

import { assertEquals } from "jsr:@std/assert";

assertEquals(zscore([10, 20, 30]), [-1, 0, 1]);

Matrix z-scores

import { assertEquals } from "jsr:@std/assert";

assertEquals(zscore([[1, 2], [3, 4]]), [[-0.7071067811865475, 0.7071067811865475], [-0.7071067811865475, 0.7071067811865475]]);

Parameters

Input array or matrix

optional
flag: 0 | 1

Normalization value (0: population, 1: sample). Default is 1

optional
dim: 0 | 1

Dimension along which to compute z-scores. Default is 0

Return Type

Standardized z-score values

Throws

When input is invalid