Statistics
Reference for statistical reduction operations.
These methods aggregate array values to compute statistical measures like sum, mean, variance, and standard deviation.
sum()
public function sum(?int $axis = null, bool $keepdims = false): float|int|NDArraySum of array elements over a given axis.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to sum. If null, sum over all elements. Optional. Default: null. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
float|int|NDArray- Scalar if axis is null, otherwise an NDArray.
Examples
$arr = NDArray::array([[1, 2, 3], [4, 5, 6]]);
// Sum all elements
$total = $arr->sum();
echo $total;
// Output: 21
// Sum along axis 0 (columns)
$col_sums = $arr->sum(axis: 0);
print_r($col_sums->toArray());
// Output: [5, 7, 9]
// Sum along axis 1 (rows)
$row_sums = $arr->sum(axis: 1);
print_r($row_sums->toArray());
// Output: [6, 15]
// Keep dimensions
$sums = $arr->sum(axis: 1, keepdims: true);
print_r($sums->shape());
// Output: [2, 1]mean()
public function mean(?int $axis = null, bool $keepdims = false): float|NDArrayMean of array elements over a given axis.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to compute mean. If null, compute mean of all elements. Optional. Default: null. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
float|NDArray- Scalar if axis is null, otherwise an NDArray.
Examples
$arr = NDArray::array([[1, 2, 3], [4, 5, 6]]);
echo $arr->mean();
// Output: 3.5
$row_means = $arr->mean(axis: 1);
print_r($row_means->toArray());
// Output: [2.0, 5.0]var()
public function var(?int $axis = null, int $ddof = 0, bool $keepdims = false): float|NDArrayVariance of array elements over a given axis.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to compute variance. If null, compute variance of all elements. Optional. Default: null. |
$ddof | int | Delta degrees of freedom (0 for population, 1 for sample). Optional. Default: 0. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
float|NDArray- Scalar if axis is null, otherwise an NDArray.
Examples
$arr = NDArray::array([1, 2, 3, 4, 5]);
echo $arr->var();
// Output: 2.0 (population variance)
echo $arr->var(ddof: 1);
// Output: 2.5 (sample variance)std()
public function std(?int $axis = null, int $ddof = 0, bool $keepdims = false): float|NDArrayStandard deviation of array elements over a given axis.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to compute std. If null, compute std of all elements. Optional. Default: null. |
$ddof | int | Delta degrees of freedom (0 for population, 1 for sample). Optional. Default: 0. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
float|NDArray- Scalar if axis is null, otherwise an NDArray.
Examples
$arr = NDArray::array([1, 2, 3, 4, 5]);
echo $arr->std();
// Output: 1.414... (population std)
echo $arr->std(ddof: 1);
// Output: 1.581... (sample std)min()
public function min(?int $axis = null, bool $keepdims = false): float|int|NDArrayMinimum of array elements over a given axis.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to find minimum. If null, find minimum of all elements. Optional. Default: null. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
float|int|NDArray- Scalar if axis is null, otherwise an NDArray.
Examples
$arr = NDArray::array([[1, 2, 3], [4, 5, 6]]);
echo $arr->min();
// Output: 1
$col_mins = $arr->min(axis: 0);
print_r($col_mins->toArray());
// Output: [1, 2, 3]max()
public function max(?int $axis = null, bool $keepdims = false): float|int|NDArrayMaximum of array elements over a given axis.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to find maximum. If null, find maximum of all elements. Optional. Default: null. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
float|int|NDArray- Scalar if axis is null, otherwise an NDArray.
Examples
$arr = NDArray::array([[1, 2, 3], [4, 5, 6]]);
echo $arr->max();
// Output: 6
$col_maxs = $arr->max(axis: 0);
print_r($col_maxs->toArray());
// Output: [4, 5, 6]product()
public function product(?int $axis = null, bool $keepdims = false): float|int|NDArrayProduct of array elements over a given axis.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to compute product. If null, compute product of all elements. Optional. Default: null. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
float|int|NDArray- Scalar if axis is null, otherwise an NDArray.
Examples
$arr = NDArray::array([[1, 2, 3], [4, 5, 6]]);
echo $arr->product();
// Output: 720
$row_prods = $arr->product(axis: 1);
print_r($row_prods->toArray());
// Output: [6, 120]any()
Test whether any element is true over a given axis.
public function any(?int $axis = null, bool $keepdims = false): bool|NDArrayFor non-bool arrays, zero is treated as false and non-zero as true. For complex numbers, the value is false if both real and imag parts are zero.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to check. If null, checks the entire array. Optional. Default: null. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
bool|NDArray- Scalar boolean if axis is null, otherwise an NDArray of bool.
Examples
$arr = NDArray::array([[0, 0], [0, 5]]);
// Check all elements
$result = $arr->any();
echo $result ? 'true' : 'false'; // true
// Check along axis 0 (columns)
$col_result = $arr->any(axis: 0);
print_r($col_result->toArray()); // [false, true]
// Check along axis 1 (rows)
$row_result = $arr->any(axis: 1);
print_r($row_result->toArray()); // [false, true]all()
Test whether all elements are true over a given axis.
public function all(?int $axis = null, bool $keepdims = false): bool|NDArrayFor non-bool arrays, zero is treated as false and non-zero as true. For complex numbers, the value is false if both real and imag parts are zero.
Parameters
| Parameter | Type | Description |
|---|---|---|
$axis | int|null | Axis along which to check. If null, checks the entire array. Optional. Default: null. |
$keepdims | bool | If true, the reduced axis is retained with size 1. Optional. Default: false. |
Returns
bool|NDArray- Scalar boolean if axis is null, otherwise an NDArray of bool.
Examples
$arr = NDArray::array([[1, 1], [1, 0]]);
// Check all elements
$result = $arr->all();
echo $result ? 'true' : 'false'; // false
// Check along axis 0 (columns)
$col_result = $arr->all(axis: 0);
print_r($col_result->toArray()); // [true, false]
// Check along axis 1 (rows)
$row_result = $arr->all(axis: 1);
print_r($row_result->toArray()); // [true, false]Summary Table
| Method | Description | Returns |
|---|---|---|
sum() | Sum of elements | Scalar or array |
mean() | Arithmetic mean | Scalar or array |
var() | Variance | Scalar or array |
std() | Standard deviation | Scalar or array |
min() | Minimum value | Scalar or array |
max() | Maximum value | Scalar or array |
product() | Product of elements | Scalar or array |
any() | Any element true | Scalar or array |
all() | All elements true | Scalar or array |
