一起可视化 5 个布尔变量的频率

数据挖掘 可视化
2022-02-24 15:30:58

我有一个包含 5 个变量的数据集,

abcde
1 0 0 1 0
0 1 0 1 1
0 1 1 0 0
0 0 0 1 0
1 1 1 0 0
0 1 1 0 1
1 0 1 0 0
1 0 0 1 1
0 1 0 1 1
0 0 1 1 0

我只对发生的百分比感兴趣,

发生,

| 一个 | 乙 | c | d | 电子|
.4 | .5 | .5 | .6 | .4

但是,我想以一种可以看到所有不同组之间重叠或不重叠的方式进行可视化。

任何的想法?

3个回答

在此处输入图像描述如果您有更丰富的数据(即超过 10 行),您将需要一个不满意的图。Upset plots 是一种像维恩图一样以直观的方式查看信息的方法,但对于 4 个以上的类别更有用。

一些参考资料可能会给您一些 R 中的想法和实现:

由于组合是已知的,我们可以使用一些二进制数的知识并用它来找到频率图

基本上 - 将二进制字符串转换为整数并根据整数值获得频率图

import numpy as np
import pandas as pd
from itertools import product
import matplotlib.pyplot as plt

# test data, 1 of every 32 combinations
combs = np.array(map(list, product([0, 1], repeat=5)))
# store in dataframe
df = pd.DataFrame(data={'a': combs[:, 0], 'b': combs[:, 1], 'c': combs[:, 2], 'd': combs[:, 3], 'e': combs[:, 4]})
# concatenate the binary sequences to strings
df['concatenate'] = df[list('abcde')].astype(str).apply(''.join, axis=1)

# to convert binary strings to integers
def int2(x):
    return int(x, 2)

# every combination has a unique value
df['unique_values'] = df['concatenate'].apply(int2)

# prepare labels for the frequency plot
variables = list('abcde')
labels = []
for combination in df.concatenate:
    tmp = ''.join([variables[i] for i, x in enumerate(combination) if x != '0'])
    labels.append(tmp)

fig, ax = plt.subplots()
counts, bins, patches = ax.hist(df.unique_values, bins=32, rwidth=0.8)

# turn of the
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    top=False,         # ticks along the top edge are off
    labelbottom=False)

# calculate the bin centers
bin_centers = 0.5 * np.diff(bins) + bins[:-1]
ax.set_xticks(bin_centers)
for label, x in zip(labels, bin_centers):
    # replace integer mapping with the labels
    ax.annotate(str(label), xy=(x, 0), xycoords=('data', 'axes fraction'),
        xytext=(0, -5), textcoords='offset points', va='top', ha='center', rotation='30')

plt.show()

在此处输入图像描述

使用Wolfram 语言,您可以使用AbsoluteCorrelation .

t = {
     {1, 0, 0, 1, 0}, {0, 1, 0, 1, 1}, 
     {0, 1, 1, 0, 0}, {0, 0, 0, 1, 0}, 
     {1, 1, 1, 0, 0}, {0, 1, 1, 0, 1}, 
     {1, 0, 1, 0, 0}, {1, 0, 0, 1, 1}, 
     {0, 1, 0, 1, 1}, {0, 0, 1, 1, 0}
    }

然后

MatrixForm[ac = AbsoluteCorrelation[t]] 

数学图形

其中对角线是边缘列频率,非对角线是联合频率。也就是说,ac[[1,1]]变量a以 0.4 的频率出现,而ac[[1,2]](第 1 行,第 2 列)变量与频率为 0.1的变量a共同出现b

这可以使用MatrixPlotArrayPlot进行可视化。

MatrixPlot[
 ac 
 , FrameTicks -> {Transpose@{Range@5, CharacterRange["a", "e"]}}
 , PlotLegends -> Automatic]

数学图形

希望这可以帮助。