【问题标题】:To make a graph using Networkx after spectral clustering on moons dataset在卫星数据集上进行光谱聚类后使用 Networkx 制作图形
【发布时间】:2021-10-25 22:21:02
【问题描述】:

我已经生成了具有 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

【问题讨论】:

    标签: python machine-learning data-science cluster-analysis networkx


    【解决方案1】:

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

    由于亲和矩阵中的值都在 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()
    

    【讨论】:

    • 您好,谢谢您的回答。但是在您的输出图中,我无法正确看到连接节点的边缘。最近的邻居节点之间的连接不清楚,我也不想在卫星的上部有一个红色圆圈,围绕“+”号@Frodnar
    • 很公平,@Ytt。我根据您的反馈更新了图形和代码。我使边缘宽度与-10 / log(weight) 成比例。我还放大了图以显示更多细节。如果答案看起来不错,请将其标记为已接受。
    • 感谢@Frodnar 的大力帮助。我正是想要这个数字。
    • 我只是想要一点帮助。可以使用 KMeans 聚类创建此图吗?对此的答案将不胜感激。@Frodnar
    • @Ytt 不要写评论说“谢谢”,而是使用upvote按钮,如果它是正确回答您问题的最佳答案,请将答案标记为正确,以便其他有与您将来可能很容易找到答案的相同问题。请花 1 分钟阅读此链接:stackoverflow.com/help/someone-answers 作为旁注...是的,您可以使用 SciKit Clustering 包中提供的 KMeans 方法,而不是 SpectralClustering,它应该可以正常工作。我建议您自己尝试一下,看看它是否真的有效。
    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 2018-02-25
    • 2014-07-04
    • 2016-02-22
    • 2021-02-18
    • 2015-09-13
    • 1970-01-01
    • 2019-07-30
    相关资源
    最近更新 更多