2017-08-30 62 views
1

我已经计算了文档距离,并且在sklearn中使用MDS以使用matplotlib绘制它们。我想用seaborn(pairplot)绘制它们,但不知道如何翻译MDS数据以便它可以被seaborn读取。如何格式化数据以供seaborn使用

from sklearn.manifold import MDS 

mds = MDS(n_components=2, dissimilarity="precomputed", random_state=1) 
pos = mds.fit_transform(dist) 
xs, ys = pos[:, 0], pos[:, 1] 

names = [name for name in labels] 

# Define the plot 
for x, y, name in zip(xs, ys, names): 
    plt.scatter(x, y, color=color) 
    plt.text(x, y, name) 


plt.show() 

回答

1

the documentation for pairplot()说明,本函数需要一个长格式数据帧,其中每列是一个变量,并且每个行是一个观察。 最简单的就是使用Pandas构造这个数据框(尽管我相信一个numpy数组可以工作)。

长形式的数据框的行数与观察值一样多,每列都是一个变量。 seaborn的功能是使用分类列来拆分数据帧是不同的组。

在你的情况下,数据帧可能会是这样的:

X   Y   label 
0 0.094060 0.484758 Label_00 
1 0.375537 0.150206 Label_00 
2 0.215755 0.796629 Label_02 
3 0.204077 0.921016 Label_01 
4 0.673787 0.884718 Label_01 
5 0.854112 0.044506 Label_00 
6 0.225218 0.552961 Label_00 
7 0.668262 0.482514 Label_00 
8 0.935415 0.100438 Label_00 
9 0.697016 0.633550 Label_01 
(...) 

你将它传递给pairplot像这样:

sns.pairplot(data=df, hue='label') 

enter image description here