问题描述
我正在尝试按照以下帖子(多维缩放)中指定的方法从距离矩阵中获取 0 到 1 之间的二维坐标:
但是,在尝试实现它时出现错误,并且我很难找到此错误的根源。这就是我正在做的:
这是距离矩阵(如你所见,最大距离为1,所以所有的点都可以在0到1之间):
import numpy as np
import math
distance_matrix = np.array(
[
[0.0,0.47659458,0.22173311,0.46660708,0.78423276],[0.47659458,0.0,0.69805139,0.01200111,0.6629441],[0.22173311,0.68249177,1.0],[0.46660708,0.6850815],[0.78423276,0.6629441,1.0,0.6850815,0.0],]
)
这是我进行多维缩放的地方:
def x_coord_of_point(D,j):
return ( D[0,j]**2 + D[0,1]**2 - D[1,j]**2 ) / ( 2*D[0,1] )
def coords_of_point(D,j):
x = x_coord_of_point(D,j)
return np.array([x,math.sqrt( D[0,j]**2 - x**2 )])
def calculate_positions(D):
(m,n) = D.shape
P = np.zeros( (n,2) )
tr = ( min(min(D[2,0:2]),min(D[2,3:n])) / 2)**2
P[1,0] = D[0,1]
P[2,:] = coords_of_point(D,2)
for j in range(3,n):
P[j,j)
if abs( np.dot(P[j,:] - P[2,:],P[j,:]) - D[2,j]**2 ) > tr:
P[j,1] = - P[j,1]
return P
P = calculate_positions(distance_matrix)
print(P)
Output: [[ 0. 0. ]
[ 0.47659458 0. ]
[-0.22132834 0.01339166]
[ 0.46656063 0.0065838 ]
[ 0.42244347 -0.66072879]]
我这样做是为了使所有点都介于 0 和 1 之间:
P = P-P.min(axis=0)
一旦我有了一组应该满足距离矩阵的点,我就从这些点计算距离矩阵,看看它是否等于原始点:
def compute_dist_matrix(P):
dist_matrix = []
for i in range(len(P)):
lis_pos = []
for j in range(len(P)):
dist = np.linalg.norm(P[i]-P[j])
lis_pos.append(dist)
dist_matrix.append(lis_pos)
return np.array(dist_matrix)
compute_dist_matrix(P)
Output: array([[0.,0.,0.6629441 ],0.68792266,0.93213762],0.66876934],0.93213762,0.66876934,0. ]])
如您所见,如果我们将这个数组与帖子开头的原始距离矩阵进行比较,则矩阵的第一项没有错误,但是越接近结尾,错误就越大和更大。如果距离矩阵比我在这个例子中使用的矩阵大,那么误差就会变得很大。
我不知道错误的来源是计算 P 的函数还是函数“compute_dist_matrix”有问题。
你能找出错误的来源吗?或者,有没有更简单的方法来计算所有这些?也许某些库中的某些函数已经执行了这种转换。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)