如何在NumPy中将三角矩阵转换为正方形? [英] How to convert triangle matrix to square in NumPy?

查看:222
本文介绍了如何在NumPy中将三角矩阵转换为正方形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对一个冗余的完整矩阵进行一些计算(即可以是一个三角形矩阵而不会丢失任何信息).我意识到我只能计算三角形的下部以获得更快的结果.完成后如何将下三角投影到上三角?

I'm doing some computations on a full matrix that is redundant (i.e. can be a triangle matrix without losing info). I realized I can compute only the lower portion of the triangle for faster results. How can I project the lower triangle into the upper once I'm done?

换句话说,我该如何反转np.tril方法?

In other words, how can I reverse the np.tril method?

print DF_var.as_matrix()
# [[1 1 0 1 1 1 0 1 0 0 0]
#  [1 1 1 1 1 0 1 0 1 1 1]
#  [0 1 1 0 0 0 0 0 0 0 0]
#  [1 1 0 1 0 0 0 0 0 0 0]
#  [1 1 0 0 1 0 0 0 0 0 0]
#  [1 0 0 0 0 1 1 0 0 0 0]
#  [0 1 0 0 0 1 1 0 0 0 0]
#  [1 0 0 0 0 0 0 1 1 0 0]
#  [0 1 0 0 0 0 0 1 1 0 0]
#  [0 1 0 0 0 0 0 0 0 1 0]
#  [0 1 0 0 0 0 0 0 0 0 1]]
print np.tril(DF_var.as_matrix())
# [[1 0 0 0 0 0 0 0 0 0 0]
#  [1 1 0 0 0 0 0 0 0 0 0]
#  [0 1 1 0 0 0 0 0 0 0 0]
#  [1 1 0 1 0 0 0 0 0 0 0]
#  [1 1 0 0 1 0 0 0 0 0 0]
#  [1 0 0 0 0 1 0 0 0 0 0]
#  [0 1 0 0 0 1 1 0 0 0 0]
#  [1 0 0 0 0 0 0 1 0 0 0]
#  [0 1 0 0 0 0 0 1 1 0 0]
#  [0 1 0 0 0 0 0 0 0 1 0]
#  [0 1 0 0 0 0 0 0 0 0 1]]

如何将其转换回完整矩阵?

How to convert it back to a full matrix?

推荐答案

假定A作为输入数组,下面列出了几种方法.

Assuming A as the input array, few methods are listed below.

方法#1::在A-

np.triu(A.T,1) + A

方法2:避免np.triu进行A.T和A之间的直接求和,然后索引以设置对角线元素-

Approach #2 : Avoid np.triu with direct summation between A.T and A and then indexing to set diagonal elements -

out = A.T + A
idx = np.arange(A.shape[0])
out[idx,idx] = A[idx,idx]

方法3::与上一个方法相同,但使用内置索引进行压缩-

Approach #3 : Same as previous one, but compact using in-builts for indexing -

out = A.T + A
np.fill_diagonal(out,np.diag(A))

方法4:与上一个方法相同,但具有布尔索引来设置对角线元素-

Approach #4 : Same as previous one, but with boolean indexing to set diagonal elements -

out = A.T + A
mask = np.eye(out.shape[0],dtype=bool)
out[mask] = A[mask]

方法5:np.where-

np.where(np.eye(A.shape[0],dtype=bool),A,A.T+A)

方法6::对所有具有np.where-

np.where(np.triu(np.ones(A.shape[0],dtype=bool),1),A.T,A)


运行时测试

功能-

def func1(A):
    return np.triu(A.T,1) + A

def func2(A):
    out = A.T + A
    idx = np.arange(A.shape[0])
    out[idx,idx] = A[idx,idx]
    return out

def func3(A):
    out = A.T + A
    np.fill_diagonal(out,np.diag(A))
    return out

def func4(A):
    out = A.T + A
    mask = np.eye(out.shape[0],dtype=bool)
    out[mask] = A[mask]
    return out

def func5(A):
    return np.where(np.eye(A.shape[0],dtype=bool),A,A.T+A)

def func6(A):
    return np.where(np.triu(np.ones(A.shape[0],dtype=bool),1),A.T,A)

时间-

In [140]: # Input array
     ...: N = 5000
     ...: A = np.tril(np.random.randint(0,9,(N,N)))
     ...: 

In [141]: %timeit func1(A)
     ...: %timeit func2(A)
     ...: %timeit func3(A)
     ...: %timeit func4(A)
     ...: %timeit func5(A)
     ...: %timeit func6(A)
     ...: 
1 loops, best of 3: 617 ms per loop
1 loops, best of 3: 354 ms per loop
1 loops, best of 3: 354 ms per loop
1 loops, best of 3: 395 ms per loop
1 loops, best of 3: 597 ms per loop
1 loops, best of 3: 440 ms per loop

看起来像方法2& #3效率很高!

Looks like the approaches # 2 & #3 are pretty efficient!

这篇关于如何在NumPy中将三角矩阵转换为正方形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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