如何将群集/类/组标签的数据帧转换为成对/未成对的成对数据帧?

时间:2019-10-25 23:56:51

标签: python pandas numpy transform transpose

我试图在聚类结果之间找到一致性,但是我很难有效地做到这一点。我想转换pandasDataFramei=node的{​​{1}} j=iteration对象(或字典)。我当前的方法是遍历所有可能性,但是我觉得有一种更有效的方法可以做到这一点。对于大型数据集,这将永远存在。

[i,j]=cluster/group

enter image description here

我如何才能更有效地完成这一部分,而不是蛮力迭代?有没有办法利用import string import pandas as pd import numpy as np from collections import * # Get alphabet as nodes nodes = list(string.ascii_lowercase) data = {0: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 1: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 2: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 3: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 4: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 5: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 6: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 7: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 8: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}, 9: {'a': 0, 'b': 0, 'c': 0, 'd': 1, 'e': 0, 'f': 0, 'g': 2, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 3, 'n': 0, 'o': 4, 'p': 0, 'q': 0, 'r': 3, 's': 0, 't': 0, 'u': 0, 'v': 4, 'w': 2, 'x': 2, 'y': 1, 'z': 0}} df_clusters = pd.DataFrame(data) 数组呢?

NumPy

enter image description here

1 个答案:

答案 0 :(得分:1)

使用numpy广播,我们可以将a行与整个数据帧进行比较,然后将b与整个数据帧进行比较,依此类推:

# `x` is a table of 26 rows and 10 columns
x = df_clusters.values

# `y` is an array of 26 tables, each having 1 row and 10 columns
y = x[:, None]

# Using numpy broadcasting, `z` contains the result of comparing each
# table in `y` against `x`. So the shape of `z` is 26 x 26 x 10
z = x == y

# Reshaping `z` by merging the first two dimensions
data = z.reshape((z.shape[0] * z.shape[1], z.shape[2])).astype('int')

# idx is the 2-permutation of values in `df_clusters.index`:
# (a,a), (a,b), ..., (a,z), (b,a), (b,b), ...
idx = pd.MultiIndex.from_product([df_clusters.index, df_clusters.index], names=['node1', 'node2'])
result = pd.DataFrame(data, index=idx, columns=df_clusters.columns)

# We don't want all permutations, only the unique combinations,
# so we have to slice the frame
from itertools import combinations
final_idx = list(combinations(df_clusters.index, 2))
result = result.loc[final_idx]

由于C(26,2)= 325,结果是325 x 10数据帧。这是一个小样本:

             0  1  2  3  4  5  6  7  8  9
node1 node2                              
a     b      1  1  1  1  1  1  1  1  1  1
      c      1  1  1  1  1  1  1  1  1  1
      d      0  0  0  0  0  0  0  0  0  0
      e      1  1  1  1  1  1  1  1  1  1
      f      1  1  1  1  1  1  1  1  1  1