距离矩阵的点坐标实现问题

2024-04-26 06:58:46 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:下面的代码没有提供预期的给定示例坐标。(什么算法?-请参考下面的链接)

我尝试实现这里描述的算法:finding the coordinates of points from distance matrix@math.stackexchange.com

代码:

# https://math.stackexchange.com/questions/156161/finding-the-coordinates-of-points-from-distance-matrix

import numpy as np
from scipy import spatial

example = np.array([[0,0],[1,3],[4,4]])
distance_matrix = spatial.distance_matrix(example,example)
 
def get_m_matrix(distance_matrix):
    d = distance_matrix
    shape = d.shape
    out = np.zeros(shape)
    for i in range(shape[0]):
        for j in range(shape[1]):
            out[i,j] = ( d[0,j]**2 + d[i,0]**2 - d[i,j]**2)/2
    return out
 
m_matrix = get_m_matrix(distance_matrix)
eigenvalue, eigenvectors = np.linalg.eig(m_matrix)
eigenvalue_matrix = np.diag(eigenvalue)
assert np.equal(np.round(np.dot(np.dot(eigenvectors, eigenvalue_matrix),np.linalg.inv(eigenvectors))),np.round(m_matrix)).all()
X = np.dot(eigenvectors, eigenvalue_matrix**0.5)
 
print('example',example,sep='\n')
print('distance_matrix',distance_matrix,sep='\n')
print('m_matrix',m_matrix,sep='\n')
print('eigenvalue',eigenvalue,sep='\n')
print('eigenvectors',eigenvectors,sep='\n')
print('eigenvalue_matrix',eigenvalue_matrix,sep='\n')
print('result',X,sep='\n')

我想我没有从它的描述中得到什么。我阅读并观看了一些关于特征值分解的视频。检查numpy文档中使用的函数。我非常确定M矩阵是好的(发现了一些其他问题,但algo有点不同,尽管M矩阵方程是一样的)。另一件事,我在youtube上的示例中检查了np.linalg.eig,发现特征向量不匹配。这个断言起了作用,但它仍然让我困惑

还有一件事,如果你在答案中使用等式,请描述变量。你不是在和专家说话,没有专家会读你的,因为他们知道。像孩子一样回答。谢谢


Tags: 代码from示例examplenpoutmatrixdot