博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
numpy.trace对于三维以上array的解析
阅读量:5864 次
发布时间:2019-06-19

本文共 3076 字,大约阅读时间需要 10 分钟。

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 npa = np.arange(8).reshape((2,2,2))print 'a =',aprint np.trace(a)x0 = a[0,0,0] + a[1,1,0]#即 0 + 6print 'a[1,1,0] =', a[1,1,0],';','x0 =',x0x1 = a[0,0,1] + a[1,1,1]#即 1 + 7print 'a[1,1,1] =', a[1,1,1],';','x1 =',x1#当然你可能认为6 = 2 + 4;或者 8 = 5 + 3,确实一个立方体应该是4条对角线,但是是不是都可以呢#请看下面的3-Db = np.array([ [ [100, 198],                 [2, 3]],               [ [4, 5],                [6,7]]])print 'b =',bprint np.trace(b)y0 = b[0,0,0] + b[1,1,0]#即 0 + 6print '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 + 7print '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 =', cprint '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).shapeprint '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)printtd10 = 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 = 6a[1,1,1] = 7 ; x1 = 8b = [[[100 198]  [  2   3]] [[  4   5]  [  6   7]]][106 205]y0 = b[0,0,0] + b[1,1,0] = 100 + 6 = 106y1 = b[0,0,1] + b[1,1,1] = 198 + 7 = 205c = [[2 8] [4 5]]trace = 7c[0,0] + c[1,1] = 2 + 5 = 7d = [[[[ 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 = 24td01 = d[0,0,0,1] + d[1,1,0,1] = 1 + 25 = 26td02 = d[0,0,0,2] + d[1,1,0,2] = 2 + 26 = 28td03 = d[0,0,0,3] + d[1,1,0,3] = 3 + 27 = 30td10 = d[0,0,1,0] + d[1,1,1,0] = 4 + 28 = 32td11 = d[0,0,1,1] + d[1,1,1,1] = 5 + 29 = 34td12 = d[0,0,1,2] + d[1,1,1,2] = 6 + 30 = 36td13 = d[0,0,1,3] + d[1,1,1,3] = 7 + 31 = 38

 

转载地址:http://muynx.baihongyu.com/

你可能感兴趣的文章
python 爬虫之爬取大街网(思路)
查看>>
mysql时间与日期函数
查看>>
hive学习5(复制表结构)
查看>>
在Win10 Anaconda中安装Tensorflow(转)
查看>>
WPF中使用MediaElement显示GIF
查看>>
安装pip和pylint
查看>>
ts 之 函数参数双向协变
查看>>
分布式session实现
查看>>
SQL Server Insert 操作效率(堆表 VS 聚集索引表)
查看>>
JS中的prototype、__proto__与constructor(图解)
查看>>
python用schedule模块实现定时任务
查看>>
java基础接口练习
查看>>
java 线程基本用法
查看>>
Log4j 配置
查看>>
正则表达式 反义
查看>>
将Tomcat注册为Windows服务
查看>>
LeetCode – Refresh – Maximum Depth of Binary Tree
查看>>
【JZOJ5915】明日之星
查看>>
ethereum/EIPs-191 Signed Data Standard
查看>>
NUC1177 排序【排序】
查看>>