在对卫星数据集进行光谱聚类后使用 Networkx 制作图表

To make a graph using Networkx after spectral clustering on moons dataset

我生成了包含 20 个点的卫星数据集,并对其进行了光谱聚类。我想在 Networkx 的帮助下使用 nearest neighbors = 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()