You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/user-stories/overlaps.md
+13-5Lines changed: 13 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,9 @@ kernelspec:
13
13
14
14
# Overlapping Groups
15
15
16
-
Generally group-by problems involve non-overlapping groups. Consider the following group of labels:
16
+
Generally group-by problems involve non-overlapping groups.
17
+
18
+
Consider the following group of labels:
17
19
18
20
```{code-cell}
19
21
import numpy as np
@@ -40,21 +42,27 @@ we get (note the reduction over `x` is implicit here):
40
42
xarray_reduce(da, labels, func="sum")
41
43
```
42
44
43
-
Now let's calculate the `sum` where `labels` is either `1` or `2`. The trick is to add a new dimension to `labels` of size `2` and assign a new label `4` in the appropriate locations.
45
+
Now let's _also_ calculate the `sum` where `labels` is either `1` or `2`.
46
+
We could easily compute this using the grouped result but here we use this simple example for illustration.
47
+
The trick is to add a new dimension with new labels (here `4`) in the appropriate locations.
44
48
```{code-cell}
45
-
# assign expanded=4 where label == 1 or 2, and -1 otherwise
49
+
# assign 4 where label == 1 or 2, and -1 otherwise
Now we reduce over `x`_and_`y` (again implicitly) to get the appropriate sum under `label=4` (and `label=-1`). We can discard the value accumulated under `label=-1` later.
58
+
Now we reduce over `x`_and_ the new dimension `y` (again implicitly) to get the appropriate sum under
59
+
`label=4` (and `label=-1`). We can discard the value accumulated under `label=-1` later.
54
60
```{code-cell}
55
61
xarray_reduce(da, expanded, func="sum")
56
62
```
57
63
64
+
This way we compute all the reductions we need, in a single pass over the data.
65
+
58
66
This technique generalizes to more complicated aggregations. The trick is to
59
67
- generate appropriate labels
60
68
- concatenate these new labels along a new dimension (`y`) absent on the object being reduced (`da`), and
0 commit comments