【问题标题】:Measure distance between data set of 5D测量5D数据集之间的距离
【发布时间】:2015-03-07 07:43:10
【问题描述】:

我想测量 5 维数据集之间的距离(欧几里得)。 它看起来像这样:

                  center                                        x
0    [0.09771348879, 1.856078237, 2.100760575, 9.25...  [-1.35602640228e-12, -2.94706481441e-11, -6.51...
1    [8.006780488, 1.097849488, 0.6275244427, 0.572...  [4.99212418613, 5.01853294023, -0.014304672946...
2    [-1.40785823, -1.714959744, -0.5524032233, -0....  [-1.61000102139e-11, -4.680034138e-12, 1.96087...

index,然后是点(center),第三个是另一个点(x),所有的点都是5D的。 我想使用 pdist,因为它适用于 n-d。但问题是这些点在矩阵 X 中排列为 m n 维行向量。虽然我上面只有数据格式而不是矩阵,并且还包含不应该的索引。

我的代码是:(S是上面的格式)

S = pd.DataFrame(paired_data, columns=['x','center'])

print (S.to_string())

Y = pdist(S[1:], 'euclidean')
print Y

【问题讨论】:

    标签: python arrays matrix distance pdist


    【解决方案1】:

    这似乎有效:

    for i in range(S.shape[0]):
        M = np.matrix( [S['x'][i], S['center'][i]] )
        print pdist(M, 'euclidean')
    

    iterrows():

    for row in S.iterrows():
        M = np.matrix( [row[1]['x'], row[1]['center']] )
        print pdist(M, 'euclidean')
    

    请注意,不需要创建矩阵,pdist 可以很好地处理 python 列表:

    for row in S.iterrows():
        print pdist([row[1]['x'], row[1]['center']], 'euclidean')
    

    【讨论】:

    • 第一个似乎工作。谢谢。如果我想总结我最后得到的所有距离,我该怎么做?
    猜你喜欢
    • 2021-05-02
    • 2013-10-03
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多