当前位置:   article > 正文

python中mean函数lon.mean()_在numpy中使用.mean()

python mean(-1)

以下是一些例子,我希望这些例子能解释发生了什么:In [191]:

#the data

a=np.random.random((3,3,3))

print a

[[[ 0.21715561 0.23384392 0.21248607]

[ 0.10788638 0.61387072 0.56579586]

[ 0.6027137 0.77929822 0.80993495]]

[[ 0.36330373 0.26790271 0.79011397]

[ 0.01571846 0.99187387 0.1301911 ]

[ 0.18856381 0.09577381 0.03728304]]

[[ 0.18849473 0.16550599 0.41999887]

[ 0.65009076 0.39260551 0.92284577]

[ 0.92642505 0.46513472 0.77273484]]]

In [192]:

#mean() returns the grand mean

a.mean()

Out[192]:

0.44176096869094533

In [193]:

#mean(0) returns the mean along the 1st axis

a.mean(0)

Out[193]:

array([[ 0.25631803, 0.22241754, 0.47419964],

[ 0.25789853, 0.6661167 , 0.53961091],

[ 0.57256752, 0.44673558, 0.53998427]])

In [195]:

#what is this doing?

a.mean(-1)

Out[195]:

array([[ 0.22116187, 0.42918432, 0.73064896],

[ 0.47377347, 0.37926114, 0.10720688],

[ 0.25799986, 0.65518068, 0.72143154]])

In [196]:

#it is returning the mean along the last axis, in this case, the 3rd axis

a.mean(2)

Out[196]:

array([[ 0.22116187, 0.42918432, 0.73064896],

[ 0.47377347, 0.37926114, 0.10720688],

[ 0.25799986, 0.65518068, 0.72143154]])

In [197]:

#Ok, this is now clear: calculate the mean along the 1st axis first, then calculate the mean along the last axis of the resultant.

a.mean(0).mean(-1)

Out[197]:

array([ 0.31764507, 0.48787538, 0.51976246])

在我看来,使用T作为变量名可能不是一个好主意。.T()表示numpy中的转置。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/360862
推荐阅读
相关标签
  

闽ICP备14008679号