[英]Symmetric matrices in numpy?
I wish to initiate a symmetric matrix in python and populate it with zeros.我希望在 python 中启动一个对称矩阵并用零填充它。
At the moment, I have initiated an array of known dimensions but this is unsuitable for subsequent input into R as a distance matrix.目前,我已经启动了一个已知维度的数组,但这不适合随后作为距离矩阵输入到 R 中。
Are there any 'simple' methods in numpy to create a symmetric matrix? numpy 中是否有任何“简单”方法来创建对称矩阵?
Edit编辑
I should clarify - creating the 'symmetric' matrix is fine.我应该澄清 - 创建“对称”矩阵很好。 However I am interested in only generating the lower triangular form, ie.,
但是我只对生成下三角形感兴趣,即,
ar = numpy.zeros((3, 3))
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
I want:我想:
array([[ 0],
[ 0, 0 ],
[ 0., 0., 0.]])
Is this possible?这可能吗?
I don't think it's feasible to try work with that kind of triangular arrays.我认为尝试使用这种三角形阵列是不可行的。
So here is for example a straightforward implementation of (squared) pairwise Euclidean distances:因此,例如,这是(平方)成对欧几里德距离的简单实现:
def pdista(X):
"""Squared pairwise distances between all columns of X."""
B= np.dot(X.T, X)
q= np.diag(B)[:, None]
return q+ q.T- 2* B
For performance wise it's hard to beat it (in Python level).就性能而言,很难击败它(在 Python 级别)。 What would be the main advantage of not using this approach?
不使用这种方法的主要优势是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.