简体   繁体   English

如何使用 NetworkX 库从 python 中生成的矩阵制作图表?

[英]How to make a graph from the produced matrix in python using NetworkX library?

I have this code, it produces a random matrix of 1s and 0s.我有这段代码,它产生一个随机的 1 和 0 矩阵。 I want to create a graph from this matrix where the 1s in the matrix represent a node and each node has a maximum of 3 edges.我想从这个矩阵创建一个图,其中矩阵中的 1 代表一个节点,每个节点最多有 3 条边。 How can i implement this, please help?我该如何实现这个,请帮忙?

import numpy as np
from random import sample

N = int(input("Enter the number of nodes:"))
my_matrix = np.zeros((N,N), dtype='int8')
rows = sample(range(N), N)
cols = sample(range(N), N)
points = zip(rows, cols)
for x, y in points:
    my_matrix[x, y] = 1

print(my_matrix)

If you matrix is just random, probably, you don't need it.如果您的矩阵只是随机的,那么您可能不需要它。 Instead, you can create graph from list of edges相反,您可以从边列表创建图形

import networkx as nx
from random import sample
import numpy as np
from numpy.random import randint


n = 7  # number of nodes in graph
max_connections = int(input("Enter max connections per node:"))  # input: 3

nodes = np.arange(n)
# create graph based on list of edges [(0, 1), (0, 4), ...]
gr = nx.Graph([
    #     for each node      select <= 'max_connections' nodes as connections
    (i, j) for i in range(n) for j in sample(nodes[nodes != i].tolist(), randint(1, max_connections+1))
])

# check number of connections
for n in gr.nodes():
    nei = list(gr.neighbors(n))
    while len(nei) > max_connections:
        gr.remove_edge(n, random.choice(nei))
        nei = list(gr.neighbors(n))

nx.draw_networkx(gr, with_labels=True, node_color='#7d99f5')

Graph:图形: 在此处输入图像描述

And you can get adjacency matrix using nx.adjacency_matrix()您可以使用nx.adjacency_matrix()获得邻接矩阵

nx.adjacency_matrix(gr, nodelist=sorted(gr.nodes())).todense()

matrix([[0, 1, 1, 0, 1, 0, 0],
        [1, 0, 0, 0, 0, 1, 1],
        [1, 0, 0, 1, 0, 1, 0],
        [0, 0, 1, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 1, 1],
        [0, 1, 1, 0, 1, 0, 0],
        [0, 1, 0, 1, 1, 0, 0]])

Addition to your code (function check_graph () fix two problems we have discussed):添加到您的代码中(函数check_graph ()修复了我们讨论过的两个问题):

def check_graph(graph, max_conn):
    # 1) remove self loops
    graph.remove_edges_from(nx.selfloop_edges(graph))

    # 2) remove random edge(s) if limit of edges per node have been exceeded
    for i in graph.nodes():
        # list of connections - nodes that are connected to the selected node 'i'
        nei = list(graph.neighbors(i))
        if len(nei) > max_conn:
            graph.remove_edges_from(
                # like if len(nei) - max_conn = 5 - 4 = 1, then one random edge will be selected
                np.random.choice(nei, size=(len(nei)-max_conn))
            )

# <-- insert your code here -->  N = 20

gr = nx.from_numpy_matrix(my_matrix)
check_graph(gr, max_conn=N)
nx.draw_networkx(gr, with_labels=True, node_color='#7d99f5')

Result looks a bit strange for me, but I don't know purposes of your graph, probably, it's okay.结果对我来说看起来有点奇怪,但我不知道你的图表的用途,可能,没关系。

在此处输入图像描述

This code generates randomly in maximum Nedges=3 edges (or less, if there is a self-adjacency or a bi-directional connection by the random process).此代码在最大 Nedges=3 边中随机生成(或更少,如果随机过程存在自邻接或双向连接)。 The self-adjacency is removed by setting the diagonal of the adjacency matrix to zero.通过将邻接矩阵的对角线设置为零来去除自邻接。 Might this hit the basic idea?这会触及基本思想吗? (omg, no: at node 4 there are more connections. A removal process might be incorporated) (天哪,不:在节点 4 处有更多连接。可能会合并删除过程)

import numpy as np
import networkx as nx

Nnodes = 6
Nedges = 3

#---- to initialize: generate a random adjacency matrix 
rng = np.random.default_rng()
adjM = rng.integers(1, size=(Nnodes,Nnodes))  # random adjacency matrix / with upper=1 it is a zero matrix
print(adjM)

#---- for eaach node generate randomly Nedges edges
for node in range(Nnodes):
    rand_ind = np.random.choice(np.arange(Nnodes), size=Nedges, replace=False, p=None) # generate randomly indexes
                                                                                       # you might use replace=False too with different results
    adjM[node, rand_ind] = 1   # insert the connections
    print(rand_ind)
print(adjM)

#---- remove self-adjacency
jDiag = np.arange(Nnodes)
adjM[jDiag, jDiag] = 0         # set the diagonals to zero
print(adjM)

#---- grafics
gr = nx.from_numpy_matrix(adjM)
nx.draw_networkx(gr, with_labels=True, node_size=400, node_color='#7d99f5',  edge_color='orange', width=3, font_weight='bold')

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM