numpy.trace對於三維以上array的解析

夢書發表於2018-03-21

numpy.trace是求shape的對角線上的元素的和,具體看 https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.trace.html

或者搜尋 numpy.trace, 二維的比較好理解,對於三維以上的對角線(三維的對角線不止2條,該選哪兩條呢)就不好理解了,以下是本人的理解

# 3-D array 的trace演算法 

import numpy as np

a = np.arange(8).reshape((2,2,2))
print 'a =',a

print np.trace(a)

x0 = a[0,0,0] + a[1,1,0]#即 0 + 6
print 'a[1,1,0] =', a[1,1,0],';','x0 =',x0
x1 = a[0,0,1] + a[1,1,1]#即 1 + 7
print 'a[1,1,1] =', a[1,1,1],';','x1 =',x1

#當然你可能認為6 = 2 + 4;或者 8 = 5 + 3,確實一個立方體應該是4條對角線,但是是不是都可以呢
#請看下面的3-D
b = np.array([ [ [100, 198],
                 [2, 3]],
               [ [4, 5],
                [6,7]]])
print 'b =',b
print np.trace(b)
y0 = b[0,0,0] + b[1,1,0]#即 0 + 6
print 'y0 = b[0,0,0] + b[1,1,0] = %d + %d = %d' % (b[0,0,0], b[1,1,0], y0)
y1 = b[0,0,1] + b[1,1,1]#即 1 + 7
print 'y1 = b[0,0,1] + b[1,1,1] = %d + %d = %d' % (b[0,0,1], b[1,1,1], y1)

#事實證明只能是其中的固定方向的
#之所以能以此種方式思考是因為2-D的直觀

c = np.array([[2, 8],
             [4,5]])
print 'c =', c
print 'trace =', np.trace(c)
trace_c = c[0,0] + c[1,1]
print 'c[0,0] + c[1,1] = %d + %d = %d' % (c[0,0], c[1,1], trace_c)

#來看看4-D的
d = np.arange(32).reshape((2,2,2,4))
print 'd =', d
#猜猜看這個trace結果是什麼shape,
#(2, 4),只要去掉前面 2個維度即可
print 'np.trace(d).shape =', np.trace(d).shape
print 'np.trace(d) =', np.trace(d)
td00 = d[0,0,0,0] + d[1,1,0,0]
print 'td00 = ' + 'd[0,0,0,0] + d[1,1,0,0] = %d + %d = %d' % (d[0,0,0,0], d[1,1,0,0], td00)
td01 = d[0,0,0,1] + d[1,1,0,1]
print 'td01 = ' + 'd[0,0,0,1] + d[1,1,0,1] = %d + %d = %d' % (d[0,0,0,1], d[1,1,0,1], td01)
td02 = d[0,0,0,2] + d[1,1,0,2]
print 'td02 = ' + 'd[0,0,0,2] + d[1,1,0,2] = %d + %d = %d' % (d[0,0,0,2], d[1,1,0,2], td02)
td03 = d[0,0,0,3] + d[1,1,0,3]
print 'td03 = ' + 'd[0,0,0,3] + d[1,1,0,3] = %d + %d = %d' % (d[0,0,0,3], d[1,1,0,3], td03)

print
td10 = d[0,0,1,0] + d[1,1,1,0]
print 'td10 = ' + 'd[0,0,1,0] + d[1,1,1,0] = %d + %d = %d' % (d[0,0,1,0], d[1,1,1,0], td10)
td11 = d[0,0,1,1] + d[1,1,1,1]
print 'td11 = ' + 'd[0,0,1,1] + d[1,1,1,1] = %d + %d = %d' % (d[0,0,1,1], d[1,1,1,1], td11)
td12 = d[0,0,1,2] + d[1,1,1,2]
print 'td12 = ' + 'd[0,0,1,2] + d[1,1,1,2] = %d + %d = %d' % (d[0,0,1,2], d[1,1,1,2], td12)
td13 = d[0,0,1,3] + d[1,1,1,3]
print 'td13 = ' + 'd[0,0,1,3] + d[1,1,1,3] = %d + %d = %d' % (d[0,0,1,3], d[1,1,1,3], td13)

 

以下是執行結果:(python 2.7, numpy:1.14.2:

a = [[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]
[6 8]
a[1,1,0] = 6 ; x0 = 6
a[1,1,1] = 7 ; x1 = 8
b = [[[100 198]
  [  2   3]]

 [[  4   5]
  [  6   7]]]
[106 205]
y0 = b[0,0,0] + b[1,1,0] = 100 + 6 = 106
y1 = b[0,0,1] + b[1,1,1] = 198 + 7 = 205
c = [[2 8]
 [4 5]]
trace = 7
c[0,0] + c[1,1] = 2 + 5 = 7
d = [[[[ 0  1  2  3]
   [ 4  5  6  7]]

  [[ 8  9 10 11]
   [12 13 14 15]]]


 [[[16 17 18 19]
   [20 21 22 23]]

  [[24 25 26 27]
   [28 29 30 31]]]]
np.trace(d).shape = (2, 4)
np.trace(d) = [[24 26 28 30]
 [32 34 36 38]]
td00 = d[0,0,0,0] + d[1,1,0,0] = 0 + 24 = 24
td01 = d[0,0,0,1] + d[1,1,0,1] = 1 + 25 = 26
td02 = d[0,0,0,2] + d[1,1,0,2] = 2 + 26 = 28
td03 = d[0,0,0,3] + d[1,1,0,3] = 3 + 27 = 30

td10 = d[0,0,1,0] + d[1,1,1,0] = 4 + 28 = 32
td11 = d[0,0,1,1] + d[1,1,1,1] = 5 + 29 = 34
td12 = d[0,0,1,2] + d[1,1,1,2] = 6 + 30 = 36
td13 = d[0,0,1,3] + d[1,1,1,3] = 7 + 31 = 38

 

相關文章