python - Numpy-从二维数组中获取邻居矩阵

标签 python numpy undirected-graph

我有一个无向图:

A,B
A,C
A,F
B,D
C,D
C,F
E,D
E,F

我需要将其转换为矩阵 N,其中

N[x,y] = 1 if x is neighbor of y (like A and B) and 0 if not

最快的方法是什么?

NOTA 该图存储为 numpy 字符串数组:

array([['A', 'B'],
       ['A', 'C'],
       ['A', 'F'],
       ['B', 'D'],
       ['C', 'D'],
       ['C', 'F'],
       ['E', 'D'],
       ['E', 'F']], 
      dtype='|S4')

预期输出:

   A B C D E F
A  1 1 1 0 0 1
B  1 1 0 1 0 0 
C  1 0 1 1 0 1
D  0 1 1 1 1 0
E  0 0 0 1 1 1 
F  1 0 1 0 1 1

谢谢

最佳答案

这是一种 NumPythonic 方法 -

# Tag each string with a numeric ID based on the uniqueness among other strings
_,ID = np.unique(graph,return_inverse=True)
M = ID.reshape(graph.shape)

# Consider each row of numeric IDs as an indexing tuple of a 2D array.
# Calculate the linear indices corresponding to each tuple.
n = M.max()+1
idx1 = M[:,0]*(n) + M[:,1]
idx2 = M[:,1]*(n) + M[:,0]

# Setup output array with 1s on the diagonal.
out = np.eye(n,dtype=int)

# Put 1s at places specified by the earlier computed pairs of linear indices
np.put(out,[idx1,idx2],1)

示例输入、输出-

In [93]: graph
Out[93]: 
array([['A', 'B'],
       ['A', 'C'],
       ['A', 'F'],
       ['B', 'D'],
       ['C', 'D'],
       ['C', 'F'],
       ['E', 'D'],
       ['E', 'F']], 
      dtype='|S4')

In [94]: out
Out[94]: 
array([[1, 1, 1, 0, 0, 1],
       [1, 1, 0, 1, 0, 0],
       [1, 0, 1, 1, 0, 1],
       [0, 1, 1, 1, 1, 0],
       [0, 0, 0, 1, 1, 1],
       [1, 0, 1, 0, 1, 1]])

运行时测试

本节基本上比较了迄今为止列出的所有解决案例的性能方法(在这篇文章和其他文章中)。

定义函数-

def networkx_based(a): #@atomh33ls's solution code
    A=nx.Graph()
    for x in a:
        A.add_edge(x[0],x[1])
    return nx.adjacency_matrix(A)

def vectorize_based(data): # @plonser's solution code
    ord_u = np.vectorize(lambda x: ord(x)-65)
    s = ord_u(data)
    res = np.zeros((s.max()+1,s.max()+1),dtype=int)
    res[s[:,0],s[:,1]] = 1
    res_sym = res + res.T
    np.fill_diagonal(res_sym,1)
    return res_sym

def unique_based(graph): #solution from this post
    _,ID = np.unique(graph,return_inverse=True)
    M = ID.reshape(graph.shape)
    n = M.max()+1
    idx1 = M[:,0]*(n) + M[:,1]
    idx2 = M[:,1]*(n) + M[:,0]
    out = np.eye(n,dtype=int)
    np.put(out,[idx1,idx2],1)
    return out

时间 -

In [321]: N = 1000

In [322]: arr = np.random.randint(65,90,(N,2))

In [323]: graph = np.array([map(chr,a) for a in arr],dtype='S4')

In [324]: %timeit networkx_based(graph)
100 loops, best of 3: 3.79 ms per loop

In [325]: %timeit vectorize_based(graph)
1000 loops, best of 3: 760 µs per loop

In [326]: %timeit unique_based(graph)
1000 loops, best of 3: 405 µs per loop

关于python - Numpy-从二维数组中获取邻居矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694826/

相关文章:

javascript - 外部 javascript/jquery 未加载到 django 项目内部

python - 编译 .pyx 文件时缺少 numpy/arrayobject.h

python - 一次性对 scipy 的 `curve_fit` 进行多次迭代

python - Django 在对象中出现错误。过滤器 : too many values to unpack

python - 在 PySNMP 代理中实现表

Python iGraph 标签截止

r - 找到反向边并从其相反方向减去其权重

python - 小列表的长排列

python - 在 python 中重现 Matlab 的 SVD