`node2vec` `TSNE` 待解决问题
生活随笔
收集整理的這篇文章主要介紹了
`node2vec` `TSNE` 待解决问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. node2vec
node2vec: Scalable Feature Learning for Networks
https://github.com/aditya-grover/node2vec
這篇文章不是將它的思路和代碼。只是因為在python中居然集成了node2vec,故而感覺很新奇,所以這里來進行一個簡單的小實驗。
1. 安裝
pip install node2vec2. 訓練
使用karate_club_graph數據集,然后進行表示學習。
from node2vec import Node2Veca = Karate_graph() # a.draw_graph() # 繪制圖像 # matrix = a.get_graph_adjacency_matrix() gn = a.get_karate_club_graph() n2v = Node2Vec(gn, dimensions=20, walk_length=10, num_walks=20) model = n2v.fit(window=5, min_count=1) # 訓練的向量表示 node_embeddings = model.wv.vectors而,Karate_graph類,是我自定義的一個類,方便獲取一些數據,如下:
import networkx as nx import numpy as np import matplotlib.pyplot as plt import community as community_louvain import matplotlib.cm as cm %matplotlib inlineclass Karate_graph(object):def __init__(self):self.G = nx.karate_club_graph()#first compute the best partitionself.partition = community_louvain.best_partition(self.G)# colorsself.colors = ["BlueViolet", "LawnGreen", "OrangeRed", "Turquoise"] # 33-1紫、32-1綠、1-1紅、7-1藍 , 減1是因為圖是從0開始的def draw_graph(self):"""繪制其圖像"""# draw the graphpos = nx.spring_layout(self.G)# 節點對應的顏色的封裝resu = {}for com in list(set(self.partition.values())):nodes = [key for key in self.partition.keys() if self.partition[key] == com]if 32 in nodes:resu[self.colors[0]] = nodeselif 31 in nodes:resu[self.colors[1]] = nodeselif 0 in nodes:resu[self.colors[2]] = nodeselse:resu[self.colors[3]] = nodes# 生成節點對應的label字典labels = {}for node in list(self.G.nodes()):labels[node] = node+1options = {"node_size": 250, "alpha": 0.8}# nodesfor color in self.colors:nx.draw_networkx_nodes(self.G, pos, nodelist=resu[color], label=resu[color], node_color=color, **options)# edgesnx.draw_networkx_edges(self.G, pos, alpha=0.5)# labelsnx.draw_networkx_labels(self.G, pos, labels, font_size=10, font_color='black')plt.axis("off") # 關閉坐標軸plt.show()# 保存下顏色字典self.node_colors_dict = resudef get_graph_adjacency_matrix(self):"""得到其鄰接矩陣表示"""return np.array(nx.adjacency_matrix(self.G).todense(), dtype=np.float32)def get_karate_club_graph(self):"""得到networkx格式的數據圖"""return self.Gdef get_node_colors(self):"""返回節點的顏色列表,方便后面的圖像的繪制"""node_colors = [0 for val in range(len(self.G))]for key in self.node_colors_dict.keys():for node in self.node_colors_dict[key]:node_colors[node] = keyreturn node_colors可以簡單的使用,a.draw_graph()來繪制圖像,即:
然后,在使用node2vec中,可以簡單的計算節點相似的節點,即:
2. TNSE
一種數據降維的算法實現,相似的還有PCA、LE、MDS、IsoMap等。
它的一種實現在sklearn中集成了,這里就直接使用。
但是,得到的東西,不具有論文中常見的聚類效果,如:
對比,Rank2vec中的實驗:
這里,就百度了一下,來自這篇博客,here:
那么,猜測是否是數據本身就具有聚類效果,才會導致學習到的嵌入表示具有一定的聚類效果?
看看,iris數據,即:
效果:
很明顯,故而,是數據本身具有一定的聚集效果。
那么,在使用node2vec學習節點的表示,再經過t-sne,是如何得到這個圖的呢:
還是,中途使用了一些聚類算法來做?
待解決問題!
總結
以上是生活随笔為你收集整理的`node2vec` `TSNE` 待解决问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qt opengl 天空盒
- 下一篇: 【更新】iWebOffice2009全文