data1 = { 'node1': [1,1,1,2],
'node2': [2,3,5,4],
'weight': [1,1,1,1], }
df1 = pd.DataFrame(data1, columns = ['node1','node2','weight'])
我想从pandas dataframe创建一个邻接矩阵。数据框有无向图的边缘列表
输出:
0 1 1 0 1
1 0 0 1 0
1 0 0 0 0
0 1 0 0 0
1 0 0 0 0
我的代码:
def adjmat():
print 'begun creating adjen mat'
data = sc.loadtxt('training.csv', dtype='str', delimiter=',',skiprows=1)
data = sc.transpose(data)
row1 = data[1].astype(int)
row2 = data[2].astype(int)
weight=data[3].astype(int)
n=0
n1=0
n2=0
n1=max(row1)
n2=max(row2)
if n1>n2:
Amat=sc.zeros((n1,n1))
#matrix=sc.zeros((n1,n1))
n=n1
else:
Amat=sc.zeros((n2,n2))
#matrix=sc.zeros((n2,n2))
n=n2
for i in range(0,len(row1)):
row=row1[i]
col=row2[i]
Amat[row-1][col-1]=weight[i]
i_lower = np.tril_indices(n, -1)
Amat[i_lower] = Amat.T[i_lower]
return Amat
我正在寻找可扩展的代码。现在我正在使用具有100,000个节点的数据集,并且此代码无法处理如此大的数据集。
答案 0 :(得分:1)
Using networkx.....
data1 = { 'node1': [1,1,1,2],
'node2': [2,3,5,4],
'weight': [1,1,1,1], }
df1 = pd.DataFrame(data1, columns = ['node1','node2','weight'])
G=nx.from_pandas_dataframe(df1,'node1','node2','weight')
Adjtraining = nx.adjacency_matrix(G)
print Adjtraining.todense()
output
[[0 1 1 0 1]
[1 0 0 1 0]
[1 0 0 0 0]
[0 1 0 0 0]
[1 0 0 0 0]]