将距离矩阵可视化为图形 [英] Visualize distance matrix as a graph

查看:623
本文介绍了将距离矩阵可视化为图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行聚类任务,并且我有一个距离矩阵.我希望将此距离矩阵可视化为2D图.请让我知道是否有任何在线方式或使用R或python之类的编程语言来实现此目的. 我的距离矩阵如下 我使用了经典的多维比例缩放功能(在R中),并获得了如下所示的2D图: 但是我要寻找的是一个在节点之间有加权边的图.

I am doing a clustering task and I have a distance matrix. I wish to visualize this distance matrix as a 2D graph. Please let me know if there is any way to do it online or in programming languages like R or python. My distance matrix is as follows, I used the classical Multidimensional scaling functionality (in R) and obtained a 2D plot that looks like: But What I am looking for is a graph with nodes and weighted edges running between them.

推荐答案

可能性1

我假设您想要一个二维图,其中节点位置之间的距离与表中提供的距离相同..

在python中,您可以将networkx用于此类应用程序.通常,这样做的方法很多,请记住,所有这些方法都是近似的(通常,鉴于成对的距离,不可能创建点的二维表示).它们是某种应力最小化(或能量). -minimization)近似值,尝试找到与 个相似距离的合理"表示形式.

In python, you can use networkx for such applications. In general there are manymethods of doing so, remember, that all of them are just approximations (as in general it is not possible to create a 2 dimensional representataion of points given their pairwise distances) They are some kind of stress-minimizatin (or energy-minimization) approximations, trying to find the "reasonable" representation with similar distances as those provided.

作为示例,您可以考虑一个四点示例(应用了正确的离散量度):

As an example you can consider a four point example (with correct, discrete metric applied):

     p1 p2 p3 p4
  ---------------
  p1  0  1  1  1
  p2  1  0  1  1
  p3  1  1  0  1
  p4  1  1  1  0

通常,绘制实际的图形"是多余的,因为您已经完全连接了一个(每对节点都已连接),因此仅绘制点就足够了.

In general, drawing actual "graph" is redundant, as you have fully connected one (each pair of nodes is connected) so it should be sufficient to draw just points.

Python示例

import networkx as nx
import numpy as np
import string

dt = [('len', float)]
A = np.array([(0, 0.3, 0.4, 0.7),
               (0.3, 0, 0.9, 0.2),
               (0.4, 0.9, 0, 0.1),
               (0.7, 0.2, 0.1, 0)
               ])*10
A = A.view(dt)

G = nx.from_numpy_matrix(A)
G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())),string.ascii_uppercase)))    

G = nx.to_agraph(G)

G.node_attr.update(color="red", style="filled")
G.edge_attr.update(color="blue", width="2.0")

G.draw('distances.png', format='png', prog='neato')

在R中,您可以尝试多维缩放

# Classical MDS
# N rows (objects) x p columns (variables)
# each row identified by a unique row name

d <- dist(mydata) # euclidean distances between the rows
fit <- cmdscale(d,eig=TRUE, k=2) # k is the number of dim
fit # view results

# plot solution 
x <- fit$points[,1]
y <- fit$points[,2]
plot(x, y, xlab="Coordinate 1", ylab="Coordinate 2", 
  main="Metric  MDS",    type="n")
text(x, y, labels = row.names(mydata), cex=.7)

可能性2

您只想绘制带有标记边缘的图形

同样,networkx可以提供帮助:

import networkx as nx   

# Create a graph
G = nx.Graph()

# distances
D = [ [0, 1], [1, 0] ]

labels = {}
for n in range(len(D)):
    for m in range(len(D)-(n+1)):
        G.add_edge(n,n+m+1)
        labels[ (n,n+m+1) ] = str(D[n][n+m+1])

pos=nx.spring_layout(G)

nx.draw(G, pos)
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels,font_size=30)

import pylab as plt
plt.show()

这篇关于将距离矩阵可视化为图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆