1

Out of curiosity, is there a specific numpy function to do the following (which would supposedly be faster):

a = np.array((0,2,4))
b = np.zeros(len(a) - 1)
for i in range(len(b)):
    b[i] = a[i:i+2].mean()

print(b)
#prints [1,3]

Cheers

0

1 Answer 1

7

You could use

b = (a[1:] + a[:-1]) / 2.

to avoid the Python loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Neat, I like it :-) I guess I have not worked with numpy for too long! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.