在卫星数据集上进行光谱聚类后使用Networkx绘制图形的步骤 [英] To make a graph using Networkx after spectral clustering on moons dataset

查看:37
本文介绍了在卫星数据集上进行光谱聚类后使用Networkx绘制图形的步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成了包含20个点的卫星数据集,并对其进行了光谱聚类。我想在Networkx的帮助下使用最近邻居=3来形成一个图。其中数据点是节点,聚类后生成的亲和度矩阵是不同节点之间的边上的权重。我还需要帮助更改两个群集的节点的颜色和形状,以便将一个群集的节点与另一个群集的节点区分开来。代码如下所示。下面给出了输出图像。我只想使用近邻=3在输出图像的节点之间绘制一个图。

import numpy as np
import os
from sklearn import metrics
from sklearn.cluster import SpectralClustering
from sklearn.neighbors import DistanceMetric
from sklearn.cluster import KMeans
import pandas as pd
import pylab as pl
import sklearn.metrics as sm
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
import networkx as nx
X, y = make_moons(n_samples=20)
print(X)
print(y)
plt.scatter(X[:,0],X[:,1], marker='o', facecolors='none', edgecolor='r')
clustering=SpectralClustering(n_clusters=2,
       assign_labels='kmeans',affinity='rbf',gamma=50, degree=3,
         random_state=0)
y_predict=clustering.fit_predict(X)
y_predict
clustering.labels_
clustering.affinity_matrix_
for i in range(0, y_predict.shape[0]):
    if y[i]==0 and y_predict[i]==0 :
        c1 = pl.scatter(X[i,0],X[i,1],c='b',
    marker='+')
    elif y[i]==1 and y_predict[i]==0:
        c2 = pl.scatter(X[i,0],X[i,1], facecolors='none', edgecolor='b',
    marker='o')
    elif y[i]==0 and y_predict[i]==1:
        c3=pl.scatter(X[i,0],X[i,1],c='r',
    marker='+')
    elif y[i]==1 and y_predict[i]==1:
        c4=pl.scatter(X[i,0],X[i,1], facecolors='none', edgecolor='r',
    marker='o')
pl.show()

Image of the clustering of moons dataset is given below

推荐答案

根据您上一个问题的答案,我相信这就是您所要求的。

由于亲和力矩阵中的值都在0和1之间,但相对大小非常不同,因此我使用-10 / log(weight)作为边缘宽度。

import numpy as np
import os
from sklearn import metrics
from sklearn.cluster import SpectralClustering
from sklearn.neighbors import DistanceMetric
from sklearn.cluster import KMeans
import pandas as pd
import pylab as pl
import sklearn.metrics as sm
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.preprocessing import MinMaxScaler
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
import networkx as nx
import math
X, y = make_moons(n_samples=20)
print(X)
print(y)
#plt.scatter(X[:,0],X[:,1], marker='o', facecolors='none', edgecolor='r')
pl.figure(figsize=(15, 12))
clustering=SpectralClustering(n_clusters=2,
       assign_labels='kmeans',affinity='rbf',gamma=50, degree=3,
         random_state=0)
y_predict=clustering.fit_predict(X)
y_predict
clustering.labels_
clustering.affinity_matrix_
for i in range(0, y_predict.shape[0]):
    if y[i]==0 and y_predict[i]==0 :
        c1 = pl.scatter(X[i,0],X[i,1],c='b',
    marker='+')
    elif y[i]==1 and y_predict[i]==0:
        c2 = pl.scatter(X[i,0],X[i,1], facecolors='none', edgecolor='b',
    marker='o')
    elif y[i]==0 and y_predict[i]==1:
        c3=pl.scatter(X[i,0],X[i,1],c='r',
    marker='+')
    elif y[i]==1 and y_predict[i]==1:
        c4=pl.scatter(X[i,0],X[i,1], facecolors='none', edgecolor='r',
    marker='o')
        
for i in range(0, len(X)):
  affinity_list = clustering.affinity_matrix_[i]
  affinity_list[i] = 0 # in case we don't want to consider the node as it's own neighbour
  nearest_neighbors_indices = np.argpartition(clustering.affinity_matrix_[i], -k)[-k:]
  for j in nearest_neighbors_indices:
    G.add_edge(tuple(X[i]), tuple(X[j]), weight = clustering.affinity_matrix_[i][j])

weights = [-10/math.log(edge[-1]['weight']) for edge in G.edges.data()]
# Draw Graph
pos = {node_name: node_name for node_name in G.nodes}
nx.draw_networkx_edges(G, pos, width=weights)
pl.show()

这篇关于在卫星数据集上进行光谱聚类后使用Networkx绘制图形的步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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