質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

5311閲覧

CSVファイルを取り込んでネットワーク図を描画したい

color_8

総合スコア20

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

1クリップ

投稿2020/07/23 01:58

編集2020/07/23 02:01

▼実現したいこと
・こちらにあるような形式のCSVファイルを取り込んで、NetworkXを使って無向グラフのネットワーク図を描画したいと考えております。

・データにある'waight'を判別して、ネットワーク図のエッジの太さを可変にする。

▼環境
・Python 3.7.3
・NetworkX

▼試したコード

python

1import networkx as nx 2import csv 3import pandas as pd 4 5# csvファイルの読み込み 6Data = open('test.csv', "r") 7next(Data, None) # CSVデータの最初の行をスキップ 8Graphtype = nx.Graph() # 無向グラフを定義 9 10G = nx.parse_edgelist(Data, delimiter=',', create_using=Graphtype, 11 nodetype=int, data=(('weight', float),)) 12 13for x in G.nodes(): 14 print ("Node:", x, "has total #degree:",G.degree(x), " , In_degree: ", G.out_degree(x)," and out_degree: ", G.in_degree(x)) 15for u,v in G.edges(): 16 print ("Weight of Edge ("+str(u)+","+str(v)+")", G.get_Edge_data(u,v)) 17 18nx.draw(G) 19plt.show()

▼エラーの内容
その結果、下から5行目の「 print ("Node:", x、、、、、」ところでアトリビュートエラーが発生してしまいます。

AttributeError: 'Graph' object has no attribute 'out_degree'

これを解決する方法をご教示いただけませんでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

これを解決する方法をご教示いただけませんでしょうか。

エラーの意味としては無向グラフにはout_degreeはないと云われています。
有効グラフにはnetworkx.DiGraph.out_degreeがあるのでGraphtypeを変更すれば動作するようにはなります。

・データにある'waight'を判別して、ネットワーク図のエッジの太さを可変にする。

networkx - change color/width according to edge attributes - inconsistent resultが参考になります。

他に

  • parse_edgelist関数におけるデータの与え方
  • weightのprint部分

がおかしいです。修正したコードを以下に示します。

Python

1import networkx as nx 2import matplotlib.pyplot as plt 3 4# csvファイルの読み込み 5Data = open('net_test2.csv', "r") 6next(Data, None) # CSVデータの最初の行をスキップ 7 8#Graphtype = nx.Graph() # 無向グラフを定義 9Graphtype = nx.DiGraph() # 有向グラフ 10 11lines = Data.readlines() # 読み込む 12G = nx.parse_edgelist(lines, delimiter=',', create_using=Graphtype, 13 nodetype=int, data=(('weight', float),)) 14 15# networkx - change color/width according to edge attributes - inconsistent result 16# https://stackoverflow.com/questions/25639169/networkx-change-color-width-according-to-edge-attributes-inconsistent-result 17edges = G.edges() 18weights = [G[u][v]['weight'] for u,v in edges] 19 20for x in G.nodes(): 21 print ("Node:", x, "has total #degree:",G.degree(x), " , In_degree: ", G.out_degree(x)," and out_degree: ", G.in_degree(x)) 22# Node: 1 has total #degree: 1 , In_degree: 1 and out_degree: 0 23# Node: 2 has total #degree: 2 , In_degree: 1 and out_degree: 1 24# Node: 3 has total #degree: 2 , In_degree: 1 and out_degree: 1 25# Node: 4 has total #degree: 1 , In_degree: 0 and out_degree: 1 26 27for u,v in G.edges(): 28 print ("Weight of Edge ("+str(u)+","+str(v)+")", G[u][v]['weight']) 29# Weight of Edge (1,2) 1.0 30# Weight of Edge (2,3) 2.0 31# Weight of Edge (3,4) 3.0 32 33nx.draw(G, width=weights) 34plt.show()

net_test2.csv

target,source,weight 1,2,1 2,3,2 3,4,3

イメージ説明

投稿2020/07/23 02:38

can110

総合スコア38233

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

color_8

2020/07/23 03:14

can110さん ご丁寧に有難う御座いました!しっかり理解できました。 無向グラフにして、 out_degreeとin_degreeも消したら 無向グラフも描画できました! おせわになりました。
guest

0

out_degreeはノードから出ていくエッジの数ということなので、無向グラフだからその属性が無いのではないでしょうか。in_degreeも同様ですね。
networkx.DiGraph.out_degree

あと、G.get_edge_dataが大文字になっていました。
以上を修正したのが下記です。

python3

1import networkx as nx 2import csv 3import pandas as pd 4 5# csvファイルの読み込み 6Data = open('test.csv', "r") 7next(Data, None) # CSVデータの最初の行をスキップ 8Graphtype = nx.Graph() # 無向グラフを定義 9 10G = nx.parse_edgelist(Data, delimiter=',', create_using=Graphtype, 11 nodetype=int, data=(('weight', float),)) 12 13for x in G.nodes(): 14 print ("Node:", x, "has total #degree:",G.degree(x)) 15for u,v in G.edges(): 16 print ("Weight of Edge ("+str(u)+","+str(v)+")", G.get_edge_data(u,v)) 17 18nx.draw(G) 19plt.show() 20""" 21Node: 202 has total #degree: 1 22Node: 237 has total #degree: 1 23Node: 280 has total #degree: 1 24Node: 281 has total #degree: 1 25Node: 118 has total #degree: 2 26Node: 38 has total #degree: 9 27Node: 139 has total #degree: 9 28Node: 158 has total #degree: 9 29Node: 160 has total #degree: 11 30Node: 236 has total #degree: 9 31Node: 282 has total #degree: 11 32Node: 283 has total #degree: 9 33Node: 284 has total #degree: 9 34Node: 285 has total #degree: 9 35Node: 286 has total #degree: 9 36Node: 6 has total #degree: 3 37Node: 240 has total #degree: 3 38Weight of Edge (202,237) {'weight': 1.0} 39Weight of Edge (280,281) {'weight': 1.0} 40Weight of Edge (118,118) {'weight': 1.0} 41Weight of Edge (38,139) {'weight': 1.0} 42Weight of Edge (38,158) {'weight': 1.0} 43Weight of Edge (38,160) {'weight': 1.0} 44Weight of Edge (38,236) {'weight': 2.0} 45Weight of Edge (38,282) {'weight': 1.0} 46Weight of Edge (38,283) {'weight': 1.0} 47Weight of Edge (38,284) {'weight': 1.0} 48Weight of Edge (38,285) {'weight': 1.0} 49Weight of Edge (38,286) {'weight': 1.0} 50Weight of Edge (139,158) {'weight': 1.0} 51Weight of Edge (139,160) {'weight': 1.0} 52Weight of Edge (139,236) {'weight': 1.0} 53Weight of Edge (139,282) {'weight': 1.0} 54Weight of Edge (139,283) {'weight': 1.0} 55Weight of Edge (139,284) {'weight': 1.0} 56Weight of Edge (139,285) {'weight': 1.0} 57Weight of Edge (139,286) {'weight': 1.0} 58Weight of Edge (158,160) {'weight': 1.0} 59Weight of Edge (158,236) {'weight': 1.0} 60Weight of Edge (158,282) {'weight': 1.0} 61Weight of Edge (158,283) {'weight': 1.0} 62Weight of Edge (158,284) {'weight': 1.0} 63Weight of Edge (158,285) {'weight': 1.0} 64Weight of Edge (158,286) {'weight': 1.0} 65Weight of Edge (160,236) {'weight': 1.0} 66Weight of Edge (160,282) {'weight': 2.0} 67Weight of Edge (160,283) {'weight': 1.0} 68Weight of Edge (160,284) {'weight': 1.0} 69Weight of Edge (160,285) {'weight': 1.0} 70Weight of Edge (160,286) {'weight': 1.0} 71Weight of Edge (160,240) {'weight': 1.0} 72Weight of Edge (160,6) {'weight': 1.0} 73Weight of Edge (236,282) {'weight': 1.0} 74Weight of Edge (236,283) {'weight': 1.0} 75Weight of Edge (236,284) {'weight': 1.0} 76Weight of Edge (236,285) {'weight': 1.0} 77Weight of Edge (236,286) {'weight': 1.0} 78Weight of Edge (282,283) {'weight': 1.0} 79Weight of Edge (282,284) {'weight': 1.0} 80Weight of Edge (282,285) {'weight': 1.0} 81Weight of Edge (282,286) {'weight': 1.0} 82Weight of Edge (282,6) {'weight': 1.0} 83Weight of Edge (282,240) {'weight': 1.0} 84Weight of Edge (283,284) {'weight': 1.0} 85Weight of Edge (283,285) {'weight': 1.0} 86Weight of Edge (283,286) {'weight': 1.0} 87Weight of Edge (284,285) {'weight': 1.0} 88Weight of Edge (284,286) {'weight': 1.0} 89Weight of Edge (285,286) {'weight': 1.0} 90Weight of Edge (6,240) {'weight': 2.0} 91"""

投稿2020/07/23 02:34

編集2020/07/23 02:39
jeanbiego

総合スコア3966

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

color_8

2020/07/23 03:14

jeanbiegoさん ご回答頂き有難う御座いました! しっかりとグラフが描画できました! 大変参考になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問