Python 创建表示网络中连接的Numpy数组

Python 创建表示网络中连接的Numpy数组,python,arrays,numpy,Python,Arrays,Numpy,假设我有一个描述节点之间网络链接的数组: array([[ 1., 2.], [ 2., 3.], [ 3., 4.]]) 这将是一个线性4节点网络,具有从节点1到节点2的链路,依此类推 将此信息转换为以下格式的数组的最佳方式是什么 array([[ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.], [ 0., 0., 0., 0.]]

假设我有一个描述节点之间网络链接的数组:

array([[ 1.,  2.],
       [ 2.,  3.],
       [ 3.,  4.]])
这将是一个线性4节点网络,具有从节点1到节点2的链路,依此类推

将此信息转换为以下格式的数组的最佳方式是什么

array([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.]])
然后,列号表示“到节点”,行表示“从节点”

另一个例子是:

array([[ 1.,  2.],
       [ 2.,  3.],
       [ 2.,  4.]]) 
给予

array([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  1.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

节点ID应该是整数。此外,numpy中的行和列从零开始编号,因此我们必须在每个维度中减去一:

import numpy as np

conns = np.array([[ 1,  2],
                  [ 2,  3],
                  [ 3,  4]])
net = np.zeros((conns.max(), conns.max()), dtype=int)

# two possibilities:

# if you need the number of connections:
for conn in conns:
    net[conn[0]-1, conn[1]-1] += 1

# if you just need a 1 for existing connection(s):
net[conns[:,0]-1, conns[:,1]-1] = 1

节点ID应该是整数。此外,numpy中的行和列从零开始编号,因此我们必须在每个维度中减去一:

import numpy as np

conns = np.array([[ 1,  2],
                  [ 2,  3],
                  [ 3,  4]])
net = np.zeros((conns.max(), conns.max()), dtype=int)

# two possibilities:

# if you need the number of connections:
for conn in conns:
    net[conn[0]-1, conn[1]-1] += 1

# if you just need a 1 for existing connection(s):
net[conns[:,0]-1, conns[:,1]-1] = 1

节点ID应该是整数。此外,numpy中的行和列从零开始编号,因此我们必须在每个维度中减去一:

import numpy as np

conns = np.array([[ 1,  2],
                  [ 2,  3],
                  [ 3,  4]])
net = np.zeros((conns.max(), conns.max()), dtype=int)

# two possibilities:

# if you need the number of connections:
for conn in conns:
    net[conn[0]-1, conn[1]-1] += 1

# if you just need a 1 for existing connection(s):
net[conns[:,0]-1, conns[:,1]-1] = 1

节点ID应该是整数。此外,numpy中的行和列从零开始编号,因此我们必须在每个维度中减去一:

import numpy as np

conns = np.array([[ 1,  2],
                  [ 2,  3],
                  [ 3,  4]])
net = np.zeros((conns.max(), conns.max()), dtype=int)

# two possibilities:

# if you need the number of connections:
for conn in conns:
    net[conn[0]-1, conn[1]-1] += 1

# if you just need a 1 for existing connection(s):
net[conns[:,0]-1, conns[:,1]-1] = 1


float
可能不是存储节点ID的最佳变量类型。@eumiro Fair point,可能应该使用integer或bool,但这是我想要的示例。基于0的节点ID会使它更简单,too@njzk2是的,虽然由于源数据的原因,我一直坚持使用基于1的变量-这可能会使
-1
成为第一步,但是…
float
可能不是存储节点ID的最佳变量类型。@eumiro Fair point,可能应该使用integer或bool,但这说明了我想要什么。基于0的节点ID会使它更简单一些, too@njzk2是的,虽然由于源数据的原因,我一直坚持使用基于1的变量-可能会使
-1
成为第一步,但是…
float
可能不是存储节点ID的最佳变量类型。@eumiro Fair point,可能应该使用integer或bool,但这只是我想要的一个例子。基于0的节点ID会使它变得更简单,too@njzk2是的,虽然由于源数据的原因,我一直使用基于1的变量-可能会使
-1
成为第一步,但是…
float
可能不是存储节点ID的最佳变量类型。@eumiro Fair point,可能应该使用integer或bool,但这是我想要的示例。基于0的节点ID会使它更简单,too@njzk2是的,尽管由于源数据的原因,我一直坚持使用基于1的方法,但是可以将
-1
作为第一步,而不是循环,您不能执行
net[conns[:,0]-1,conns[:,1]-1]=1
?代替循环,你不能做
net[conns[:,0]-1,conns[:,1]-1]=1
?代替循环,你不能做
net[conns[:,0]-1,conns[:,1]-1
?代替循环,你不能做
net[conns[:,0]-1,conns[:,1]=1