landlab.data_record.aggregators¶
- aggregate_items_as_count(ids, size=None)[source]¶
Count the number of time an id appears in an array.
- Parameters:
- Returns:
The number of times each id appears.
- Return type:
ndarray of int
Examples
>>> from landlab.data_record.aggregators import aggregate_items_as_count >>> aggregate_items_as_count([1, 2, 3, 3, 1, 5]) array([0, 2, 1, 2, 0, 1]) >>> aggregate_items_as_count([1, 2, 3, 3, 1, 5], size=8) array([0, 2, 1, 2, 0, 1, 0, 0])
Negative ids are ignored.
>>> aggregate_items_as_count([1, 2, 3, 3, -1, 5]) array([0, 1, 1, 2, 0, 1])
- aggregate_items_as_gmean(ids, values, *, weights=None, where=None, out=None)[source]¶
Compute a weighted geometric mean of values grouped by integer IDs.
Group
valuesbyidsand compute the weighted geometric mean for each group.- Parameters:
ids (array_like of int, shape (n,)) – Integer group labels for each value. Negative IDs are ignored.
values (array_like, shape (n,)) – Values to aggregate. Must be strictly positive.
weights (array_like, shape (n,), optional) – Weights associated with each value. If not provided, all weights are taken to be 1.
where (array_like of bool, shape (n,), optional) – Boolean mask indicating which items to include. If not provided, all items are included.
out (ndarray of float, shape (n_groups,), optional) – Output array. If provided, results are written in-place.
- Returns:
out – Weighted geometric mean for each group. Elements with no valid items are left unchanged in
outif provided, or contain uninitialized values ifoutwas created internally.- Return type:
Examples
>>> import numpy as np
>>> ids = [0, 0, 1, 1, 1] >>> values = [1.0, 4.0, 1.0, 3.0, 9.0] >>> weights = [1.0, 1.0, 1.0, 2.0, 1.0]
>>> aggregate_items_as_gmean(ids, values, weights=weights) array([2., 3.])
Use
whereto filter items:>>> where = [True, True, False, False, True] >>> aggregate_items_as_gmean(ids, values, where=where) array([2., 9.])
Reuse an output array:
>>> out = np.full(2, np.nan) >>> where = [True, True, False, False, False] >>> aggregate_items_as_gmean(ids, values, out=out, where=where) array([ 2., nan]) >>> out array([ 2., nan])
- aggregate_items_as_mean(ids, values, weights=None, size=None)[source]¶
Find the mean of values associated with an id.
- Parameters:
- Returns:
The mean of the values at each id.
- Return type:
ndarray of int
Examples
>>> from landlab.data_record.aggregators import aggregate_items_as_mean >>> aggregate_items_as_mean([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5]) array([1.5, 3. , 0. , 3. , 1. , 5. ]) >>> aggregate_items_as_mean([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5], size=8) array([1.5, 3. , 0. , 3. , 1. , 5. , 0. , 0. ])
Negative ids are ignored.
>>> aggregate_items_as_mean([0, -1, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5]) array([1., 3., 0., 3., 1., 5.])
- aggregate_items_as_sum(ids, values, size=None)[source]¶
Find the sum of values associated with an id.
- Parameters:
- Returns:
The sum of the values at each id.
- Return type:
ndarray of int
Examples
>>> from landlab.data_record.aggregators import aggregate_items_as_sum >>> aggregate_items_as_sum([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5]) array([3., 3., 0., 3., 1., 5.]) >>> aggregate_items_as_sum([0, 0, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5], size=8) array([3., 3., 0., 3., 1., 5., 0., 0.])
Negative ids are ignored.
>>> aggregate_items_as_sum([0, -1, 1, 3, 4, 5], [1, 2, 3, 3, 1, 5]) array([1., 3., 0., 3., 1., 5.])