Skip to content

How could I apply a function of multiple irregular fields without iterating along the axis? #1100

Answered by imdinu
imdinu asked this question in Q&A
Discussion options

You must be logged in to vote

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:

# 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 comparison

Suggestion

Wouldn't it make sense to have this be the behaviour of ak.mean(arr, axis=1) in this case?

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@jpivarski
Comment options

@imdinu
Comment options

@agoose77
Comment options

Answer selected by imdinu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants