-
I would like to be able to do array operations across different fields but I can't figure out how. Let's take the mean as an example. Here I have irregular size arrays for the x and y fields across different entries, but for each individual entry the length of those two arrays is the same. How could I compute the element-wise mean between "x" and "y" for each entry? data = [
{"x": np.array([1, 2, 3]), "y": np.array([2, 3, 4])},
{"x": np.array([5, 6]), "y": np.array([6, 7])},
{"x": np.array([0, 0, 0, 0]), "y":np.array([1, 1, 1, 1])}
]
arr = ak.Array(data) The the desired result would be: xy_mean = [
{"avg": [1.5, 2.5, 3.5]},
{"avg": [5.5, 6.5]},
{"avg": [0.5, 0.5, 0.5, 0.5]}
]
Is there any way of achieving this without iterating over each entry? Perhaps some clever uses of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Easy solution actuallyOk, I have tinkered around a bit more and realised the answer is really straight forward. I could just use vectorized addition and division: # vectorized operations
xy_mean = (arr["x"]+arr["y"])/2 This is easily readable and scales very well compared to iterating over each row: # iterative approach
def iterate(arr):
return ak.Array(
[np.mean([arr["x"][i], arr["y"][i]], axis=0)
for i in range(len(arr.layout))]) Time comparisonSuggestionWouldn't it make sense to have this be the behaviour of |
Beta Was this translation helpful? Give feedback.
Easy solution actually
Ok, I have tinkered around a bit more and realised the answer is really straight forward. I could just use vectorized addition and division:
This is easily readable and scales very well compared to iterating over each row:
Time comparison
Suggestion
Wouldn't it make sense to have this be the behaviour of
ak.mean(arr, axis=1)
in this case?