30

我有以下 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]]

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

#     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
4

5 回答 5

18

一种方法是使用NetworkX构建图,并直接将邻接矩阵作为数据框获取nx.to_pandas_adjacency。为了解释图中的共现,我们可以创建一个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图,还可以根据我们期望的行为创建邻接矩阵或其他矩阵。我们可以使用以下方法创建它:

  • nx.Graph: 如果我们想为(或)边设置1两个条目(x,y)和(y,x) 。因此,这将产生一个对称的邻接矩阵(x,y)(y,x)
  • nx.DiGraph:如果(x,y)应该只将(x,y)条目设置为1
  • nx.MultiGraph:对于与 a 相同的行为,nx.Graph但考虑边共现
  • nx.MultiDiGraph:对于与 a 相同的行为,nx.DiGraph但也要考虑边共现
于 2020-10-21T13:45:53.943 回答
8

一种方法是在轴 1 处添加pair_arraypair_array反转的 ,这可以使用[::-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]
于 2020-10-21T13:48:24.553 回答
2

您可以预先创建一个带有零的适当大小的数据框,然后通过循环对来增加适当的单元格:

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
于 2020-10-21T14:06:04.233 回答
1

这是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
于 2020-10-21T13:45:39.307 回答
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
于 2020-11-12T20:31:03.570 回答