我是Python新手。
这是我的三维数组:
my_data=numpy.zeros((index1,index2,index3))
为了便于说明,假设尺寸为:
index1 = 5
index2 = 4
index3 = 100
我想计算给定索引 2 值的所有索引值的平均值。
我尝试了各种选择:
# Does not work
result[index1][index2] = numpy.mean(my_data[index1][index2][index3], axis=2)
# Also does not work
result = numpy.zeros((index1, index2))
result[index1][index2] = numpy.mean(my_data[index1][index2])
我缺少什么?
只需使用np.mean()
与axis
关键词:
import numpy as np
np.random.seed(0)
data = np.random.randint(0,5,size=(3,3,3))
Yields:
[[[4 0 3]
[3 3 1]
[3 2 4]]
[[0 0 4]
[2 1 0]
[1 1 0]]
[[1 4 3]
[0 3 0]
[2 3 0]]]
然后申请:
np.mean(data,axis=1)
#Or data.mean(axis=1)
Returns:
[[3.33333333 1.66666667 2.66666667]
[1. 0.66666667 1.33333333]
[1. 3.33333333 1. ]]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)