用numpy最快的方式对内存最少的上三角元素求和 [英] Fastest way in numpy to sum over upper triangular elements with the least memory

查看:99
本文介绍了用numpy最快的方式对内存最少的上三角元素求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对对称矩阵执行类型i<j的求和.这等于对矩阵的上三角元素求和,不包括对角线.

I need to perform a summation of the kind i<j on symmetric matrices. This is equivalent to sum over the upper triangular elements of a matrix, diagonal excluded.

给出A一个对称的N x N数组,最简单的解决方案是np.triu(A,1).sum(),但是我想知道是否存在更快的方法,它们需要更少的内存. 似乎在大型数组上(A.sum() - np.diag(A).sum())/2更快,但是如何避免从np.diag创建甚至N x 1数组呢? 双重嵌套的for循环不需要额外的内存,但是显然这不是在Python中使用的方式.

Given A a symmetric N x N array, the simplest solution is np.triu(A,1).sum() however I was wondering if faster methods exist that require less memory. It seems that (A.sum() - np.diag(A).sum())/2 is faster on large array, but how to avoid creating even the N x 1 array from np.diag? A doubly nested for loop would require no additional memory, but it is clearly not the way to go in Python.

推荐答案

您可以将np.diag(A).sum()替换为np.trace(A);这不会创建临时的Nx1数组

You can replace np.diag(A).sum() with np.trace(A); this will not create the temporary Nx1 array

这篇关于用numpy最快的方式对内存最少的上三角元素求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆