簡體   English   中英

Networkx:如何從 csv 文件創建圖邊?

[英]Networkx : How to create graph edges from a csv file?

我正在嘗試使用 .networkx 創建一個圖形,到目前為止,我已經從以下文本文件創建了節點:文件 1(user_id.txt) 示例數據:

user_000001
user_000002
user_000003
user_000004
user_000005
user_000006
user_000007

文件 2(user_country.txt) 示例數據:如果用戶沒有輸入他的國家詳細信息,也包含一些空行

 Japan
 Peru
 United States

 Bulgaria
 Russian Federation
 United States

文件 3(user_agegroup.txt) 數據:包含四個年齡組

 [12-18],[19-25],[26-32],[33-39]

我還有另外兩個文件,其中包含以下示例數據,用於在圖中添加邊

文件 4(id,agegroup.txt)

user_000001,[19-25]
user_000002,[19-25]
user_000003,[33-39]
user_000004,[19-25]
user_000005,[19-25]
user_000006,[19-25]
user_000007,[26-32]

文件 5(id,country.txt)

(user_000001,Japan)
(user_000002,Peru)
(user_000003,United States)
(user_000004,)
(user_000005,Bulgaria)
(user_000006,Russian Federation)
(user_000007,United States)

到目前為止,我已經編寫了以下代碼來繪制僅包含節點的圖形:(請檢查代碼,因為print g.number_of_nodes()從不打印正確的節點數,盡管print g.nodes()顯示正確的節點數。)

import csv
import networkx as nx
import matplotlib.pyplot as plt
g=nx.Graph()

#extract and add AGE_GROUP nodes in graph
f1 = csv.reader(open("user_agegroup.txt","rb"))
for row in f1: 
    g.add_nodes_from(row)
    nx.draw_circular(g,node_color='blue')

#extract and add COUNTRY nodes in graph
f2 = csv.reader(open('user_country.txt','rb'))
for row in f2:
    g.add_nodes_from(row) 
    nx.draw_circular(g,node_color='red')

#extract and add USER_ID nodes in graph
f3 = csv.reader(open('user_id.txt','rb'))
for row in f3:
    g.add_nodes_from(row)
    nx.draw_random(g,node_color='yellow')

print g.nodes()
plt.savefig("path.png")
print g.number_of_nodes()
plt.show()

除此之外,我不知道如何從 file4 和 file5 添加邊。 對此代碼的任何幫助表示贊賞。 謝謝。

為簡化起見,我在user_id.txt和id,country.txt文件中創建了用戶ID [1,2,3,4,5,6,7]。 您的代碼中存在一些問題:

1-首先將一些節點添加到圖形中(例如從user_id.txt文件中),然后繪制它,然后從另一個文件向圖形添加一些其他節點,然后在同一圖形上再次重新繪制整個圖形。 所以,最后你在一個圖中有很多圖形。

2-您使用draw_circular方法繪制兩次,這就是為什么藍色節點從未出現,因為它們被“紅色”節點覆蓋。

我對您的代碼進行了一些更改,最后只繪制了一次。 為了繪制具有所需顏色的節點,我在添加節點時添加了一個名為colors的屬性。 然后我使用這個屬性來構建一個我發送到draw_networkx函數的顏色映射。 最后,添加邊緣有點棘手,因為id,country.txt中的空字段因此我必須在創建圖形之前刪除空節點。 這是后來出現的代碼和圖。

G=nx.Graph()

#extract and add AGE_GROUP nodes in graph
f1 = csv.reader(open("user_agegroup.txt","rb"))
for row in f1: 
    G.add_nodes_from(row, color = 'blue')

#extract and add COUNTRY nodes in graph
f2 = csv.reader(open('user_country.txt','rb'))
for row in f2:
    G.add_nodes_from(row, color = 'red') 

#extract and add USER_ID nodes in graph
f3 = csv.reader(open('user_id.txt','rb'))
for row in f3:
    G.add_nodes_from(row, color = 'yellow')

f4 = csv.reader(open('id,agegroup.txt','rb'))
for row in f4:
    if len(row) == 2 : # add an edge only if both values are provided
        G.add_edge(row[0],row[1])

f5 = csv.reader(open('id,country.txt','rb'))

for row in f5:
    if len(row) == 2 : # add an edge only if both values are provided
        G.add_edge(row[0],row[1])
# Remove empty nodes
for n in G.nodes():
    if n == '':
        G.remove_node(n)
# color nodes according to their color attribute
color_map = []
for n in G.nodes():
    color_map.append(G.node[n]['color'])
nx.draw_networkx(G, node_color = color_map, with_labels = True, node_size = 500)

plt.savefig("path.png")

plt.show()

在此輸入圖像描述

您可以使用 for like:

for a,b in df_edges.iterrows():
    G.add_edges_from([(b['source'], b['target'])])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM