26

I Want to write the code below as Pythonic way, applying mean over two axis. What the best way to do this?

import numpy as np

m = np.random.rand(30, 10, 10)  
m_mean = np.zeros((30, 1))    
for j in range(30):
    m_mean[j, 0] = m[j, :, :].mean()
3
  • Why is m_mean a 2D array? Commented Nov 5, 2015 at 18:34
  • 1
    why create a second numpy array with only one row? why not just have a simple list? Commented Nov 5, 2015 at 18:34
  • 6
    NumPythonic way would be m.mean(axis=(1,2)). Commented Nov 5, 2015 at 18:35

2 Answers 2

47

If you have a sufficiently recent NumPy, you can do

m_mean = m.mean(axis=(1, 2))

I believe this was introduced in 1.7, though I'm not sure. The documentation was only updated to reflect this in 1.10, but it worked earlier than that.

If your NumPy is too old, you can take the mean a bit more manually:

m_mean = m.sum(axis=2).sum(axis=1) / np.prod(m.shape[1:3])

These will both produce 1-dimensional results. If you really want that extra length-1 axis, you can do something like m_mean = m_mean[:, np.newaxis] to put the extra axis there.

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

2 Comments

With older numpy, you can also just do m.mean(axis=2).mean(axis=1)
@Rob: Ah, you're right. Since all the means in the m.mean(axis=2) step are over the same number of elements, m.mean(axis=2).mean(axis=1) correctly gives each element equal weight.
2

You can also use the numpy.mean() ufunc and pass the output array as an argument to out= as in:

np.mean(m, axis=(1, 2), out=m_mean)

Comments

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.