如何将两列数组转换为具有出现次数的矩阵?

How can I convert a two column array to a matrix with counts of occurences?

我有以下 numpy 数组:

import numpy as np

pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382), 
                       (31, 183), (31, 267), (31, 382), (183, 267), (183, 382)])

print(pair_array)

#[[205 254]
# [205 382]
# [254 382]
# [ 18  69]
# [205 382]
# [ 31 183]
# [ 31 267]
# [ 31 382]
# [183 267]
# [183 382]]

有没有办法将此数组转换为包含所有可能组合出现次数的对称 pandas 数据框? 我期待这样的事情:

#     18  31  69 183 205 254 267 382 
#  18  0   0   1   0   0   0   0   0
#  31  0   0   0   1   0   0   1   1
#  69  1   0   0   0   0   0   0   0
# 183  0   1   0   0   0   0   1   1
# 205  0   0   0   0   0   1   0   2
# 254  0   0   0   0   1   0   0   1
# 267  0   1   0   1   0   0   0   0
# 382  0   1   0   1   2   1   0   0

这是crosstab:

pd.crosstab(pair_array[:,0], pair_array[:,1])

输出:

col_0  69   82   183  254  267  382
row_0                              
18       1    0    0    0    0    0
31       0    1    1    0    1    0
183      0    0    0    0    1    1
205      0    0    0    1    0    2
254      0    0    0    0    0    1

一种方法是使用 NetworkX and obtain the adjacency matrix directly as a dataframe with nx.to_pandas_adjacency. To account for the co-occurrences of the edges in the graph, we can create a nx.MultiGraph 构建图形,它允许连接同一对节点的多条边:

import networkx as nx

G = nx.from_edgelist(pair_array, create_using=nx.MultiGraph)
nx.to_pandas_adjacency(G, nodelist=sorted(G.nodes()), dtype='int')

      18   31   69   183  205  254  267  382
18     0    0    1    0    0    0    0    0
31     0    0    0    1    0    0    1    1
69     1    0    0    0    0    0    0    0
183    0    1    0    0    0    0    1    1
205    0    0    0    0    0    1    0    2
254    0    0    0    0    1    0    0    1
267    0    1    0    1    0    0    0    0
382    0    1    0    1    2    1    0    0

构建一个 NetworkX 图,还可以根据我们预期的行为创建邻接矩阵或其他矩阵。我们可以使用 a:

创建它
  • nx.Graph:如果我们想为(x,y)(或(y,x)) 边缘。这将因此产生一个对称的邻接矩阵
  • nx.DiGraph:如果(x,y)应该只将(x,y)条目设置为1
  • nx.MultiGraph:对于与 nx.Graph 相同的行为,但占边缘 co-occurrences
  • nx.MultiDiGraph:对于与 nx.DiGraph 相同的行为,但也占边缘 co-occurrences

一种方法是在 pair_array 后附加 pair_array 在轴 1 处反转,这可以使用 [::-1] 完成。并附加使用 np.vstack/np.r_/np.concatenate.

现在使用pd.crosstab进行交叉制表。

all_vals = np.r_[pair_array, pair_array[:, ::-1]]
pd.crosstab(all_vals[:, 0], all_vals[:, 1])

col_0  18   31   69   183  205  254  267  382
row_0                                        
18       0    0    1    0    0    0    0    0
31       0    0    0    1    0    0    1    1
69       1    0    0    0    0    0    0    0
183      0    1    0    0    0    0    1    1
205      0    0    0    0    0    1    0    2
254      0    0    0    0    1    0    0    1
267      0    1    0    1    0    0    0    0
382      0    1    0    1    2    1    0    0

正如@QuangHoang指出的,当相同的对出现不止一次时,即[(18, 18), (18, 18), ...],那么使用

rev = pair_array[:, ::-1]
m = (pair_array == rev)
rev = rev[~np.all(m, axis=1)]
all_vals = np.r_[pair_arr, rev]

您可以事先用零创建一个适当大小的数据框,然后通过遍历对来增加适当的单元格:

import numpy as np
import pandas as pd

pair_array = np.array([(205, 254), (205, 382), (254, 382), (18, 69), (205, 382),
                       (31, 183), (31, 267), (31, 82), (183, 267), (183, 382)])

vals = sorted(set(pair_array.flatten()))
n = len(vals)

df = pd.DataFrame(np.zeros((n, n), dtype=np.int), columns=vals, index=vals)

for r, c in pair_array:
    df.at[r, c] += 1
    df.at[c, r] += 1

print(df)

输出:

     18   31   69   82   183  205  254  267  382
18     0    0    1    0    0    0    0    0    0
31     0    0    0    1    1    0    0    1    0
69     1    0    0    0    0    0    0    0    0
82     0    1    0    0    0    0    0    0    0
183    0    1    0    0    0    0    0    1    1
205    0    0    0    0    0    0    1    0    2
254    0    0    0    0    0    1    0    0    1
267    0    1    0    0    1    0    0    0    0
382    0    0    0    0    1    2    1    0    0

如果您可以添加 pandas 作为依赖项,您可以使用此实现

>>> import pandas as pd
>>> df = pd.DataFrame(pair_array)
>>> pd.crosstab(df[0], df[1])
1    69   183  254  267  382
0
18     1    0    0    0    0
31     0    1    0    1    1
183    0    0    0    1    1
205    0    0    1    0    2
254    0    0    0    0    1