将矩阵乘积转换为nx.Graph

时间:2018-08-19 09:58:34

标签: python networkx graph-theory

我有一个这样的数据框

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'a' : [1,1,0,0], 'b': [0,1,1,0], 'c': [0,0,1,1]})

我得到一个矩阵乘积

df.T.dot(df)

   a  b  c
a  2  1  0
b  1  2  1
c  0  1  2

我想获得nx.Graph,然后到达nx.draw_networkx

G.add_node('a', weight = 2 ) # 2 means sum of 'a'
....................
G.add_edge('a','b',range=1) # 1 means cell's value at the intersection
................

我当然可以如上所述手动操作,但是在实际数据集中,更多数据。是否有更简单的方法将矩阵乘积转换为nx.Graph

2 个答案:

答案 0 :(得分:1)

使用循环!如果我对您的理解正确,那么以下方法应该有效,

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'a': [1, 1, 0, 0], 'b': [0, 1, 1, 0], 'c': [0, 0, 1, 1]})

product = df.T.dot(df)

graph = nx.Graph()

for column in df.columns:
    graph.add_node(column, weight=product[column][column])

for row_label, values in product.iterrows():
    for column_label, value in values.iteritems():
        if row_label != column_label:
            graph.add_edge(row_label, column_label, range=value)

nx.draw_networkx(graph)
plt.show()

哪个产量

Example graph

答案 1 :(得分:1)

没有循环的另一种选择是使用pandas数据帧重塑和networkx 2.0与pandas集成方法:

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'a': [1, 1, 0, 0], 'b': [0, 1, 1, 0], 'c': [0, 0, 1, 1]})

product = df.T.dot(df)

graph = nx.Graph()

dfG = product.stack().rename('value').rename_axis(['source','target']).reset_index()

G = nx.Graph()

G = nx.from_pandas_edgelist(dfG.query('source != target'), 'source', 'target', 'value', G)

attr_dict = dfG.query('source == target').set_index('source')['value'].to_dict()
nx.set_node_attributes(G, attr_dict,'weight')

nx.draw_networkx(G)

输出:

enter image description here