本文介绍了numpy:将数组转换为三角矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种将线性数组转换为三角矩阵的内置方法.当我找不到一个时,我正在寻求实施一个的帮助.
I was looking for a built in method to convert an linear array to triangular matrix. As I failed in find one I am asking for help in implementing one.
想象一个像这样的数组:
Imagine an array like:
In [203]: dm
Out[203]: array([ 0.80487805, 0.90243902, 0.85365854, ..., 0.95121951,
0.90243902, 1. ])
In [204]: dm.shape
Out[204]: (2211,)
我想将此数组转换为三角形矩阵或对称矩形矩阵.
And I would like to convert this array to a an triangular matrix or a symmetric rectangular matrix.
In [205]: reshapedDm = dm.trian_reshape(67, 67)
我如何实现trian_reshape作为从一维数组返回三角矩阵的函数?
How I would implement trian_reshape as function that returns an triangular matrix from 1-D array?
推荐答案
>>> tri = np.zeros((67, 67))
>>> tri[np.triu_indices(67, 1)] = dm
有关详细信息,请参见文档以获取triu_indices
.要获取低三角矩阵,请使用np.tril_indices
并将偏移量设置为-1
而不是1
.
See doc for triu_indices
for details. To get a lower-triangular matrix, use np.tril_indices
and set the offset to -1
instead of 1
.
这篇关于numpy:将数组转换为三角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!