我目前正在使用python中的聚类算法。 我的数据是一个具有40,000个节点和400,000个边的稀疏矩阵。 例如:
(0, 10) 1
(0, 14) 1
我的聚类结果是python列表将类似于以下内容,但更大:
[ 9 9 9 9 9 9 9 9 9 9 2 2 2 2 2 2 3 3 3 3 3 3 3 2 6 6 0 2 7 4 2 2 2 2 4 4 4 4 4 10 6 6 6 2 7 7 5 5 1 0 0 10 10 10 0 0 0 1 1 1 1 1 1 1 1 1 1 1 6 6 6 6 2 8 8 6 1]
我最初使用networkx绘制图形,但仅适用于较小的情况 这是我的代码:
`
def plotCluster(W, predict):
color = list(col.cnames.keys())
# G = nx.from_numpy_matrix(W)
G = nx.from_scipy_sparse_matrix(W)
print(type(G))
color_map = []
for key in predict:
for i in predict[key]:
color_map.append(color[i+10])
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()`
结果将如下所示:
我现在想使用gephi,但是我只能将原始数据写为gexf文件并在gephi中打开。 我不知道如何使用自己的聚类结果并绘制与python类似的图形。
答案 0 :(得分:0)
nx.set_node_attributes()
)。 nx.write_graphml()
将文件另存为GraphML。 答案 1 :(得分:0)
我一直在拼命地寻找一种根据我们定义的节点属性为节点着色的方法。例如,我正在处理一些定价,并且想要为节点中的某个项目的价格在某个范围内的价格着色。谢谢DYZ提供的步骤,我使用了您的步骤来使事情正常进行。我将发布更详细的答案,因此它对希望这样做的其他人很有帮助
对于那些从额外文件中读取数据并希望基于此数据为图形着色的人,我使用了以下方法。我的财产数据文件是使用monty的dumpfn以字典格式保存的,所以我正在使用loadfn读取此文件
from monty.serialization import dumpfn, loadfn
from collections import defaultdict
property_data = loadfn("property_data_file")
attribute_color_dict = defaultdict(dict)
#iterate through graph nodes
for i in graph.nodes:
#get property you want to color and make a range for each color
target_property_to_color = property_data[i]['property_you_want_to_color']
if float(target_property_to_color) <=2000:
attribute_color_dict[i]['color'] = "#0000FF" #blue color
elif 2000< float(target_property_to_color) <10000:
attribute_color_dict[i]['color'] = "#008000" #green color
else:
attribute_color_dict[i]['color'] = '#FF0000' #red color
attributes = dict(attribute_color_dict)
#add attributes in the graph
nx.set_node_attributes(graph, attributes)
#save the graph in graphml format
nx.write_graphml(graph, "colored_nodes.graphml" )
现在,您可以打开gephi应用程序,然后加载图形。最初,您将看到根据节点颜色着色的边缘,但是您可以通过取消选中gephi中的节点颜色选项来消除此问题(如下所示)。