關於 numpy axis
The way the axis is specified here can be confusing to users coming from other languages.
The axis
keyword specifies the dimension of the array that will be collapsed, rather than the dimension that will be returned.
So specifying axis=0
means that the first axis will be collapsed: for two-dimensional arrays, this means that values within each column will be aggregated.
比如:
M=[[ 0.8967576 0.03783739 0.75952519 0.06682827]
[ 0.8354065 0.99196818 0.19544769 0.43447084]
[ 0.66859307 0.15038721 0.37911423 0.6687194 ]]
M.min(axis=0)
array([ 0.66859307, 0.03783739, 0.19544769, 0.06682827])
M.max(axis=1)
array([ 0.8967576 , 0.99196818, 0.6687194 ])
如果axis=0,那麼輸出矩陣是1行,求每一列的平均(按照每一行去求平均);axis=1,輸出矩陣是1列,求每一行的平均(按照每一列去求平均)
還可以這麼理解,axis是所表示的維度被壓縮成1。