简体   繁体   中英

To average 2-D numpy.array with numpy.mean or numpy.average

I am trying to average two-dimensional numpy arrays. So, I used numpy.mean but the result is the empty array.

import numpy as np
ws1 = np.array(ws1)
ws1_I8 = np.array(ws1_I8) 
ws1_I10 = np.array(ws1_I10)
WSAV = np.mean([ws1,ws1_I8,ws1_I10])
print WSAV

I used both np.mean and np.average but the result is same as the empty array. The each of ws1 , ws1_I8 , ws1_I10 has the shape of (18, 75) and I would like to have the result array of (18, 75) shape.

Any idea or help would be really appreciated.

If ws1 , ws1_I8 and ws1_I10 all have shape (18, 75), then np.mean([ws1, ws1_I8, ws1_I10]) should return mean of all the values in all the arrays. (I'm not sure what you mean by "the result is same as the empty array".) np.mean will convert [ws1, ws1_I8, ws1_I10] into a 3-d array with shape (3, 18, 75). To get np.mean to average only along the first axis, use the argument axis=0 :

WSAV = np.mean([ws1, ws1_I8, ws1_I10], axis=0)

Or, you could simply write:

WSAV = (ws1 + ws1_I8 + ws1_I10) / 3.0

It sounds like this would do it for you:

average_list = [ws1, ws1_I8, ws1_I10]
WSAV = sum(average_list) / len(average_list)

Assuming that what you want in your case is (ws1+ws1_I8+ws1_I10)/3 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM