私はcsvファイルにデータを持っています、そして私は以下のようにパンダを通してそれを読んでいます
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
sub_data = pd.read_csv('sample.csv')
出力:
user_id item_id rating
0 772 36 3
1 471 228 5
2 641 401 4
3 312 98 4
4 58 504 5
このデータを使用してネットワークグラフを作成する:
edges = [tuple(x) for x in sub_data[['user_id','item_id']].values.tolist()]
B = nx.Graph()
B.add_nodes_from(sub_data['user_id'].unique(), bipartite=0, label='user')
B.add_nodes_from(sub_data['item_id'].unique(), bipartite=1, label='item')
B.add_edges_from(edges, label='rating')
以下のコードを使用してグラフをプロットします。
pos=nx.spring_layout(B)
nx.draw(B,pos,node_color='#A0CBE2',edge_color='#00bb5e',width=1,
edge_cmap=plt.cm.Blues,with_labels=True)
ノードを取得しているだけです。ここで、ノードを接続し、その接続のラベルとして評価する必要があります
あなたが書いたコードを実行しようとしましたが、2部グラフを取得することができました。あなたの場合、NetworkXは私のような二部グラフをレンダリングできると思いますが、グラフにはより多くのノードがあるため、エッジが正しく表示されません。このようなもの
エッジがほとんど見えないことがわかります。ノードの数も10に制限すると、エッジも表示される場合があります。例の複製に興味がある場合は、サンプルのcsvデータを次に示します(質問で提供されたデータにさらにいくつかのポイントを追加しました)。
,user_id,item_id,rating
0,772,36,3
1,471,228,5
2,641,401,4
3,312,98,4
4,58,504,5
5,67,98,4
6,471,229,3
これで、両側に2セットのノードがある2部レイアウトでグラフを表示する必要があることを理解しました。これを実現する代わりに、bipartite_layoutを使用する必要がありますspring_layout
。
完全なコードは次のとおりです。
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
sub_data = pd.read_csv('sample.csv')
edges = [tuple(x) for x in sub_data[['user_id','item_id']].values.tolist()]
B = nx.Graph()
B.add_nodes_from(sub_data['user_id'].unique(), bipartite=0, label='user')
B.add_nodes_from(sub_data['item_id'].unique(), bipartite=1, label='item')
B.add_edges_from(edges, label='rating')
# Now instead of spring_layout, use bipartite_layout
# First specify the nodes you want on left or top
left_or_top = sub_data['user_id'].unique()
# Then create a bipartite layout
pos = nx.bipartite_layout(B, left_or_top)
# Pass that layout to nx.draw
nx.draw(B,pos,node_color='#A0CBE2',edge_color='#00bb5e',width=1,
edge_cmap=plt.cm.Blues,with_labels=True)
また、ここで説明したようにbipartite_setsbipartite_layout
を使用する代わりに、ノードの1つのセットを明示的に渡したことに注意してください。グラフが切断され、AmbiguousSolutionErrorが発生する可能性があるためです。詳細については、こことここのドキュメントをお読みください。
このGoogleColabNotebookで完全なコードを表示することもできます。
更新:エッジを表示する場合は、次のように割り当てる必要があります(評価を追加するためのコードが正しくありませんでした)
# Extract the ratings while extracting the edges from DF
edges = [tuple(x) for x in sub_data[['user_id','item_id', 'rating']].values.tolist()]
B = nx.Graph()
B.add_nodes_from(sub_data['user_id'].unique(), bipartite=0, label='user')
B.add_nodes_from(sub_data['item_id'].unique(), bipartite=1, label='item')
# Now assign the ratings correctly to edges
for row in edges:
B.add_edge(row[0], row[1], rating=row[2])
次に、draw_netwokrx_edge_labelsを使用してエッジラベルを描画します。
left_or_top = sub_data['user_id'].unique()
pos = nx.bipartite_layout(B, left_or_top)
# Draw the graph
nx.draw(B,pos,node_color='#A0CBE2',edge_color='#00bb5e',width=1,
edge_cmap=plt.cm.Blues,with_labels=True)
# Get the edge labels for ratings
edge_labels = nx.get_edge_attributes(B,'rating')
# Draw the edge labels
nx.draw_networkx_edge_labels(B, pos, edge_labels=edge_labels)
参照:
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加