Python 如何创建关联矩阵

Python 如何创建关联矩阵,python,matrix,Python,Matrix,我正在努力创造 [[ 1, 1, 1, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 1, 1, 0, 0, 0, 0], [ 0, -1, 0, -1, 0, 1, 1, 0, 0], [ 0, 0, 0, 0, -1, -1, 0, 1, 0], [ 0, 0, -1, 0, 0, 0, -1, 0, 1], [ 0, 0, 0, 0, 0, 0, 0, -1, -1]] S=[1, 2

我正在努力创造

[[ 1,  1,  1,  0,  0,  0,  0,  0,  0],
 [-1,  0,  0,  1,  1,  0,  0,  0,  0],
 [ 0, -1,  0, -1,  0,  1,  1,  0,  0],
 [ 0,  0,  0,  0, -1, -1,  0,  1,  0],
 [ 0,  0, -1,  0,  0,  0, -1,  0,  1],
 [ 0,  0,  0,  0,  0,  0,  0, -1, -1]]

S=[1, 2, 3, 4, 5, 6]
D=[[1, 2], [1, 3], [1, 5], [2, 3], [2, 4], [3, 4], [3, 5], [4, 6], [5, 6]]
INC = [[0]*len(D) for _ in range(len(S))]

for i in range(len(D)):
在这之后,我做了一些错误的事情,我只得到了一个零矩阵

    for j in S:
        if i == j:
            INC.append(1)
我试着把它分为两个不同的列表,结果开始变得复杂起来

my_list1 = [i[0] for i in D]
my_list2 = [i[1] for i in D]

我能猜出你想要什么…你的变量名很差。我会用它来做所有的计算

import networkx as nx

nodes = [1, 2, 3, 4, 5, 6]
edges = [[1, 2], [1, 3], [1, 5], [2, 3], [2, 4], [3, 4], [3, 5], [4, 6], [5, 6]]

G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

incidence_matrix = -nx.incidence_matrix(G, oriented=True) 
# ^ this returns a scipy sparse matrix, can convert into the full array as below
# (as long as your node count is reasonable: this'll have that squared elements)
print(incidence_matrix.toarray())
输出:

[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [-1.  0.  0.  1.  1.  0.  0.  0.  0.]
 [ 0. -1.  0. -1.  0.  1.  1.  0.  0.]
 [ 0.  0.  0.  0. -1. -1.  0.  1.  0.]
 [ 0.  0. -1.  0.  0.  0. -1.  0.  1.]
 [ 0.  0.  0.  0.  0.  0.  0. -1. -1.]]

我能猜出你想要什么…你的变量名很差。我会用它来做所有的计算

import networkx as nx

nodes = [1, 2, 3, 4, 5, 6]
edges = [[1, 2], [1, 3], [1, 5], [2, 3], [2, 4], [3, 4], [3, 5], [4, 6], [5, 6]]

G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

incidence_matrix = -nx.incidence_matrix(G, oriented=True) 
# ^ this returns a scipy sparse matrix, can convert into the full array as below
# (as long as your node count is reasonable: this'll have that squared elements)
print(incidence_matrix.toarray())
输出:

[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [-1.  0.  0.  1.  1.  0.  0.  0.  0.]
 [ 0. -1.  0. -1.  0.  1.  1.  0.  0.]
 [ 0.  0.  0.  0. -1. -1.  0.  1.  0.]
 [ 0.  0. -1.  0.  0.  0. -1.  0.  1.]
 [ 0.  0.  0.  0.  0.  0.  0. -1. -1.]]

这只是一种不同于您的版本的替代方法,但您可以将其用作灵感。我相信你应该读一下

无论如何。。。这是我对关联矩阵的看法:

rows = 6
cols = 9
ones = [[0,1,2], [0,4,5], [1,3,5,6], [4,5,7], [3,6,8], [7, 8]]

INC = [ [ 0 for i in xrange(cols) ] for j in xrange(rows) ]

for i,row in enumerate(ones):
    for col in row:
        INC[i][col] = 1

[[1, 1, 1, 0, 0, 0, 0, 0, 0], 
 [1, 0, 0, 0, 1, 1, 0, 0, 0], 
 [0, 1, 0, 1, 0, 1, 1, 0, 0], 
 [0, 0, 0, 0, 1, 1, 0, 1, 0], 
 [0, 0, 0, 1, 0, 0, 1, 0, 1], 
 [0, 0, 0, 0, 0, 0, 0, 1, 1]]
请注意,我正在对行进行操作,而不是像我怀疑的那样对列进行操作。 通过在“edges”+/-中设置值插入“-1”,如果要实现此版本,请使用if-,elif+,否则使用pass


在Python3中
xrange
必须更改为
range
这只是一种与您的版本不同的替代方法,但您可以将其用作灵感。我相信你应该读一下

无论如何。。。这是我对关联矩阵的看法:

rows = 6
cols = 9
ones = [[0,1,2], [0,4,5], [1,3,5,6], [4,5,7], [3,6,8], [7, 8]]

INC = [ [ 0 for i in xrange(cols) ] for j in xrange(rows) ]

for i,row in enumerate(ones):
    for col in row:
        INC[i][col] = 1

[[1, 1, 1, 0, 0, 0, 0, 0, 0], 
 [1, 0, 0, 0, 1, 1, 0, 0, 0], 
 [0, 1, 0, 1, 0, 1, 1, 0, 0], 
 [0, 0, 0, 0, 1, 1, 0, 1, 0], 
 [0, 0, 0, 1, 0, 0, 1, 0, 1], 
 [0, 0, 0, 0, 0, 0, 0, 1, 1]]
请注意,我正在对行进行操作,而不是像我怀疑的那样对列进行操作。 通过在“edges”+/-中设置值插入“-1”,如果要实现此版本,请使用if-,elif+,否则使用pass


Python3中的必须将
xrange
更改为
range

此关联矩阵是否有模式?如果有规律的话,它可能会帮助我们想出一个答案。我不知道该怎么做。你想要什么?一个{+1,0,-1}的矩阵给出了两个你想成为+/-1的坐标列表?这个关联矩阵有模式吗?如果有规律的话,它可能会帮助我们想出一个答案。我不知道该怎么做。你想要什么?{+1,0,-1}的矩阵给出了两个坐标列表,您希望为+/-1?如何处理权重?权重可以是分配给每条边的参数,如
G.add_edge(1,2,weight=3)
为节点1和2之间的边分配等于3的权重。如何处理权重?权重可以是分配给每个边的参数,如
G.add_edge(1,2,weight=3)
为节点1和2之间的边分配等于3的权重。