在网络中建立术语关系
Building terms relations within a network
我试图表示 A 列中的数字与其在 B 中的对应值之间的关系。
A B
Home [Kitchen, Home, Towel]
Donald [US, 02 , Donald, Trump]
Trump [Trump,Family, Cat, Dog]
Dog [Dog,Cat,Paws]
A 列中的数字和 B 列中的数字是图中的节点。我想将 B 中的元素连接到 A 或相互连接。例如:
- A 中的家 link 与自身相连;如果我查看 B 列(该值仅出现在第一行),则 B 中的 Home 连接到 Kitchen 和 Towel (inging link);
- 唐纳德 link 自己在里面,因为唐纳德只在 B 中;但是,B 中的 Donald 还与 US、02 和 Trump 相关联(输入 link);
- 特朗普与唐纳德有一个即将离任的 link 和即将到来的 links(家庭、猫和狗);
- Dog 与 Trump 有一个即将离任的 link,并且有一个即将到来的 links(猫和爪子)。
那么规则应该是这样的:
- 如果 A 中的某个词在 B 中的另一行中,则创建传出 link;
- 如果 B 中也包含 A 中的单词,则为 B 中的每个单词创建一个输入 link 到 A 中的单词。
我应该如何调整我的代码?
file = file.assign(B=file.B.map(list)).explode('B')
G = nx.DiGraph()
nx.add_path(G, file['A'])
nx.add_path(G, file['B'])
nx.draw_networkx(G)
plt.show()
将您的 table 转换为 pandas dataframe
然后遍历其行,您可以像这样添加相应的边:
import networkx as nx
import pandas as pd
from pyvis.network import Network
df = pd.DataFrame(
[
['Home', ['Kitchen', 'Home', 'Towel']],
['Donald', ['US', '02' , 'Donald', 'Trump']],
['Trump', ['Trump','Family', 'Cat', 'Dog']],
['Dog', ['Dog', 'Cat' , 'Paws']]
],
columns=['A', 'B']
)
G = nx.DiGraph()
for i, j in enumerate(df['A']):
for index, row in df.iterrows():
if i != index:
if j in row['B']:
G.add_edge(row['A'], j)
else:
for n in row['B']:
if j != n:
G.add_edge(j, n)
if G.in_degree(j) == 0:
G.add_edge(j , j)
N = Network(directed=True) # using pyvis to show self loops as well
for n, attrs in G.nodes.data():
N.add_node(n)
for e in G.edges.data():
N.add_edge(e[0], e[1])
N.write_html('graph.html')
这给了我下面的图表:
希望这就是您想要的!
我试图表示 A 列中的数字与其在 B 中的对应值之间的关系。
A B
Home [Kitchen, Home, Towel]
Donald [US, 02 , Donald, Trump]
Trump [Trump,Family, Cat, Dog]
Dog [Dog,Cat,Paws]
A 列中的数字和 B 列中的数字是图中的节点。我想将 B 中的元素连接到 A 或相互连接。例如:
- A 中的家 link 与自身相连;如果我查看 B 列(该值仅出现在第一行),则 B 中的 Home 连接到 Kitchen 和 Towel (inging link);
- 唐纳德 link 自己在里面,因为唐纳德只在 B 中;但是,B 中的 Donald 还与 US、02 和 Trump 相关联(输入 link);
- 特朗普与唐纳德有一个即将离任的 link 和即将到来的 links(家庭、猫和狗);
- Dog 与 Trump 有一个即将离任的 link,并且有一个即将到来的 links(猫和爪子)。
那么规则应该是这样的:
- 如果 A 中的某个词在 B 中的另一行中,则创建传出 link;
- 如果 B 中也包含 A 中的单词,则为 B 中的每个单词创建一个输入 link 到 A 中的单词。
我应该如何调整我的代码?
file = file.assign(B=file.B.map(list)).explode('B')
G = nx.DiGraph()
nx.add_path(G, file['A'])
nx.add_path(G, file['B'])
nx.draw_networkx(G)
plt.show()
将您的 table 转换为 pandas dataframe
然后遍历其行,您可以像这样添加相应的边:
import networkx as nx
import pandas as pd
from pyvis.network import Network
df = pd.DataFrame(
[
['Home', ['Kitchen', 'Home', 'Towel']],
['Donald', ['US', '02' , 'Donald', 'Trump']],
['Trump', ['Trump','Family', 'Cat', 'Dog']],
['Dog', ['Dog', 'Cat' , 'Paws']]
],
columns=['A', 'B']
)
G = nx.DiGraph()
for i, j in enumerate(df['A']):
for index, row in df.iterrows():
if i != index:
if j in row['B']:
G.add_edge(row['A'], j)
else:
for n in row['B']:
if j != n:
G.add_edge(j, n)
if G.in_degree(j) == 0:
G.add_edge(j , j)
N = Network(directed=True) # using pyvis to show self loops as well
for n, attrs in G.nodes.data():
N.add_node(n)
for e in G.edges.data():
N.add_edge(e[0], e[1])
N.write_html('graph.html')
这给了我下面的图表:
希望这就是您想要的!