使用scikit进行光谱聚类学习通过Networkx生成的图 [英] Spectral clustering using scikit learn on graph generated through networkx

查看:228
本文介绍了使用scikit进行光谱聚类学习通过Networkx生成的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3000x50的特征向量矩阵.我使用sklearn.metrics.pairwise_distances作为'Similarity_Matrix'获得了类似矩阵.现在,我使用networkx使用在上一步中生成的相似度矩阵作为G=nx.from_numpy_matrix(Similarity_Matrix)来创建图.我现在想在此图G上执行光谱聚类,但是一些Google搜索未能提供scikit在该图上学习光谱聚类的一个不错的例子:(官方文档显示了如何对某些图像数据进行光谱聚类.至少对于像我这样的新手来说还不太清楚.

I have a 3000x50 feature vector matrix. I obtained a similarity matrix for this using sklearn.metrics.pairwise_distances as 'Similarity_Matrix'. Now I used networkx to create a graph using the similarity matrix generated in the previous step as G=nx.from_numpy_matrix(Similarity_Matrix). I want to perform spectral clustering on this graph G now but several google searches have failed to provide a decent example of scikit learn spectral clustering on this graph :( The official documentation shows how spectral clustering can be done on some image data which is highly unclear at least to a newbie like myself.

任何人都可以给我一个代码示例,或者使用networkx,scikit Learn等进行图形切割或图形分区.

Can anyone give me a code sample for this or for graph cuts or graph partitioning using networkx, scikit learn etc.

谢谢!

推荐答案

adj_matrix = nx.from_numpy_matrix将帮助您创建一个邻接矩阵,该矩阵将成为您的亲和矩阵.您需要像下面这样喂scikit-learn:SpectralClustering(affinity = 'precomputed', assign_labels="discretize",random_state=0,n_clusters=2).fit_predict(adj_matrix)

adj_matrix = nx.from_numpy_matrix will help you create an adjacency matrix which will be your affinity matrix. You need to feed this to scikit-learn like this: SpectralClustering(affinity = 'precomputed', assign_labels="discretize",random_state=0,n_clusters=2).fit_predict(adj_matrix)

如果没有任何相似度矩阵,则可以将相似性"参数的值更改为"rbf"或"nearest_neighbors".以下示例说明了整个光谱聚类管道:

If you don't have any similarity matrix, you can change the value of 'affinity' param to 'rbf' or 'nearest_neighbors'. An example below explains the entire Spectral Clustering pipeline:

import sklearn
import networkx as nx
import matplotlib.pyplot as plt

'''Graph creation and initialization'''
G=nx.Graph()
G.add_edge(1,2)  # default edge weight=1
G.add_edge(3,4,weight=0.2) #weight represents edge weight or affinity
G.add_edge(2,3,weight=0.9) 
G.add_edge("Hello", "World", weight= 0.6)

'''Matrix creation'''
adj_matrix = nx.to_numpy_matrix(G) #Converts graph to an adj matrix with adj_matrix[i][j] represents weight between node i,j.
node_list = list(G.nodes()) #returns a list of nodes with index mapping with the a 

'''Spectral Clustering'''
clusters = SpectralClustering(affinity = 'precomputed', assign_labels="discretize",random_state=0,n_clusters=2).fit_predict(adj_matrix)
plt.scatter(nodes_list,clusters,c=clusters, s=50, cmap='viridis')
plt.show()

这篇关于使用scikit进行光谱聚类学习通过Networkx生成的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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